[英]Updating excel sheet using JXL (JExcel) lead to 0 KB file
I have a problem in writing into an existing excel file using JExcel "jxl" library. 使用JExcel“ jxl”库写入现有的excel文件时遇到问题。 I have a jTable that contains data of a sheet, when the user presses the delete button, the selected row should be deleted from the sheet & also from the table model .. in the following code, it is deleted from the jTable model but not from the sheet !
我有一个jTable包含工作表的数据,当用户按下Delete按钮时,应该从工作表以及表模型中删除选定的行..在下面的代码中,将其从jTable模型中删除,但不删除从表! actually the entire workbook is turned into 0 KB after the delete button is pressed!
实际上,按下删除按钮后,整个工作簿都变成了0 KB! could you tell me what is problem?
你能告诉我什么问题吗?
"note: i tried changing the name of the copied workbook into another name and removed this line (sheet.removeRow(deletedrow+1); ) and it copies the original workbook just fine.. the problem I think occurs in the remove line" “注意:我尝试将复制的工作簿的名称更改为另一个名称,并删除了这一行(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
}
I encountered the same problem, and you're right, the problem is with the removeRow. 我遇到了同样的问题,对了,问题出在removeRow上。 It works perfectly for last lines, but can't remove middle lines.
它非常适合最后一行,但不能删除中间行。
What I did was that I copied the following rows one row back, and removed the last row. 我所做的是我将以下各行复制了回来,并删除了最后一行。 Here is the code:
这是代码:
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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.