我在使用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
}
答案 0 :(得分:0)
我遇到了同样的问题,你是对的, 问题在于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());