Spreadsheet Management with Aspose.Cells FOSS for Java
Aspose.Cells FOSS for Java is a pure-Java 17, MIT-licensed library for creating
and manipulating Excel .xlsx workbooks without any Microsoft Office dependency.
This post walks through the most common spreadsheet management operations: creating
workbooks, loading files, working with cells, and saving results.
Setting Up
Add the Maven dependency to your pom.xml:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells-foss</artifactId>
<version>1.0.0</version>
</dependency>
No license file or API key is required. The library is fully open-source under MIT.
Creating a Workbook
The Workbook() constructor creates a new, blank workbook with one default sheet:
import com.aspose.cells.foss.Workbook;
import com.aspose.cells.foss.Worksheet;
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);
System.out.println(sheet.getName()); // Sheet1
Use WorksheetCollection.add(sheetName) to add named sheets and
removeAt(sheetName) to remove them:
int idx = workbook.getWorksheets().add("Report");
workbook.getWorksheets().get(idx).setName("Report");
workbook.getWorksheets().removeAt("Sheet1");
Loading an Existing File
Pass a file path to Workbook(fileName) to open an existing .xlsx workbook:
Workbook workbook = new Workbook("data/report.xlsx");
System.out.println("Sheets: " + workbook.getWorksheets().getCount());
For input streams (e.g. from a database or HTTP response), use Workbook(stream):
try (java.io.InputStream is = new java.io.FileInputStream("data/report.xlsx")) {
Workbook workbook = new Workbook(is);
}
Reading and Writing Cells
Worksheet.getCells() returns the Cells collection. Access individual cells by
name or by zero-based row/column index:
import com.aspose.cells.foss.Cells;
import com.aspose.cells.foss.Cell;
Cells cells = workbook.getWorksheets().get(0).getCells();
// Write by cell name
cells.get("A1").putValue("Product");
cells.get("B1").putValue("Revenue");
// Write by row/column index
cells.get(1, 0).putValue("Widget A"); // A2
cells.get(1, 1).putValue(45000); // B2
// Read back
String name = cells.get("A2").getStringValue();
Object rev = cells.get("B2").getValue();
System.out.println(name + ": " + rev);
Setting Formulas
Assign a formula string via Cell.setFormula():
cells.get("B3").setFormula("=SUM(B2:B2)");
System.out.println(cells.get("B3").getFormula()); // =SUM(B2:B2)
Formula results are resolved when the workbook is saved and reloaded.
Saving the Workbook
Workbook.save(fileName) writes the workbook; the format is determined by the
file extension (.xlsx produces an Excel 2007+ file):
workbook.save("output/result.xlsx");
Always wrap large workbooks in try-with-resources to release file handles promptly:
try (Workbook workbook = new Workbook("data/input.xlsx")) {
workbook.getWorksheets().get(0).getCells().get("A1").putValue("Updated");
workbook.save("data/output.xlsx");
}
What to Explore Next
Aspose.Cells FOSS for Java supports a broad spreadsheet API beyond basic cell manipulation:
- Worksheet protection:
protect()/unprotect()onWorksheet - AutoFilter:
Worksheet.getAutoFilter()for programmatic data filtering - Cell styles:
Cell.getStyle()/setStyle()withStyleproperties - Merged cells:
Cells.merge(firstRow, firstColumn, totalRows, totalColumns)
For a full API walkthrough, see the developer guide and the API reference.