Parse Excel Files in Java Easily – Read .XLS and .XLSX Files

How to Parse Excel Files Easily Using Java

Excel files are widely used to store and exchange structured data, such as reports, user-submitted forms, and exported records from other systems. In many Java applications, developers need to open these Excel files and extract the data for further processing.

In Java, parsing an Excel file usually means loading an .xls or .xlsx file, reading worksheets, and converting cell values into Java-friendly formats such as strings, numbers, or dates. This article shows how to parse Excel files in Java step by step using Spire.XLS for Java, with practical examples ranging from basic text reading to data type–aware parsing.

Table of Contents


Prepare the Environment

Before parsing Excel files, you need to add Spire.XLS for Java to your project. The library supports both .xls and .xlsx formats and does not require Microsoft Excel to be installed.

Add the Dependency

If you are using Maven, add the following dependency to your pom.xml:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls</artifactId>
        <version>16.1.3</version>
    </dependency>
</dependencies>

Once the dependency is added, you are ready to load and parse Excel files in Java.

If you are not using Maven, you can also download Spire.XLS for Java and add it to your project manually.


Load and Parse an Excel File in Java

The first step when parsing an Excel file is to load it into a Workbook object and access the worksheet you want to read.

import com.spire.xls.*;

public class ParseExcel {
    public static void main(String[] args) {
        Workbook workbook = new Workbook();
        workbook.loadFromFile("data.xlsx");

        Worksheet sheet = workbook.getWorksheets().get(0);
        System.out.println("Worksheet loaded: " + sheet.getName());
    }
}

Preview of the reading result:

Read Excel sheet names into Java

This code works for both .xls and .xlsx files. After loading the worksheet, you can start reading rows and cells.


Read Excel Data as Text (Basic Parsing)

In many cases, developers only need to read Excel data as text, without worrying about specific data types. This approach is simple and suitable for logging, displaying data, or quick imports.

Read All Cells as Strings

for (int i = 1; i <= sheet.getLastRow(); i++) {
    for (int j = 1; j <= sheet.getLastColumn(); j++) {
        String cellText = sheet.getCellRange(i, j).getValue();
        System.out.print(cellText + "\t");
    }
    System.out.println();
}

Preview of the text reading result:

Read Excel data as text in Java

Using getValue() returns the formatted value shown in Excel. This is often the easiest way to read data when precision or data type conversion is not critical.

If your requirement goes beyond reading and involves modifying or editing Excel files, you can refer to a separate guide that demonstrates how to edit Excel documents in Java using Spire.XLS.


Parse Excel Cells into Different Data Types

For data processing, validation, or calculations, reading everything as text is usually not enough. In these cases, you need to parse Excel cell values into proper Java data types.

Parse Numeric Values (int / double / float)

In Excel, many cells are stored internally as numeric values, even if they are displayed as dates, currencies, or percentages. Spire.XLS for Java allows you to read these cells directly using getNumberValue().

CellRange usedRange = sheet.getAllocatedRange();
System.out.println("Raw number values:");
for (int i = usedRange.getRow(); i <= usedRange.getLastRow(); i++) {
    for (int j = usedRange.getColumn(); j <= usedRange.getLastColumn(); j++) {
        CellRange cell = sheet.getRange().get(i, j);
        if (!(Double.isNaN(cell.getNumberValue())))
            {
                System.out.print(cell.getNumberValue() + "\t");
            }
        }
    System.out.println();
}

Below is a preview of the numeric reading result:

Read numeric values from Excel in Java

This method returns the underlying numeric value stored in the cell, regardless of the display format applied in Excel.

Convert Numeric Values Based on Application Logic

Once you have the numeric value, you can convert it to the appropriate Java type according to your application requirements.

double numberValue = cell.getNumberValue();

// Convert to int
int intValue = (int) numberValue;

// Convert to float
float floatValue = (float) numberValue;

// Keep as double
double doubleValue = numberValue;

For example, IDs, counters, or quantities are often converted to int, while prices, balances, or measurements are better handled as double or float.

Note: Excel dates are also stored as numeric values. If a cell represents a date or time, it is recommended to read it using date-related APIs instead of treating it as a plain number. This is covered in the next section.

Parse Date and Time Values

