How to Convert One or Multiple Excel (XLSX) Worksheets to CSV Format using Java

Cloudmersive
3 min readOct 14, 2022

--

Looking for a free & convenient way to convert one or more XLSX worksheets — and not entire spreadsheets — to CSV format? Our Document Conversion API endpoint contains two relevant file conversion services for your convenience, which handle one or multiple XLSX worksheet to CSV file conversions respectively. To use either, all you need to do is follow along with the demonstration below & use the ready-to-run Java code examples I’ve provided to structure your API call.

Our first step — regardless of which API we want to use — is to install the API client. In this demonstration, we’ll install with Maven (if you’re looking to install with Gradle, our API console page will have you covered). Let’s begin installation by adding a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Now let’s wrap up installation by adding one additional reference to the pom.xml dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

With installation complete, we can shift focus to our controller and add the following imports:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDocumentApi;

Last but not least, we can go about calling each respective file conversion function. First, however, we’ll need to make sure we have an API key ready to authenticate access; we can get one by visiting the Cloudmersive website and registering a free account (this account will provide a limit of 800 API calls per month & zero additional commitments).

Let’s now call the Single XLSX Worksheet to CSV Conversion API. We can use the below code examples to structure our API call, passing our API key & input file path through the appropriate fields (indicated in the code comments):

ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
String outputEncoding = "outputEncoding_example"; // String | Optional, set the output text encoding for the result; possible values are UTF-8, ASCII and UTF-32. Default is UTF-8.
try {
byte[] result = apiInstance.convertDocumentXlsxToCsv(inputFile, outputEncoding);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentXlsxToCsv");
e.printStackTrace();
}

Now let’s call the Multiple XLSX Worksheets to CSV Conversion API. The code we’ll use is very similar, and your API call will return multiple CSV files — one for each worksheet in the spreadsheet file you included:

ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
String outputEncoding = "outputEncoding_example"; // String | Optional, set the output text encoding for the result; possible values are UTF-8, ASCII and UTF-32. Default is UTF-8.
try {
CsvCollection result = apiInstance.convertDocumentXlsxToCsvMulti(inputFile, outputEncoding);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentXlsxToCsvMulti");
e.printStackTrace();
}

Please note that each API offers the option to set he output text encoding if you wish to do so; you can set this option by following the example provided in the code comments.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet