java - 编辑将 JTable 写入 Excel

标签 java excel eclipse swing jtable

我正在尝试将我的 JTable 导出到 Excel 文件。列名和行名都很好,但是我添加到 JTable 中的所有信息都没有被写入。我尝试了 System.out.println() 并在除列名和行名之外的任何地方打印 Null 值。我试图从 mother google 那里得到答案,但经过 2 小时的阅读和尝试,仍然没有任何进展。 留在我脑海中的是,在写入 Excel 部分的代码中可能存在一些错误,或者添加到 JTable 的所有内容都只是我显示器上的图片,而不是其中的实际数据。? 如果我错了,请纠正我,非常感谢任何帮助。

这里是写入Excel部分。 在第一个 For 循环中,我得到了标题,在第二个 For 循环中,我应该得到 JTable 中的所有内容,但我没有。

TableColumnModel tcm = nrp.rotaTable.getColumnModel();

    String nameOfFile = JOptionPane.showInputDialog("Name of the file");

    Workbook wb = new HSSFWorkbook();
    CreationHelper createhelper = wb.getCreationHelper();

    Sheet sheet = wb.createSheet("new sheet");
    Row row = null;
    Cell cell = null;

    for (int i = 0; i < nrp.tableModel.getRowCount(); i++) {
        row = sheet.createRow(i);
        for (int j = 0; j < tcm.getColumnCount(); j++) {

            cell = row.createCell(j);
            cell.setCellValue(tcm.getColumn(j).getHeaderValue().toString());

        }
    }
    for (int i = 1; i < nrp.tableModel.getRowCount(); i++) {
        row = sheet.createRow(i);
        System.out.println("");
        for (int j = 0; j < nrp.tableModel.getColumnCount(); j++) {

            cell = row.createCell(j);
            cell.setCellValue((String) nrp.tableModel.getValueAt(i, j)+" ");
            System.out.print((String) nrp.tableModel.getValueAt(i, j)+" ");
        }
    }


    File file = new File("Some name.xls");
    FileOutputStream out = new FileOutputStream(file);
    wb.write(out);
    out.close();
    wb.close();
  }
}

这是 FocusListener 代码。

rotaTable.addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent e) {
            }
            public void focusLost(FocusEvent e) {
                CellEditor cellEditor = rotaTable.getCellEditor();
                if (cellEditor != null)
                    if (cellEditor.getCellEditorValue() != null)
                        cellEditor.stopCellEditing();
                    else
                        cellEditor.cancelCellEditing();
            }
        });

我正在使用“DefaultTableModel”

 DefaultTableModel tableModel = new DefaultTableModel(12,8); 
JTable rotaTable = new JTable(tableModel); 

这是我第一次使用 POI 库。

我的 JTable 图片 http://imgur.com/a/jnB8j

控制台打印结果图片http://imgur.com/a/jnB8j

创建的 Excel 文件的图片。 http://imgur.com/a/jnB8j

最佳答案

你必须从 0 开始行索引,因为表行从 0 开始,从 1 创建 excel 行,因为首先你要写列名。我修改了你的第二个 for 循环,如下所示:

for (int i= 0; i < nrp.tableModel.getRowCount(); i++) {
    row = sheet.createRow(i+1);
    System.out.println("");
    for (int j = 0; j < nrp.tableModel.getColumnCount(); j++) {
        cell = row.createCell(j);
        if(nrp.tableModel.getValueAt(i, j)!=null){
        cell.setCellValue((String) nrp.tableModel.getValueAt(i, j));
        }
        System.out.print((String) nrp.tableModel.getValueAt(i, j)+" ");
    }
}

关于java - 编辑将 JTable 写入 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43566772/

相关文章:

java - 如何从 BufferedImage 的 java 中的 R、G、B 值获取 rgb 像素值

java - Excel 在生成的 CSV 文件中未正确显示欧元符号

java - Hibernate通过java.util设置多对一关系

r - 通过 R 保持保护(或重新保护)导入的 Excel 工作簿的某些列

java - eclipse是否每个项目都使用proguard?

java - 现有的Maven项目可以添加SpringBoot框架吗

java - 如何检查手机日期是否为当前日期

java - Spring5 hibernate模板的findByNamedQueryAndNamedParam方法的替代

java - 如何使用自上而下的方法构建 REST Web 服务?

Excel 公式检查单元格内容