简体   繁体   中英

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. 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 ! actually the entire workbook is turned into 0 KB after the delete button is pressed! 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"

     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. 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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM