{{announcement.body}}
{{announcement.title}}

How To Convert CSV to XLSX Using Java

DZone 's Guide to

How To Convert CSV to XLSX Using Java

We will show you how to convert one or multiple CSV files into an Excel Spreadsheet.

· Java Zone ·
Free Resource

Excel 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: 

  1. Quickly and efficiently convert your CSV files into XLSX using Java, and 

  1. Convert multiple CSV files into 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. 

XML
 




xxxxxxxxxx
1


 
1
<repositories> 
2
    <repository> 
3
        <id>jitpack.io</id> 
4
        <url>https://jitpack.io</url> 
5
    </repository> 
6
</repositories> 



Then, add a dependency reference in pom.xml:  

XML
 




xxxxxxxxxx
1


 
1
<dependencies> 
2
<dependency> 
3
    <groupId>com.github.Cloudmersive</groupId> 
4
    <artifactId>Cloudmersive.APIClient.Java</artifactId> 
5
    <version>v3.54</version> 
6
</dependency> 
7
</dependencies> 



Alternatively, to install with Gradle, add it in your root build.gradle at the end of repositories:  

Java
 




xxxxxxxxxx
1


1
allprojects { 
2
    repositories { 
3
        ... 
4
        maven { url 'https://jitpack.io' } 
5
    } 
6
} 



And add the dependency in build.gradle:  

Java
 




xxxxxxxxxx
1


 
1
dependencies { 
2
        implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54' 
3
} 



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.  

Java
 




xxxxxxxxxx
1


 
1
// Import classes: 
2
import com.cloudmersive.client.invoker.ApiClient; 
3
import com.cloudmersive.client.invoker.ApiException; 
4
import com.cloudmersive.client.invoker.Configuration; 
5
import com.cloudmersive.client.invoker.auth.*; 
6
import com.cloudmersive.client.ConvertDocumentApi; 



Then, we will call our function. The code shown below provides an example of how to complete this process.   

Java
 




xxxxxxxxxx
1
33


 
1
ApiClient defaultClient = Configuration.getDefaultApiClient(); 
2
 
          
3
// Configure API key authorization: Apikey 
4
ApiKeyAuth Apikey = (ApiKeyAuth)defaultClient.getAuthentication("Apikey"); 
5
Apikey.setApiKey("YOUR API KEY"); 
6
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null) 
7
//Apikey.setApiKeyPrefix("Token"); 
8
 
          
9
ConvertDocumentApi apiInstance = new ConvertDocumentApi(); 
10
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on. 
11
try { 
12
    byte[] result = apiInstance.convertDocumentCsvToXlsx(inputFile); 
13
    System.out.println(result); 
14
} catch (ApiException e) { 
15
    System.err.println("Exception when calling ConvertDocumentApi#convertDocumentCsvToXlsx"); 
16
    e.printStackTrace(); 
17
} 



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. 

Java
 




xxxxxxxxxx
1
53


 
1
ApiClient defaultClient = Configuration.getDefaultApiClient();  
2
 
          
3
// Configure API key authorization: Apikey  
4
ApiKeyAuth Apikey = (ApiKeyAuth)defaultClient.getAuthentication("Apikey"); 
5
Apikey.setApiKey("YOUR API KEY");  
6
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)  
7
//Apikey.setApiKeyPrefix("Token");  
8
 
          
9
 ConvertDocumentApi apiInstance = new ConvertDocumentApi();  
10
File inputFile1 = new File("/path/to/inputfile"); // File | First input file to perform the operation on.  
11
File inputFile2 = new File("/path/to/inputfile"); // File | Second input file to perform the operation on.  
12
File inputFile3 = new File("/path/to/inputfile"); // File | Third input file to perform the operation on.  
13
File inputFile4 = new File("/path/to/inputfile"); // File | Fourth input file to perform the operation on.  
14
File inputFile5 = new File("/path/to/inputfile"); // File | Fifth input file to perform the operation on.  
15
File inputFile6 = new File("/path/to/inputfile"); // File | Sixth input file to perform the operation on.  
16
File inputFile7 = new File("/path/to/inputfile"); // File | Seventh input file to perform the operation on.  
17
File inputFile8 = new File("/path/to/inputfile"); // File | Eighth input file to perform the operation on.  
18
File inputFile9 = new File("/path/to/inputfile"); // File | Ninth input file to perform the operation on.  
19
File inputFile10 = new File("/path/to/inputfile"); // File | Tenth input file to perform the operation on.  
20
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.  
21
try {  
22
byte[] result = apiInstance.convertDocumentCsvMultiToXlsx(inputFile1, inputFile2, inputFile3, inputFile4, inputFile5, inputFile6, inputFile7, inputFile8, inputFile9, inputFile10, worksheetNames); 
23
System.out.println(result);  
24
} catch (ApiException e) {  
25
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentCsvMultiToXlsx");  
26
e.printStackTrace();  
27
} 



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.

Topics:
api, comma separated values, conversion, convert, csv, excel, java, spreadsheets, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}