JExcel - Modify Multiple Cells Before Closing Writ

2019-05-22 23:34发布

问题:

I am trying to put a title in cell (0,0) and then use a for() loop to add file paths into each following row. However, I am only able to add one line of code; after the title, no other cells in the Excel document are modified.

Here's where I create the file:

private void chooseSheetActionPerformed(java.awt.event.ActionEvent evt) {                                            
        sheetNum = chooseSheet.getSelectedIndex() - 1;
        try {
            WritableWorkbook workbook = Workbook.createWorkbook(newXls);
            workbook.createSheet("Sheet1", 0);
            workbook.write();
            workbook.close();

            writeXls();
        } catch (Exception ex) {
            Logger.getLogger(NewScore.class.getName()).log(Level.SEVERE, null, ex);
        }
    }  

And here's where I try to write to it:

public void writeXls() throws Exception {
    Workbook wb = Workbook.getWorkbook(newXls);
    WritableWorkbook copy = Workbook.createWorkbook(newXls, wb);
    WritableSheet ws = copy.getSheet(sheetNum);
    WritableCell cell;

    // Body Part - Title
    Label lab = new Label(0,0,partName);
    cell = (WritableCell) lab;
    ws.addCell(cell);
    copy.write();
    // Image Info
    int i = 1;
    for (File file : imageArray(dir)) {
        Label label = new Label (0,i,"test" + i);
        cell = label;
        ws.addCell(cell);
        copy.write();
        i++;
    }
    copy.close();
}

Is there a way to make my for() loop work, or do I need to go about this a different way?

Thanks!

回答1:

Okay. Initially I was confused wether you are trying to create a new workbook or edit an existing one. But it looks like you need a new one here.

It appears there were two issues in the code example. First one is that you retrieved just created excel file and copied its content into itself.

Workbook wb = Workbook.getWorkbook(newXls);
WritableWorkbook copy = Workbook.createWorkbook(newXls, wb);

Another thing that I noticed is that in order to save all the changes it is required to call write on the WritableWorkbook instance only once in the end.

I finished with such code.

private static final String FILE_NAME = "D:/test_out.xls";
private static final String SHEET_NAME = "Test sheet name";
private static final int SHEET_INDEX = 0;
private static final String HEADER = "My header";

public static void main(String[] args) throws Exception {
    WritableWorkbook writableWorkbook = Workbook.createWorkbook(new File(FILE_NAME));
    WritableSheet writableSheet = writableWorkbook.createSheet(SHEET_NAME, SHEET_INDEX);

    int columnIndex = 0;
    int rowIndex = 0;

    writableSheet.addCell(new Label(columnIndex, rowIndex, HEADER));

    for (String value : Arrays.asList("First value", "Second value", "Another value")) {
        writableSheet.addCell(new Label (columnIndex, ++rowIndex, value));
    }

    writableWorkbook.write();
    writableWorkbook.close();
}

It created for me an excel file with one sheet. The sheet contains one column with 4 cells: My header, First value, Second value, Another value. Of course, you are free to put there any values you need :)