gpt4 book ai didi

java - 使用 JXL (JExcel) 更新 Excel 工作表导致文件大小为 0 KB

转载 作者:太空宇宙 更新时间:2023-11-04 14:38:53 25 4
gpt4 key购买 nike

我在使用 JExcel“jxl”库写入现有 Excel 文件时遇到问题。我有一个包含工作表数据的 jTable,当用户按下删除按钮时,应从工作表和表模型中删除所选行。在下面的代码中,它从 jTable 模型中删除,但不删除从床单上!实际上,按下删除按钮后,整个工作簿都变成了 0 KB!你能告诉我出了什么问题吗?

“注意:我尝试将复制的工作簿的名称更改为另一个名称并删除了这一行 (sheet.removeRow(deletedrow+1); ),它很好地复制了原始工作簿。我认为问题发生在删除线”

     private void removebuttonActionPerformed(java.awt.event.ActionEvent evt) {                                             
int deletedrow;
deletedrow = logtable.getSelectedRow();
int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this record from "+ sheetname + " sheet?", "Confirmation Message", dialogButton);
if (dialogResult == 0) {

try {


Workbook workbook = Workbook.getWorkbook(new File("path/" + wbname + ".xls"));

WritableWorkbook copy = Workbook.createWorkbook(new File("path/" +wbname +".xls"), workbook);
WritableSheet sheet = copy.getSheet(sheetname);
sheet.removeRow(deletedrow + 1);

copy.write();
copy.close();

JOptionPane.showMessageDialog(this, "The record has been deleted from (" + sheetname + ") successfully", "Information Message", JOptionPane.INFORMATION_MESSAGE);

model.removeRow(deletedrow);
logtable.setModel(model);
}//try
catch (IOException | BiffException | WriteException | HeadlessException e) {
e.printStackTrace();

}
}//if confirmation message = yes

}

最佳答案

我也遇到了同样的问题,你说得对,问题出在removeRow 上。它非常适合最后一行,但无法删除中间行。

我所做的是将以下行复制回一行,并删除最后一行。这是代码:

for (int rowIdx = deletedrow; rowIdx < sheet.getRows(); rowIdx++) {
for (int colIdx = 0; colIdx < sheet.getColumns(); colIdx++) {
Cell readCell = sheet.getCell(colIdx, rowIdx+1);
Label label = new Label(colIdx, rowIdx, readCell.getContents());
CellFormat readFormat = readCell.getCellFormat();
if (readFormat != null) {

label.setCellFormat(readFormat);
}
sheet.addCell(label);
}

}
sheet.removeRow(sheet.getRows());

关于java - 使用 JXL (JExcel) 更新 Excel 工作表导致文件大小为 0 KB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25212885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com