How to Convert Excel to CSV in Java

This tutorial explains how to convert Excel to CSV in Java. There are many applications that require data in CSV format whereas the source data is available in an Excel file. In this case, using Java convert Excel to CSV and then use it wherever required. There may be cases where specific separators are required to be saved in the CSV file to differentiate values retrieved from different columns. This can also be achieved by using TxtSaveOptions class object.

Steps to Convert Excel to CSV in Java

  1. Add Aspose.Cells to the project using Maven
  2. Add a reference to TxtSaveOptions and Workbook using Import in the program
  3. Load the source Excel file into the Workbook object
  4. Instantiate the TxtSaveOptions to set parameters of the output CSV file
  5. Set the separator to be used in the output CSV file
  6. Save the Excel file as CSV

In the above steps, we load an Excel file and then save it as CSV. We set CSV file characteristics by using TxtSaveOptions class object. This class provides a variety of options like you can put quotes around all the values, clear the source workbook after conversion, indicate whether all the sheets or only the active sheet is to be converted, the entire sheet is to be converted or only a particular range is required to be transformed and many more. You can save the output CSV file to a physical file on disk or save it to a memory stream as per requirement.

Code for Conversion of Excel to CSV in Java

This code initializes a Workbook class object and then instantiates a TxtSaveOptions class object for setting parameters of the resultant CSV file. Finally Workbook.save() is used to generate CSV file. If you want to convert an Excel file to CSV format using a language other than Java say C#, you may refer to the article export large Excel file to CSV in C#.

Neither interop is used nor MS Excel is required for this conversion.

 English