How to Load a Spreadsheet in Java

Overview

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

RequirementDetail
Java17 or later (JDK)
Build toolMaven 3.9+ or Gradle 8+
Dependencyorg.aspose:aspose-cells-foss:26.7.0
Input fileA valid .xlsx workbook

Add to pom.xml:

<dependency>
  <groupId>org.aspose</groupId>
  <artifactId>aspose-cells-foss</artifactId>
  <version>26.7.0</version>
</dependency>

Step 1 — Load from a File Path

Pass the absolute or relative file path to the Workbook(fileName) constructor:

import org.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. A missing file raises WorkbookLoadException; a file that is not a valid XLSX package raises InvalidFileFormatException. Both extend CellsException.


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 org.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 org.aspose.cells_foss.Workbook;
import org.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 org.aspose.cells_foss.WorksheetCollection;
import org.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 org.aspose.cells_foss.Cells;
import org.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:

import org.aspose.cells_foss.Workbook;

try (Workbook workbook = new Workbook("data/large.xlsx")) {
    // read or modify the workbook
    workbook.save("data/output.xlsx");
}
// workbook.close() is called automatically here

Next Steps

See Also

 English