CSV contains the comma-separated values, normally used to store tabular data without formatting. These files can be viewed in any text editor and also in MS Excel for tabular format. On the other hand, the most used formats for MS Excel files are XLS and XLSX. These formats support countless formatting options. This article discusses the conversion of the Excel spreadsheets of XLS/XLSX format to CSV format and CSV to XLS/XLSX format programmatically using Java.
The following topics are covered below:
Java API for Excel Files and CSV Conversion
GroupDocs.Conversion offers Java API that allows the conversion of spreadsheet formats into one another. I will be using this API for converting XLSX into CSV and also CSV into XLS or XLSX using Java. Additionally, the API allows the back and forth conversion of many other document and image formats like word-processing documents, presentations, eBooks, JPG, PNG, WebP, and many more.
Download or Configure
You may download the JAR file from the downloads section, or just get the repository and dependency configurations for the pox.xml of your maven-based Java applications.
<repository> <id>GroupDocsJavaAPI</id> <name>GroupDocs Java API</name> <url>http://repository.groupdocs.com/repo/</url> </repository>
<dependency> <groupId>com.groupdocs</groupId> <artifactId>groupdocs-conversion</artifactId> <version>21.7</version> </dependency>
Convert CSV to Excel (XLS/XLSX) in Java
Transformation of comma-separated data into the tabular form for a better presentation requires conversion from CSV to XLS/XLSX format. The following steps allow converting the CSV files to XLS/XLSX format within the Java application.
- Prepare the load options for loading the CSV file.
- Load the CSV using the Converter class.
- Set the conversion format to XLSX using SpreadsheetConvertOptions.
- Call the convert method to get the CSV data transformed into XLSX format.
The following code shows how to convert your CSV file into XLSX format in Java.
For the XLS format, just set the conversion format accordingly and provide the appropriate file name with the extension.
Convert Excel (XLS/XLSX) to CSV in Java
Similarly, if formatting is not required, you can remove the styles and visuals and simply keep the comma-separated data by converting the XLS/XLSX to CSV format and saving the space.
The following steps allow converting the XLS or XLSX format to CSV within the Java applications.
- Load the Excel file (XLS or XLSX) using the Converter class.
- Set the conversion format to CSV using SpreadsheetConvertOptions.
- Call the convert method to transform the spreadsheet data into CSV format.
The following code shows how to convert XLS or XLSX to CSV format in Java.
Get a Free API License
You can get a free temporary license in order to use the API without the evaluation limitations.
Conclusion
To conclude, you learned how to convert the MS Excel files to CSV format and also the conversion of CSV files to XLS and XLSX format programmatically with your Java applications. You can learn more about the Java Conversion API using the documentation, or by examples available on GitHub. Get in touch with us for any query at the forum.