How to Work with Cells Core in Java
Aspose.Cells FOSS for Java builds Excel .xlsx workbooks in pure Java
through three core classes: Workbook (the file), Worksheet (a sheet inside
it), and Cells/Cell (the grid of values and their styling). This guide
walks through the complete core workflow — create a workbook, name a
worksheet, write values into cells, read them back, apply basic styling,
and save the result.
Step-by-Step Guide
Step 1: Install the Package
<dependency>
<groupId>org.aspose</groupId>
<artifactId>aspose-cells-foss</artifactId>
<version>26.7.0</version>
</dependency>Step 2: Import Required Classes
import org.aspose.cells_foss.Cell;
import org.aspose.cells_foss.Style;
import org.aspose.cells_foss.Workbook;
import org.aspose.cells_foss.Worksheet;Step 3: Create a Workbook and Name a Worksheet
Workbook implements the standard Java auto-closeable resource
contract, so a try-with-resources block
releases its internal buffers once the sheet is saved. Workbook.getWorksheets()
returns the sheet collection; a brand-new workbook already contains one
sheet at index 0, reached with WorksheetCollection.get(0):
try (Workbook workbook = new Workbook()) {
Worksheet sheet = workbook.getWorksheets().get(0);
sheet.setName("Report");
}Step 4: Write Values into Cells
Worksheet.getCells() returns the Cells collection for the sheet.
Cells.get(cellName) resolves an A1-style reference to a Cell, and
Cell.putValue(value) accepts a text string, number, or boolean:
sheet.getCells().get("A1").putValue("Revenue");
sheet.getCells().get("B1").putValue(12500.75);Step 5: Read Values Back
Cell.getValue() returns the raw stored value; Cell.getStringValue()
and Cell.getDisplayStringValue() return formatted text representations:
Cell revenueCell = sheet.getCells().get("B1");
System.out.println(revenueCell.getStringValue());Step 6: Apply Basic Cell Styling
Cell.getStyle() returns a copy of the cell’s current Style; modify it
through Style.getFont() and Style.setCustom(formatCode), then write it
back with Cell.setStyle(style):
Cell total = sheet.getCells().get("B1");
Style style = total.getStyle();
style.getFont().setBold(true);
style.setCustom("#,##0.00");
total.setStyle(style);
sheet.getCells().getRows().get(0).setHeight(22.0);
sheet.getCells().getColumns().get(1).setWidth(14.5);Step 7: Save the Workbook
Workbook.save(fileName) writes the current content to an .xlsx file:
workbook.save("report.xlsx");Common Issues and Fixes
Styling changes do not appear after calling setStyle(). Cell.getStyle()
returns a detached copy of the style, not a live reference. Always call
Cell.setStyle(style) again after modifying the returned object, as shown
in Step 6 — mutating the copy alone has no effect on the cell.
putValue() stores the wrong type. Cell.putValue(value) has several
overloads; passing a boxed number where a String was intended (or vice
versa) changes how Excel renders the cell. Check Cell.getType() after
loading a workbook if a value looks different than expected.
Row or column index is out of range. RowCollection.get(index) and
ColumnCollection.get(index) create the row/column metadata entry at that
index if it does not exist yet (rows and columns are sparse); passing a
negative index throws an exception. Confirm the index is zero-based and
non-negative.
Workbook.save() throws an exception mentioning “at least one worksheet”.
WorksheetCollection.removeAt(...) can remove the last remaining sheet.
A workbook must retain at least one worksheet before Workbook.save(...)
is called.
Changes are lost after the try-with-resources block exits. Call
Workbook.save(fileName) (or one of its stream overloads) before the
try-with-resources block closes — Workbook.close() releases internal
state and does not implicitly save.
Frequently Asked Questions
Does a new Workbook() start with any worksheets?
Yes. new Workbook() creates a workbook with one worksheet already present
at index 0 — there is no need to call WorksheetCollection.add() before
writing to the first sheet.
How do I add more worksheets?
Call WorksheetCollection.add() (adds a sheet with a default name) or
WorksheetCollection.add(sheetName) to choose the name directly; both
return the new sheet’s index.
How do I reference a cell by row/column numbers instead of an A1 string?
Cells.get(row, column) accepts zero-based row and column integers and
returns the same Cell as Cells.get(cellName).
What is the difference between getValue() and getStringValue()?
Cell.getValue() returns the underlying typed value as a plain Java
object (a number, text, or boolean). Cell.getStringValue() returns the raw
text form, while Cell.getDisplayStringValue() applies the cell’s
number format before returning text — use it when the formatted
on-screen representation matters.