How To Convert CSV to XLSX Using Java
We will show you how to convert one or multiple CSV files into an Excel Spreadsheet.
Join the DZone community and get the full member experience.
Join For FreeExcel is the household name in spreadsheet software for good reason. With an emphasis on user experience and interfacing, clients will feel comfortable with the familiar layout and formatting of an Excel spreadsheet. For this reason, when displaying data to your customer base, Excel is king.
An issue may arise, however, when data is stored as a CSV file rather than Excel. Many developers choose to store data as a CSV, due to its simplicity and expedience. However, while it is possible to open a CSV file in Excel, the manual process is often not the ideal solution for many businesses that rely on automated systems. Furthermore, while the CSV file format is familiar to developers and those who work with data input and analysis, clients may not recognize information in the way that it is meant to be perceived. This leads to trouble when the information being conveyed is of imminent importance to both your organization and your customers.
Luckily, this problem can be solved easily, as Cloudmersive has a solution for that.
Using one of our APIs, you can:
Quickly and efficiently convert your CSV files into XLSX using Java, and
Convert multiple CSV files into a group of worksheets in a single Excel spreadsheet.
First, however, we must start with some initial setup and installation steps.
Our library installation begins with a repository reference for our Maven POM file, to add the library.
xxxxxxxxxx
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then, add a dependency reference in pom.xml:
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>
Alternatively, to install with Gradle, add it in your root build.gradle at the end of repositories:
xxxxxxxxxx
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
And add the dependency in build.gradle:
xxxxxxxxxx
dependencies {
implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'
}
After these initial steps are completed, our focus should turn to the controller, where our imports should be added to the top of the file.
xxxxxxxxxx
// 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;
Then, we will call our function. The code shown below provides an example of how to complete this process.
xxxxxxxxxx
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.convertDocumentCsvToXlsx(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentCsvToXlsx");
e.printStackTrace();
}
Following these steps, you have set yourself up for success in converting CSV files into XLSX spreadsheets.
However, what happens when you need to covert multiple CSVs into one Excel workbook? The API we are going to show you next will split each CSV file you are converting into separate worksheets within the same Excel file.
The function of this process is quite similar and follows the same procedures for install and setup, as well as the import classes.
The first change occurs when we call our function, convertDocumentCsvMultiToXlsx, as shown below.
xxxxxxxxxx
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();
}
With this, you can easily add multiple CSV files into your Excel document by simply adding more input lines, as well as adding names for each worksheet within the function.
Cloudmersive takes pride in helping you solve any problems that may arise as we move into the digital age, especially when our systems can aid in bridging the gap between your business and your clients. If you find yourself needing additional help or have questions regarding any of our APIs, we will be happy to offer our assistance.
Opinions expressed by DZone contributors are their own.
Comments