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/