Everything I have seen on this issue has been in a main method, however, I am working on a larger system and require a class that can create an Excel workbook, sheets and then write into a cell by the given sheet, row, column and dataToWrite. When the createWorkbook() function runs, however, it creates an empty text document. I suspect it has something to do with the workbook.write() use, however, the documentation is limited and kind of confusing.
//Constructor
public ExcelWriter(String outputFile) {
this.outputFile=outputFile;
try {
workbook=Workbook.createWorkbook(new File(outputFile));
workbook.write();
}
catch(IOException e) {
System.err.print(e.getMessage());
}
sheets=new ArrayList<WritableSheet>();
sheetCount=0;
}
public void createSheet(String name) {
WritableSheet toAdd=workbook.createSheet(name, sheetCount);
sheets.add(toAdd);
try {
workbook.write();
}
catch(IOException e) {
System.err.print(e.getMessage());
}
sheetCount++;
}
public void write(int sheet, int row, int col, String data) {
WritableSheet currentSheet=sheets.get(sheet);
Label label = new Label(row, col, data);
try {
currentSheet.addCell(label);
workbook.write();
}
catch(WriteException | IOException e) {
System.err.print(e.getMessage());
}
}