How to Read Excel Files in Java with Free Spire.XLS – Step-by-Step Guide
This tutorial explains how to automate Excel data extraction in Java using the free Free Spire.XLS library, covering installation, core classes and methods, and detailed code examples for reading a single cell, a cell range, and an entire worksheet, enabling efficient batch processing and integration with other systems.
Reading Excel data programmatically enables automation of data import, batch processing, comparison, and updates, improving efficiency and reducing manual errors; the extracted data can also be integrated with other systems.
Read a specific cell
Read a range of cells
Read an entire worksheet
Install the free Java library
The free Java library for handling Excel is Free Spire.XLS for Java . It limits .xls files to 5 worksheets and 200 rows per sheet, but imposes no limits on .xlsx files.
You can download the package manually and add the JAR files, or install it via Maven.
Download link: https://www.e-iceblue.cn/Downloads/Free-Spire-XLS-JAVA.html
Core classes and methods for reading Excel data
The following interfaces from Free Spire.XLS for Java are used to read cells, ranges, or worksheets:
Workbook and Worksheet classes represent an Excel workbook and its sheets.
CellRange class represents a specific cell or a cell range.
Worksheet.getCellRange(String name) – obtains a CellRange for a given cell or range.
Worksheet.getAllocatedRange() – obtains the data‑filled range of a worksheet.
CellRange.getValue() – returns the value or text of a cell (formulas are returned as the formula string).
Example 1: Read a single cell
<code>import com.spire.xls.CellRange;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class ReadData {
public static void main(String[] args) {
// Create Workbook object
Workbook wb = new Workbook();
// Load Excel file
wb.loadFromFile("货物.xlsx");
// Get the first worksheet
Worksheet sheet = wb.getWorksheets().get(0);
// Get the specified cell
CellRange cell = sheet.getCellRange("A9");
// Print cell value
System.out.print("A9 cell value is " + cell.getValue());
}
}
</code>Result of reading cell A9:
Example 2: Read a range of cells
<code>import com.spire.xls.CellRange;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class ReadData {
public static void main(String[] args) {
Workbook wb = new Workbook();
wb.loadFromFile("货物.xlsx");
Worksheet sheet = wb.getWorksheets().get(0);
CellRange range = sheet.getCellRange("A11:F15");
for (int i = 0; i < range.getRows().length; i++) {
for (int j = 0; j < range.getColumnCount(); j++) {
System.out.print(range.get(i + 11, j + 1).getValue() + " ");
}
System.out.println();
}
}
}
</code>Result of reading the cell range:
Example 3: Read an entire worksheet
<code>import com.spire.xls.CellRange;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class ReadData {
public static void main(String[] args) {
Workbook wb = new Workbook();
wb.loadFromFile("货物.xlsx");
Worksheet sheet = wb.getWorksheets().get(0);
CellRange locatedRange = sheet.getAllocatedRange();
for (int i = 0; i < locatedRange.getRows().length; i++) {
for (int j = 0; j < locatedRange.getColumnCount(); j++) {
System.out.print(locatedRange.get(i + 1, j + 1).getValue() + " ");
}
System.out.println();
}
}
}
</code>Result of reading the worksheet data:
These examples demonstrate how Java can be used to read Excel files for batch processing. The free Free Spire.XLS for Java library also supports generating, converting, editing, and printing Excel files; further details are available in its Chinese tutorial.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.