In Excel, date and time values are internally stored as numbers, while the display format determines how they appear in the worksheet. Spire.XLS for Java provides the getDateTimeValue() method to read these values directly as Date objects, allowing you to handle date and time data more conveniently in Java.

For example, if a column is designed to store date values, you can read all cells in that range as Date objects:

CellRange usedRange = sheet.getAllocatedRange();
System.out.println("Date values:");

for (int i = 0; i < usedRange.getRowCount(); i++) {
    // Read values from column F (for example, a date column)
    CellRange cell = usedRange.get(String.format("G%d", i + 1));
    java.util.Date date = cell.getDateTimeValue();
    System.out.println(date);
}

Preview of the date reading result from the seventh column:

Read date values from Excel in Java

This approach is widely used in real-world applications such as reports, data imports, or spreadsheets with predefined columns.

Because Excel dates are stored as numeric values, getDateTimeValue() converts the numeric value into a Date object and is typically applied to columns that represent date or time information.

Parse Mixed Cell Values in a Practical Way

In real-world Excel files, a single column may contain different kinds of values, such as text, numbers, dates, booleans, or empty cells. When parsing such data in Java, a practical approach is to read cell values using different APIs and select the most appropriate representation based on your business logic.

CellRange cell = sheet.getRange().get(2, 1); // B2

// Formatted text (what is displayed in Excel)
String text = cell.getText();

// Raw string value
String value = cell.getValue();

// Generic underlying value (number, boolean, date, etc.)
Object rawValue = cell.getValue2();

// Formula, if the cell contains one
String formula = cell.getFormula();

// Evaluated result of the formula
String evaluated = cell.getEnvalutedValue();

// Numeric value
double numberValue = cell.getNumberValue();

// Date value (commonly used for columns representing dates or times)
java.util.Date dateValue = cell.getDateTimeValue();

// Boolean value
boolean booleanValue = cell.getBooleanValue();

In practice, many applications use getText() as a safe fallback for display, logging, or export scenarios. For data processing, methods like getNumberValue(), getDateTimeValue(), or getBooleanValue() are typically applied based on the known meaning of each column.

This flexible approach works well for user-generated or loosely structured Excel files and helps avoid incorrect assumptions while keeping the parsing logic simple and robust.

If your primary goal is reading Excel files in Java—for example, extracting cell values for display or reporting—you may also want to refer to a separate guide that focuses specifically on Excel data reading scenarios in Java.


Common Parsing Scenarios in Real Applications

Parse Excel Rows into Java Objects

A common use case is mapping each row in an Excel sheet to a Java object, such as a DTO or entity class.

For example, one row can represent a product or a record, and each column maps to a field in the object. After parsing, you can store the objects in a list for further processing or database insertion.

Read Excel Data into Collections

Another typical scenario is reading Excel data into a List<List > or similar structure. This is useful for batch processing, validation, or data transformation pipelines.

 

Spire.XLS for Java makes it straightforward to iterate through rows and cells, allowing developers to extract numeric or date values based on column semantics.

Why Use Spire.XLS for Java to Parse Excel Files?

When parsing Excel files in Java, Spire.XLS for Java offers several advantages:

  • Supports both .xls and .xlsx formats
  • No dependency on Microsoft Excel
  • Simple APIs for reading text, numbers, and dates
  • Suitable for server-side and desktop Java applications

These features make it a practical choice for Excel parsing tasks in real-world projects.

Spire.XLS for Java also supports insert different types of data into Excel worksheets. Check out How to Write Data into Excel Files Using Java for more details.


Conclusion

Parsing Excel files in Java is a common requirement in many applications, whether you need to read simple text values or extract structured data with correct data types. By using Spire.XLS for Java, you can load Excel files, read cell values as text, and parse numbers or dates with minimal code.

Depending on your use case, you can choose between basic text-based parsing or type-aware data extraction. With the right approach, Excel data can be safely and efficiently integrated into your Java applications.

To access all features of Spire.XLS for Java without evaluation limitations, you can apply a free trial license.


FAQ

Can I parse both XLS and XLSX files in Java?

Yes. Spire.XLS for Java supports both .xls and .xlsx formats using the same API.

What is the difference between reading Excel as text and parsing data types?

Reading as text returns the formatted value shown in Excel, while parsing data types preserves numeric and date values for calculations and logic.

Do I need Microsoft Excel installed to parse Excel files?

No. Spire.XLS for Java works independently of Microsoft Excel.