将JTable导出到Excel文件中

时间:2017-04-12 07:11:21

标签: java excel poi-hssf hssf

这里我试图将JTable导出到excel文件中...我没有在控制台上得到任何错误...但是在excel表上我只得到了列名...我的目标是想在这里显示数据库表JTable square,在这下面有Export按钮,所以点击这个按钮后,应该为上面显示的JTable创建excel文件。

因此无法识别实际错误

    JButton btnExport = new JButton("Export");
    btnExport.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent arg0)
        {
            try 
            {
                String query="Select * from client";
                PreparedStatement pst=conn.prepareStatement(query);
                ResultSet rs=pst.executeQuery();
                table.setModel(DbUtils.resultSetToTableModel(rs));

                HSSFWorkbook wb = new HSSFWorkbook();
                HSSFSheet sheet = wb.createSheet("Excel Sheet");
                HSSFRow rowhead = sheet.createRow(0);
                rowhead.createCell(0).setCellValue("Client_Vendor code");
                rowhead.createCell(1).setCellValue("Client_Name");
                rowhead.createCell(2).setCellValue("Purchaser_Name");
                rowhead.createCell(3).setCellValue("User_Name");
                rowhead.createCell(4).setCellValue("Sales_Engg");

                int index=1;
                while(rs.next())
                    {
                    HSSFRow row = sheet.createRow(index);
                    row.createCell(0).setCellValue(rs.getInt(1));
                    row.createCell(1).setCellValue(rs.getString(2));
                    row.createCell(2).setCellValue(rs.getString(3));
                    row.createCell(3).setCellValue(rs.getString(4));
                    row.createCell(4).setCellValue(rs.getString(5));
                    index++;

                    }  

                    FileOutputStream fileOut = new FileOutputStream("e:/CLIENTDATA/client.xlsx");
                    wb.write(fileOut);
                    fileOut.close();
                    System.out.println("Data is saved in excel file.");
                 }
                catch (Exception e) 
            {
                e.printStackTrace();
            }

1 个答案:

答案 0 :(得分:2)

所以你的问题就在这里......

table.setModel(DbUtils.resultSetToTableModel(rs));

DBUtils正在阅读ResultSet的末尾,这意味着当您致电rs.next()时,它会返回false,因为没有更多数据

你有两个chocies ......

你可以......

调用ResultSet#beforeFirst,但所有数据库都支持此功能,并且可能会遇到性能损失

你可以......

停止使用DBUtils并手动构建TableModel。首先查看How to Use Tables了解更多详情

ResultSet rs = pst.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Excel Sheet");
HSSFRow rowhead = sheet.createRow(0);

DefaultTableModel model = new DefaultTableModel();
for (int col = 0; col < columnCount; col++) {
    String columnName = rsmd.getColumnName(col + 1);
    model.addColumn(columnName);
    rowhead.createCell(col).setCellValue(columnName);
}

int index = 1;
while (rs.next()) {
    Vector tableRow = new Vector(columnCount);
    HSSFRow row = sheet.createRow(index);
    for (int col = 0; col < columnCount; col++) {
        Object value = rs.getObject(col + 1);
        if (value instanceof Integer) {
            row.createCell(col).setCellValue((int) value);
            tableRow.add((int) value);
        } else {
            row.createCell(col).setCellValue(value.toString());
            tableRow.add(value.toString());
        }
    }
    model.addRow(tableRow);
}

try (FileOutputStream fileOut = new FileOutputStream("e:/CLIENTDATA/client.xlsx")) {
    wb.write(fileOut);
}

我还建议您查看JDBC Database AccessThe try-with-resources Statement