How to Convert an Excel XLS (97–03) Spreadsheet Directly to CSV Format using Java
It’s no secret that CSV is an ideal interoperable data type for sharing data in spreadsheet format. If you’re working with old-school Excel spreadsheets, however, you might notice some issues with compatibility when converting and sharing those files. Thankfully, you can easily circumvent this problem by using our XLS (97–03) to CSV conversion API. Requiring only your input file path & Cloudmersive API key as inputs, this API will return your data encoded in CSV format in an instant. The best part? You can use this API for free — you just need to register a free account on our website (free accounts yield a limit of 800 API calls per month: perfect for small-scale projects, startups, etc.) and include your API key in the appropriate field within the code examples provided below.
The below demonstration will help you structure your API call using ready-to-run code examples in Java (supplied from our API Console page, where you can also test all our API solutions with sample files).
We can begin by first installing the API client. Include a reference in the pom.xml repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then include a reference in the pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Lastly, add the imports to the top of your file & call the function:
// 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;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.
try {
byte[] result = apiInstance.convertDocumentXlsToCsv(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentXlsToCsv");
e.printStackTrace();
}
Within the above code, include your API key in the setApikey field and pass your input file path through the inputFile field. After that, you’re all done!