DEV Community 👩‍💻👨‍💻


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.

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

        //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


Top comments (0)

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.