
CSV (Comma-Separated Values) remains a universal format for data exchange due to its simplicity, readability, and wide compatibility across platforms. If you're looking for a robust and efficient method to read CSV in Java, the Spire.XLS for Java library offers a powerful and straightforward solution.
This guide will walk you through how to use Java to load and read CSV files, as well as convert them into structured DataTables for seamless data manipulation and analysis in your applications.
- Why Choose Spire.XLS for Java to Parse CSV Files?
- Step-by-Step: Read a CSV File in Java
- Advanced: Read CSV into DataTable in Java
- Frequently Asked Questions
- Conclusion
Why Choose Spire.XLS for Java to Parse CSV Files?
Compared with other CSV parser in Java, Spire.XLS offers several advantages for CSV processing:
- Simplified API for reading CSV files
- Support for custom delimiters (not just commas)
- Built-in range detection to avoid empty rows/columns
- Natively converts CSV data to DataTable
- Seamlessly switch between CSV, XLS, and XLSX formats
Step-by-Step: Read a CSV File in Java
Spire.XLS for Java provides the Workbook class to load CSV files and the Worksheet class to access data. Below are the steps to read CSV files line by line with automatic delimiter detection:
1. Setup and Dependencies
First, ensure you have Spire.XLS for Java included in your project. You can add it via Maven by including the following dependency:
<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>15.10.5</version>
</dependency>
</dependencies>
2. Load the CSV File
Spire.XLS for Java loads CSV files into a Workbook object, where each CSV row becomes a worksheet row.
import com.spire.xls.*;
public class ReadCSV {
public static void main(String[] args) {
// Create Workbook instance
Workbook workbook = new Workbook();
// Load CSV file (specify delimiter)
workbook.loadFromFile("sample.csv", ",", 1, 1);
}
}
Parameters:
The loadFromFile() method accepts four parameters:
- "sample.csv": The input CSV file path.
- ", ": Custom delimiter (e.g."," ";" or "\t").
- 1: Start row index.
- 1: Start column index.
3. Access Worksheet & Read CSV Data
Spire.XLS treats CSV files as single-worksheet workbooks, so we access the first worksheet and then iterate through rows/columns:
// Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
// Get the used range (avoids iterating over empty rows/columns)
CellRange dataRange = sheet.getAllocatedRange();
//Iterate through the rows
for (int i = 0; i < dataRange.getRowCount(); i++) {
//Iterate through the columns
for (int j = 0; j < dataRange.getColumnCount(); j++) {
// Get cell text
CellRange cell = dataRange.get(i+1,j+1);
System.out.print(cell.getText() + "\t"); // Use tab to separate columns
}
System.out.println(); // New line per row
Output: Read data from a CSV file and print out with tab separation for readability.

Advanced: Read CSV into DataTable in Java
For structured data manipulation, converting CSV to a DataTable is invaluable. A DataTable organizes data into rows and columns, making it easy to query, filter, or integrate with databases.
Java code to read a CSV file and export to a DataTable:
import com.spire.xls.*;
import com.spire.xls.data.table.DataTable;
public class CSVtoDataTable {
public static void main(String[] args) {
// Create a workbook and load a csv file
Workbook workbook = new Workbook();
workbook.loadFromFile("sample.csv", ",", 1, 1);
// Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
// Export to DataTable
DataTable dataTable = sheet.exportDataTable();
// Get row and column count
System.out.println("Total columns: " + dataTable.getColumns().size());
System.out.println("Total rows: " + dataTable.getRows().size());
System.out.println();
// Print column names
for (int i = 0; i < dataTable.getColumns().size(); i++) {
System.out.print(dataTable.getColumns().get(i).getColumnName() + " | ");
}
System.out.println();
System.out.println("----------------------------------------------------------");
// Print rows
for (int i = 0; i < dataTable.getRows().size(); i++) {
for (int j = 0; j < dataTable.getColumns().size(); j++) {
System.out.print(dataTable.getRows().get(i).getString(j) + "\t");
}
System.out.println();
}
}
}
Key Explanations:
- exportDataTable(): convert CSV data into a DataTable directly, no manual row/column mapping required.
- DataTable Benefits: Easily access basic information such as column count, row count, column names, and row data etc.
Output:

You may also read: Convert CSV to Excel in Java
Frequently Asked Questions
Q1: How do I handle CSV files with different delimiters (semicolon, tab, etc.)?
A: Specify the delimiter in the loadFromFile() method:
// For semicolon-delimited files
workbook.loadFromFile("sample.csv", ";", 0, 0);
// For tab-delimited files
workbook.loadFromFile("sample.csv", "\t", 0, 0);
// For pipe-delimited files
workbook.loadFromFile("sample.csv", "|", 0, 0);
Q2: How do I skip header rows in a CSV file?
A: You can skip header rows by iterating from the second row. For example, if your CSV has 2 header rows (rows 1 and 2) and data starts at row 3:
// Start reading from the third row
for (int i = 2; i < dataRange.getRowCount(); i++) {
for (int j = 0; j < dataRange.getColumnCount(); j++) {
// Convert 0-based loop index to Spire.XLS's 1-based cell index
CellRange cell = dataRange.get(i + 1, j + 1);
System.out.print(cell.getText() + "\t");
Q3. Can I export a specific range of a CSV to a DataTable?
A: Yes. Spire.XLS lets you define a precise cell range and export it to a DataTable with the exportDataTable(CellRange range, boolean exportColumnNames) method.
Conclusion
Spire.XLS for Java simplifies CSV file reading in Java, offering a robust alternative to manual parsing or basic libraries. Whether you need to read a simple CSV, or convert it to a structured DataTable, this guide provides the corresponding examples to help you implement CSV parsing efficiently.
For more advanced features (e.g., exporting to PDF), check the Spire.XLS for Java Documentation.
