How to Convert XLSX to CSV in Java
Convert any Excel document to CSV with support for both XLSX and XLSB file formats, as well as legacy formats such as XLS.
Join the DZone community and get the full member experience.Join For Free
With design-focused formatting capabilities and a recognizable, approachable interface, it is no wonder that Excel is the leading spreadsheet software. Because it possesses the functionality to create both complex tables and graphs, users often utilize the application as a one-stop-shop for data entry and graphic design.
While many choose to use Excel as their main resource for data input, it is not always the ideal format when compiling and sharing that data with other programs. This is partially caused by the in-depth formatting that is so appealing to other users, as it cannot easily be converted to non-compatible applications. In these instances, it would be more useful to use a plain text version of your input data. This is where the Comma-Separated Values (CSV) format is most useful. CSV will show the data in stacked rows to represent each column in your average Excel file. This can then be easily read by other applications with no need to work around difficult formatting. For example, if a budgets officer receives an Excel spreadsheet tracking invoices, they can then convert this data to CSV to input more easily into their company databases. This will not only accelerate processing speeds but increase overall productivity as a result.
The following Convert APIs will allow you to instantly convert any Excel file to CSV, with support for the legacy format XLS. We will also show you how to convert multiple Excel worksheets at once to return separate CSV files. In performing this conversion process, our main goal is to preserve the organization of the data and remove clunky formatting that may hinder your data entry.
Our first API will show you how to convert a single XLSX spreadsheet to CSV in Java. We will need to install our library with a repository reference to Jitpack, as seen here:
Then, we will add a reference to the dependency:
To install with Gradle, add it into your root build.gradle at the end of repositories:
Then, add the dependency in build.gradle:
After this, we can add our imports to the top of the file and call our function, ConvertDocumentXlsxToCsv:
Your main input for this action should be an input file. Optionally, you can also set the output text encoding for the result. This can be UTF-8, ASCII and UTF-32, with a default of UTF-8. This will automatically take your specified spreadsheet and convert it to plain text in CSV.
To ensure that the API functions properly, you will need to verify that:
- Your input file is valid
- Your API Key has been properly inserted into the code block. This can be retrieved at no cost on the Cloudmersive website and will provide access to 800 monthly calls across our library of APIs.
If you need to convert multiple worksheets from one Excel file, the following API will be your best option. This can handle Excel documents of any size and will maintain the returned CSV files in the same order of the input file worksheets.
As shown above, you will need to first install the library using the same references. Then we can once again place our imports at the top of the file and call our function, ConvertDocumentXlsxToCsvMulti:
If you are working with a legacy version of Excel or need to convert an older file for current databasing, this last API will allow you to convert your XLS files to CSV. It follows the same initial steps as our two previous examples: install the library with a Jitpack reference in Maven and input our imports at the top of the file. After this, you can call our function, ConvertDocumentXlsToCsv:
If you have any questions about using these APIs or inquiries concerning other API solutions, you can visit the Cloudmersive website where our team is happy to help with anything you might need.
Opinions expressed by DZone contributors are their own.