How to Edit/Modify an existing Excel file in Java with Jexcel API

前端 未结 2 838
一生所求
一生所求 2021-01-16 16:14

I want to edit an existing Excel file with Java, to add some more data to an existing template excel file. So i used Jexcel for this purpose.

As suggested everywhere

相关标签:
2条回答
  • 2021-01-16 16:58

    I ran into this same issue and to solve the problem I updated to the latest version of jxl.jar via maven. After doing this there was a very long delay when it ran destinationWorkbook = Workbook.createWorkbook(outputFile, sourceWorkbook); but it completed successfully without errors.

    0 讨论(0)
  • 2021-01-16 17:00

    Figured out the problem.

    We have to close the input file before writing back (editing) the same file.

    so to edit an existing Excel file with Jexcel

    File inp = new File("H://"+file_name);
    File out = new File("H://"+file_name);
    Workbook existingWorkbook = Workbook.getWorkbook(inp);// This opens up a read-only copy of the workbook
    WritableWorkbook copy = Workbook.createWorkbook(out,existingWorkbook); // This opens up a writable workbook so that we can edit the copy
    //..........Some writes to excel workbook...........
    // Now before writing & closing the copy, first close the existing one
    existingWorkbook.close();    // Important: Close it before writing the copy with copy.write();
    inp.close();
    copy.write();
    copy.close();
    
    0 讨论(0)
提交回复
热议问题