apache poi: saving jtable to a file

后端 未结 2 1385
遇见更好的自我
遇见更好的自我 2021-01-18 13:58

I\'ve recently started working with java and I\'m facing some issues with the apache poi library when I need to create an excel file starting from a jTable.

I\'ve re

相关标签:
2条回答
  • 2021-01-18 14:24

    I know that probably this is late for the post, but maybe for others that want to have the code, here is something that works.

        TableModel model = raportTable.getModel();
    
        SXSSFWorkbook wb = new SXSSFWorkbook(-1);
        SXSSFSheet sh = wb.createSheet("Report");
        Row row = sh.createRow(0);
        for (int i = 0; i < model.getColumnCount(); i++) {
            Cell cell = row.createCell(i);
            cell.setCellValue(model.getColumnName(i));
        }
    
        for(int i = 0; i < model.getRowCount(); i++) {
            row = sh.createRow(i + 1);
            for(int j = 0; j < model.getColumnCount(); j++){
                Cell cell = row.createCell(j);
                cell.setCellValue(model.getValueAt(i,j).toString());
            }
        }
    
        try {
            FileOutputStream excel = new FileOutputStream(filename + ".xlsx");
            wb.write(excel);
            excel.flush();
            excel.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    

    Hope it helps.

    0 讨论(0)
  • 2021-01-18 14:27

    Please read the tutorial about JTable, Creating a Table Model and implemented data types, JTable knows those Column Classes, and returns proper value for output to POI; for most of types in the MS Excel:

    1. you have to loop inside cells in current row, too

    2. have to format output for various data types (Date, Double, String)

    3. then you can focus on creating formula(s) and coloring cell(s)

    Code to loop through TableModel can generate MS Excel File, too, with standard windows delimiters:

    public class ExcelCustomerReport {
    
        public ExcelCustomerReport() {
        }
    
        public void exportTable(JTable table, File file) throws IOException {
            TableModel model = table.getModel();
            FileWriter out = new FileWriter(file);
            String groupExport = "";
            for (int i = 0; i < (model.getColumnCount()); i++) {//* disable export from TableHeaders
                groupExport = String.valueOf(model.getColumnName(i));
                out.write(String.valueOf(groupExport) + "\t");
            }
            out.write("\n");
            for (int i = 0; i < model.getRowCount(); i++) {
                for (int j = 0; j < (model.getColumnCount()); j++) {
                    if (model.getValueAt(i, j) == null) {
                        out.write("null" + "\t");
                    } else {
                        groupExport = String.valueOf(model.getValueAt(i, j));
                        out.write(String.valueOf(groupExport) + "\t");
                    }
                }
                out.write("\n");
            }
            out.close();
        }
    }
    
    0 讨论(0)
提交回复
热议问题