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!
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.
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.
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 :)