DEV Community 👩‍💻👨‍💻

carlwils
carlwils

Posted on • Updated on

Convert Excel to Text (.TXT) File using Java

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>https://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>
Enter fullscreen mode Exit fullscreen mode

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);

    }
}
Enter fullscreen mode Exit fullscreen mode

ReadExcel

Top comments (0)

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.