How to Load a Spreadsheet in Java
How to Load a Spreadsheet in Java
Aspose.Cells FOSS for Java lets you open existing Excel .xlsx workbooks from a
file path or from any java.io.InputStream. This guide shows both loading patterns
and explains how to access worksheets and cells after the workbook is loaded.
Prerequisites
| Requirement | Detail |
|---|---|
| Java | 17 or later (JDK) |
| Build tool | Maven 3.9+ or Gradle 8+ |
| Dependency | com.aspose:aspose-cells-foss:1.0.0 |
| Input file | A valid .xlsx workbook |
Add to pom.xml:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells-foss</artifactId>
<version>1.0.0</version>
</dependency>Step 1 — Load from a File Path
Pass the absolute or relative file path to the Workbook(fileName) constructor:
import com.aspose.cells.foss.Workbook;
Workbook workbook = new Workbook("data/report.xlsx");
System.out.println("Loaded. Sheets: " + workbook.getWorksheets().getCount());The constructor reads the file and deserializes the XLSX content into memory. If
the file does not exist or is not a valid XLSX, a WorkbookLoadException is thrown.
Step 2 — Load from a Stream
Use Workbook(stream) when the file content comes from a network connection, a
database BLOB, or an in-memory buffer:
import com.aspose.cells.foss.Workbook;
import java.io.FileInputStream;
import java.io.InputStream;
try (InputStream is = new FileInputStream("data/report.xlsx")) {
Workbook workbook = new Workbook(is);
System.out.println("Loaded from stream. Sheets: " + workbook.getWorksheets().getCount());
}Always wrap the InputStream in a try-with-resources block so the stream is closed
even if the constructor throws.
Step 3 — Load with Options
Use Workbook(fileName, options) or Workbook(stream, options) to pass a
LoadOptions instance when you need fine-grained control over the load behaviour:
import com.aspose.cells.foss.Workbook;
import com.aspose.cells.foss.LoadOptions;
LoadOptions opts = new LoadOptions();
// configure opts here if needed
Workbook workbook = new Workbook("data/report.xlsx", opts);Step 4 — Access Worksheets
After loading, use Workbook.getWorksheets() to iterate sheets or retrieve a
specific sheet by index or by name:
import com.aspose.cells.foss.WorksheetCollection;
import com.aspose.cells.foss.Worksheet;
WorksheetCollection sheets = workbook.getWorksheets();
System.out.println("Sheet count: " + sheets.getCount());
// By index
Worksheet first = sheets.get(0);
System.out.println("First sheet: " + first.getName());
// By name
Worksheet named = sheets.get("Summary");Step 5 — Read Cell Values
Once you have a Worksheet, use getCells() to access individual cells:
import com.aspose.cells.foss.Cells;
import com.aspose.cells.foss.Cell;
Cells cells = first.getCells();
// Read by cell name
Cell a1 = cells.get("A1");
System.out.println("A1 value: " + a1.getStringValue());
// Read by row/column index (0-based)
Cell b2 = cells.get(1, 1);
System.out.println("B2 value: " + b2.getValue());Step 6 — Release Resources
Workbook implements AutoCloseable. For large files, use try-with-resources:
try (Workbook workbook = new Workbook("data/large.xlsx")) {
// read or modify the workbook
workbook.save("data/output.xlsx");
}
// workbook.close() is called automatically hereNext Steps
- Working with Spreadsheet Management — full create/modify/save walkthrough
- How to Create an Aspose.Cells FOSS Workbook — create workbooks from scratch
- How to Work with Validation — add data validation rules