简体   繁体   English

将 JTable 导出为 Excel 文件

[英]Export JTable into Excel file

here I am trying to export JTable into excel file...I didn't get any error on console...but on excel sheet I got only column names...my aim is want to show database table in this JTable square, and below this there is Export button,so after clicking this button excel file should be created for above displaying JTable.在这里,我试图将 JTable 导出到 excel 文件中……我在控制台上没有收到任何错误……但是在 excel 表上我只有列名……我的目标是要在这个 JTable 方块中显示数据库表,在此下方有导出按钮,因此单击此按钮后,应为上面显示 JTable 创建 excel 文件。

so cant recognized actual error所以无法识别实际错误

    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();
            }

So your problem is right here...所以你的问题就在这里......

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

DBUtils is reading to the end of the ResultSet , meaning when you call rs.next() , it returns false , because there is no more data DBUtils正在读取ResultSet的末尾,这意味着当您调用rs.next() ,它返回false ,因为没有更多数据

You have two chocies...你有两个选择...

You Could...你可以...

Call ResultSet#beforeFirst , but this is not supported by all database and there may be a performance hit调用ResultSet#beforeFirst ,但这不是所有数据库都支持的,可能会影响性能

You Could...你可以...

Stop using DBUtils and build the TableModel manually.停止使用DBUtils并手动构建TableModel Start by having a look at How to Use Tables for more details首先查看如何使用表格以获取更多详细信息

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);
}

I'd also recommend having a look at JDBC Database Access and The try-with-resources Statement我还建议查看JDBC Database AccessThe try-with-resources Statement

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

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