How to Convert Multiple CSV Files into a Single XLSX Spreadsheet using Java
Creating Excel spreadsheets from CSV files is a common task for data professionals. Our Multi-CSV file to XLSX conversion API is designed to help you make clean, native conversions for multiple CSV files at once, so you can consolidate your data right away and avoid the time-suck of pointing and clicking through each file conversion.
With the Java code examples provided below, you can easily incorporate this API into your application and process up to 10 CSV files at once into a single XLSX spreadsheet. Before you get started following the demonstration, you’ll first need to register a free account on our website to get your API key (free-tier keys provide a limit of 800 API calls per month with zero additional commitments).
Our first step is to install the API client with Maven. We can do so by first adding a reference to the pom.xml repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
And then adding a 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 turn our attention to the controller and copy in our 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;
Now we can call the function. Within the code block below, we can include each of our input file paths (up to 10) in the fields inputFile1, inputFile2, etc. In addition, we have the option to specify the name of each CSV worksheet, which can be done in the same order we entered our input file paths. If you elect to configure this option, make sure to follow the correct format and place a comma in between each string.
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 inputFile1 = new File("/path/to/inputfile"); // File | First input file to perform the operation on.
File inputFile2 = new File("/path/to/inputfile"); // File | Second input file to perform the operation on.
File inputFile3 = new File("/path/to/inputfile"); // File | Third input file to perform the operation on.
File inputFile4 = new File("/path/to/inputfile"); // File | Fourth input file to perform the operation on.
File inputFile5 = new File("/path/to/inputfile"); // File | Fifth input file to perform the operation on.
File inputFile6 = new File("/path/to/inputfile"); // File | Sixth input file to perform the operation on.
File inputFile7 = new File("/path/to/inputfile"); // File | Seventh input file to perform the operation on.
File inputFile8 = new File("/path/to/inputfile"); // File | Eighth input file to perform the operation on.
File inputFile9 = new File("/path/to/inputfile"); // File | Ninth input file to perform the operation on.
File inputFile10 = new File("/path/to/inputfile"); // File | Tenth input file to perform the operation on.
String worksheetNames = "worksheetNames_example"; // String | Optional; Specify the name of each CSV's worksheet in order, separated with commas (e.g. \"worksheet1,worksheet2,worksheet3\"). Defaults to the names of the input CSV files. Recommended when inputting the files directly, without file names.
try {
byte[] result = apiInstance.convertDocumentCsvMultiToXlsx(inputFile1, inputFile2, inputFile3, inputFile4, inputFile5, inputFile6, inputFile7, inputFile8, inputFile9, inputFile10, worksheetNames);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentCsvMultiToXlsx");
e.printStackTrace();
}
After that, we just need to include our API key in the appropriate field (indicated by the code comments), and we’re all done. You can put your CSV to XLSX conversion needs to rest!