This content originally appeared on DEV Community and was authored by jelizaveta
In daily data processing and system integration, we often encounter scenarios where we need to extract data from Excel files and process it in plain text format. Whether for data analysis, automated reporting, or data exchange with other systems, converting Excel to text is a fundamental and important task. Traditional copy-and-paste methods are inefficient and error-prone, while manually parsing Excel files is complex and time-consuming. So, is there an efficient and convenient way to help Java developers easily achieve this conversion?
The answer is yes! This article will show you how to use the powerful Spire.XLS for Java library to easily convert Excel to text in Java applications. It not only helps solve common data processing challenges but also significantly improves development efficiency.
Why Choose Spire.XLS for Java?
Spire.XLS for Java is a professional Excel component designed for creating, reading, editing, and converting Excel files in Java. It provides a wide range of features, including:
- Comprehensive Excel support : Supports all mainstream formats, including XLS, XLSX, and XLSM.
- Powerful conversion capabilities : Convert Excel not only to text but also to PDF, HTML, CSV, images, and more.
- Ease of use : Offers intuitive APIs, making it quick to get started.
- High performance : Performs well when handling large Excel files.
- Standalone operation : Does not rely on Microsoft Office and can run independently on servers.
Choosing Spire.XLS for Java means choosing a powerful, stable, and reliable tool that greatly simplifies the complexity of handling Excel files in Java.
Environment Setup and Preparation
Before using Spire.XLS for Java, you need to import it into your Java project. There are two common ways to do this:
1. Maven Dependency
If you are using Maven to manage your project, simply 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>15.9.1</version>
</dependency>
</dependencies>
2. Manual JAR Import
Alternatively, you can download the JAR package from the Spire.XLS for Java official website and manually add it to your project’s build path.
Once configured, you’re ready to start using Spire.XLS.
Converting Excel to Text with Spire.XLS
Below is a complete code example that demonstrates how to convert an Excel file to a text file using Spire.XLS.
Core Steps:
-
Load the Excel file : Use the
Workbook
class to load the target file. - Get the worksheet : Select the worksheet to be converted.
-
Perform the conversion : Call the
saveToFile
method and specify the output format as text. - Save the text file : Define the output file path and encoding.
Code Example:
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import java.nio.charset.Charset;
import java.io.File;
public class ExcelToTextConverter {
public static void main(String[] args) {
// Excel file path
String excelFilePath = "data.xlsx";
// Output text file path
String outputTextFilePath = "output.txt";
try {
// 1. Load Excel file
Workbook workbook = new Workbook();
workbook.loadFromFile(excelFilePath);
// 2. Get the first worksheet
// To convert all worksheets, loop through workbook.getWorksheets()
Worksheet worksheet = workbook.getWorksheets().get(0);
// 3. Perform conversion and save
// The second parameter specifies the delimiter between cells, e.g., " " (space) or "," (comma)
// The third parameter specifies encoding; UTF-8 is recommended to avoid garbled text
// Ensure the output directory exists
File outputFile = new File(outputTextFilePath);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
worksheet.saveToFile(outputTextFilePath, ",", Charset.forName("UTF-8")); // Use comma as delimiter
System.out.println("Excel file '" + excelFilePath + "' has been successfully converted to text file '" + outputTextFilePath + "'.");
} catch (Exception e) {
e.printStackTrace();
System.err.println("An error occurred during conversion: " + e.getMessage());
}
}
}
Notes:
-
Delimiter choice : In
worksheet.saveToFile()
, the second parameter specifies the delimiter between cell contents. Common options include space" "
, comma","
, and tab"\t"
. Choose according to your needs. -
Encoding : To avoid issues with Chinese or other special characters, explicitly specify encoding such as
Charset.forName("UTF-8")
. - Empty cell handling : Spire.XLS for Java preserves blank cells and separates them using the specified delimiter.
-
Multiple worksheet conversion : If your Excel file contains multiple worksheets and you want them all converted, loop through
workbook.getWorksheets()
and save each one separately.
Conclusion
This article introduced how to use the Spire.XLS library in Java to convert Excel files into text. With its rich features, simple API, and excellent performance, Spire.XLS is an ideal choice for Java developers dealing with Excel files. It not only helps you efficiently extract and convert data but also proves valuable in scenarios like automated reporting and system integration.
This content originally appeared on DEV Community and was authored by jelizaveta

jelizaveta | Sciencx (2025-09-30T03:14:18+00:00) Convert Excel to Text with Java. Retrieved from https://www.scien.cx/2025/09/30/convert-excel-to-text-with-java/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.