将行导出到Excel时如何替换默认的Jtable列值-Java

时间:2019-07-11 18:13:23

标签: excel jtable apache-poi

            for (int i = 0; i < model.getRowCount(); i++) {
                XSSFRow newRow = sheet1.createRow(i);
                for (int j = 0; j < model.getColumnCount(); j++) {
                    XSSFCell excelCell = newRow.createCell((short) j);
                    if (j == model.getColumnCount() - 1) {
                        JLabel excelJL = (JLabel) model.getValueAt(i, j);
                        ImageIcon excelImageIcon = (ImageIcon) excelJL.getIcon();
                        String imagePath = excelImageIcon.getDescription();
                        //[i][j] = imagePath;
                    }

                        excelCell.setCellValue(model.getValueAt(i, j).toString());

                }
            }

是从jtable到excel导出数据,我该如何设置 imagePath 在excel列中设置的字符串,而不是最后一个jtable列。在这种情况下,最后一列输出jlabel模型,而不是输出图像路径。我有一种提取图像路径的方法,我想用新数据替换该列,它是图像路径字符串,它存储在“ imagePath”变量中

1 个答案:

答案 0 :(得分:0)

            //Loop through the jtable columns and rows to get its values
            for (int i = 0; i < model.getRowCount(); i++) {
                XSSFRow excelRow = excelSheet.createRow(i);
                for (int j = 0; j < model.getColumnCount(); j++) {
                    XSSFCell excelCell = excelRow.createCell(j);

                    //Change the image column to output image path
                    //Fourth column contains images
                    if (j == model.getColumnCount() - 1) {
                        JLabel excelJL = (JLabel) model.getValueAt(i, j);
                        ImageIcon excelImageIcon = (ImageIcon) excelJL.getIcon();
                        //Image Name Is Stored In ImageIcons Description First set it And Then Retrieve it.
                        excelImagePath = excelImageIcon.getDescription();
                    }

                    excelCell.setCellValue(model.getValueAt(i, j).toString());
                    if (excelCell.getColumnIndex() == model.getColumnCount() - 1) {
                        excelCell.setCellValue(excelImagePath);
                    }
                }

            }

正在更改最后一列。因此,此代码将所有列值更改为传递的参数的值。就我而言,我正在显示图像路径。