简体   繁体   English

将 Java Swing JTable 标头导出​​到 Excel

[英]Export Java Swing JTable header to Excel

In my application, I have exported the table content to Excel already, but the result excludes the table header.在我的应用程序中,我已经将表格内容导出到 Excel,但结果不包括表格标题。 Wondering how to export JTable to Excel include the table header?想知道如何将JTable导出到 Excel 包括表头? I tried couple ways, but still can not see the table header in excel, the following is my code:我尝试了几种方法,但仍然无法在excel中看到表头,以下是我的代码:

    defautTableModel = new DefaultTableModel(null,columnNames){
        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };

    // the jTable row are generated dynamically.
    final JTable jTable = new JTable(defautTableModel);

    jTable.setLocation(20,60);
    jTable.setSize(950,450);
    jTable.setRowHeight(25);

    JTableHeader jTableHeader = jTable.getTableHeader();
    jTableHeader.setLocation(20,30);
    jTableHeader.setSize(950,30);
    jTableHeader.setFont(new Font(null, Font.BOLD, 16));
    jTableHeader.setResizingAllowed(true);
    jTableHeader.setReorderingAllowed(true);

    jTable.add(jTableHeader);

    JScrollPane tablePanel = new JScrollPane(jTable);
    tablePanel.setLocation(10,10);
    tablePanel.setSize(960,400);

    // export data to excel method 

    public void exportToExcel(){
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet();
    for (int i = 0; i < defautTableModel.getRowCount(); i++) {
        Row = sheet.createRow(i);
        for (int j = 0; j < defautTableModel.getColumnCount(); j++) {
            Cell = Row.createCell(j);
            try {
                if (defautTableModel.getValueAt(i,j) != null){
                    Cell.setCellValue(defautTableModel.getValueAt(i,j).toString());
                    FileOutputStream fileOut = new FileOutputStream(Constant.Path_TestData_Output);
                    wb.write(fileOut);
                    fileOut.flush();
                    fileOut.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

You are only accessing the data columns and you need to get the header ones first.您只访问数据列,您需要先获取标题列。 You can acheive it by different means, either you use the method getColumnName as ThomasEdwin mentioned in his comment or use columnNames variable that you have provided to your model constructor: new DefaultTableModel(null,columnNames)您可以通过不同的方式实现它,您可以使用 ThomasEdwin 在他的评论中提到的方法getColumnName或使用您提供给模型构造函数的columnNames变量: new DefaultTableModel(null,columnNames)

Hope it helps希望它有帮助

Export Java Swing JTable header with JTable index to Excel将带有 JTable 索引的 Java Swing JTable 标头导出​​到 Excel

 private static void writeToExcell(DefaultTableModel TabR ,TableColumnModel tableM) throws IOException { try { JFileChooser fileChooser = new JFileChooser(); int retval = fileChooser.showSaveDialog(fileChooser); if (retval == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); if (file != null) { if (!file.getName().toLowerCase().endsWith(".xls")) { file = new File(file.getParentFile(), file.getName() + ".xls"); @SuppressWarnings("resource") Workbook wb = new HSSFWorkbook(); @SuppressWarnings("unused") CreationHelper createhelper = wb.getCreationHelper(); org.apache.poi.ss.usermodel.Sheet sheet = wb.createSheet(); org.apache.poi.ss.usermodel.Row row = null; org.apache.poi.ss.usermodel.Cell cell = null; for (int a=0;a<TabR.getRowCount();a++) { row = sheet.createRow(a+1); /* We create an Excel layer Row. We understand how many rows are in TabR with GetRowCount from TabR, the DefaultTableModel of the current JTable, and add one to it.*/ for (int b=0;b<tableM.getColumnCount();b++) { cell = row.createCell(b);/* With the GetColumnCount function of the existing JTable's TableColumnModel, we create more Column in the JTable.*/ cell.setCellValue(TabR.getValueAt(a, b).toString()); /*we give the value of the cell. */ } } for (int c=0;c<cell.getRowIndex();c++) { row = sheet.createRow(c); for (int d=0;d<tableM.getColumnCount();d++) { cell = row.createCell(d); cell.setCellValue(tableM.getColumn(d).getHeaderValue().toString()); } } FileOutputStream out = new FileOutputStream(file); wb.write(out); out.close(); } } } } catch (FileNotFoundException ex) { Logger.getLogger(B_anaEk.class.getName()).log(Level.ALL, null, ex); } catch (IOException ex) { Logger.getLogger(B_anaEk.class.getName()).log(Level.ALL, null, ex); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM