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

标签 java excel swing 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,我如何设置要在 excel 列中设置的 imagePath 字符串来替换最后一个 jtable 列。在这种情况下,最后一列输出 jlabel 模型而不是输出图像路径。我有一种方法来提取图像路径,我想用新数据替换该列,该新数据是图像路径字符串并将其存储在“imagePath”变量中

最佳答案

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

            }

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

关于java - 将行导出到 Excel 时如何替换默认的 jtable 列值 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56995158/

相关文章:

list - 索引多列并匹配不同的值,返回跨列的唯一值列表

java - 更新不同类的 java swing 组件

swing - Java-读取复选框是否被选中

java - JTextPane 在文本显示后丢失样式

java - 在Spring应用程序中使用jpa实体管理器将blob导入mysql

excel - 更新Excel VBA字典中的值

java - 具有矩阵的对象的深度复制(克隆)(Java)

excel - 如何修改此代码以仅搜索可见的行和列

java - Android:使用 Lambda 间隔执行 runnable

java - Java中如何修改Html内容