Convert Excel to Text (.TXT) File using Java

·

1 min read

Since the text (.TXT) files are frequently used for storing data, in certain situations, you may need to convert your Excel documents into text files. This artile will share how to accomplish this task using Free Spire.XLS for Java.

Import Jar Dependency (2 Method)

1# Download the free library and unzip it, then add the Spire.Xls.jar file to your project as dependency.
2# Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

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

Sample Code

With only a few lines of code, you can convert an .xlsx/xls file into a .txt file. The complate sample code is shown as below:

import com.spire.xls.*;

import java.nio.charset.Charset;

public class toText {
    public static void main(String[] args) {
        //Create a Workbook object
        Workbook workbook = new Workbook();

        //Load a sample Excel document
        workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\test.xlsx");

        //Get the first worksheet
        Worksheet worksheet = workbook.getWorksheets().get(0);

        //Convert to text
        Charset charset = Charset.forName("utf8");
        worksheet.saveToFile("toText.txt", " ", charset);

    }
}