Skip to content
Raphael Stoeckli edited this page Jan 17, 2016 · 1 revision

Creating a new workbook

Workbook workbook = new Workbook("test1.xlsx", "Sheet1");
This is the most common constructor. You can define the filename and the name of the first worksheet. This worksheet will also be the current worksheet after creating the workbook.

#Creating an additional worksheet workbook.addWorksheet("Sheet2");
The new created worksheet will be the current worksheet. To set another worksheet as current sheet, use workbook.setCurrentWorksheet("Sheet1");.

#Adding cells workbook.getCurrentWorksheet().addNextCell("Test"); or workbook.getCurrentWorksheet().addNextCell(22.589);
addNextCell() adds values to a cell and sets the "cursor" to the next column (default) or row.

workbook.getCurrentWorksheet().addCell("ABC", "A1"); or workbook.getCurrentWorksheet().addCell(true, 0, 4);
addCell() adds values to the passed cell address. The address can be a string like "A1" or the column and row number (zero-based). In this case 0 is "A" and 4 is "5" → "A5".

workbook.getCurrentWorksheet().addStringCellRange(values, "A4:C4"); (values is a List<> of 3 values like strings etc.)
addStringCellRange() adds a range of values to the passed cell range. The Range can be in the form "A4:C4" or as start address and end address.
There are several methods to add cell ranges (unfortunately, Java cannot handle ArrayLists with generic content as overloaded method):

  • addObjectCellRange() → general purpose
  • addStringCellRange() → for Strings
  • addIntegerCellRange() → for Integers (int32)
  • addDoubleCellRange() → for Doubles (64 bit)
  • addFloatCellRange() → for Floats (32 bit)
  • addDateCellRange() → for Dates
  • addBooleanCellRange() → for Boolean

If using addNextCell() you can jump to the next row or column with workbook.getCurrentWorksheet().goToNextRow(); or workbook.getCurrentWorksheet().goToNextColumn();
The direction for addNextCell() can be set by workbook.getCurrentWorksheet().setCurrentCellDirection(Worksheet.CellDirection.RowToRow); or workbook.getCurrentWorksheet().setCurrentCellDirection(Worksheet.CellDirection.ColumnToColumn);.

#Using styles [TBD]

#Saving the workbook workbook.save(); or workbook.saveAs("C:\\temp\\test2.xlsx");

Clone this wiki locally