DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Generic Way To Convert Between Java and PostgreSQL Enums
  • How to Convert CSV to XML in Java
  • Practical Generators in Go 1.23 for Database Pagination
  • Java and MongoDB Integration: A CRUD Tutorial [Video Tutorial]

Trending

  • Debugging Core Dump Files on Linux - A Detailed Guide
  • Automatic Code Transformation With OpenRewrite
  • How to Format Articles for DZone
  • How to Convert XLS to XLSX in Java
  1. DZone
  2. Data Engineering
  3. Data
  4. How to Convert XLSX to CSV in Java

How to Convert XLSX to CSV in Java

Convert any Excel document to CSV with support for both XLSX and XLSB file formats, as well as legacy formats such as XLS.

By 
Brian O'Neill user avatar
Brian O'Neill
DZone Core CORE ·
Oct. 05, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
12.9K Views

Join the DZone community and get the full member experience.

Join For Free

With design-focused formatting capabilities and a recognizable, approachable interface, it is no wonder that Excel is the leading spreadsheet software. Because it possesses the functionality to create both complex tables and graphs, users often utilize the application as a one-stop-shop for data entry and graphic design.  

While many choose to use Excel as their main resource for data input, it is not always the ideal format when compiling and sharing that data with other programs. This is partially caused by the in-depth formatting that is so appealing to other users, as it cannot easily be converted to non-compatible applications. In these instances, it would be more useful to use a plain text version of your input data. This is where the Comma-Separated Values (CSV) format is most useful. CSV will show the data in stacked rows to represent each column in your average Excel file. This can then be easily read by other applications with no need to work around difficult formatting. For example, if a budgets officer receives an Excel spreadsheet tracking invoices, they can then convert this data to CSV to input more easily into their company databases. This will not only accelerate processing speeds but increase overall productivity as a result. 

The following Convert APIs will allow you to instantly convert any Excel file to CSV, with support for the legacy format XLS. We will also show you how to convert multiple Excel worksheets at once to return separate CSV files. In performing this conversion process, our main goal is to preserve the organization of the data and remove clunky formatting that may hinder your data entry. 

Our first API will show you how to convert a single XLSX spreadsheet to CSV in Java. We will need to install our library with a repository reference to Jitpack, as seen here: 

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


Then, we will add a reference to the dependency:  

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>


To install with Gradle, add it into 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
}


Then, add the dependency in build.gradle:  

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


After this, we can add our imports to the top of the file and call our function, ConvertDocumentXlsxToCsv:  

Java
xxxxxxxxxx
1
25
 
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;
7
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15
16
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
17
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
18
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.
19
try {
20
    byte[] result = apiInstance.convertDocumentXlsxToCsv(inputFile, outputEncoding);
21
    System.out.println(result);
22
} catch (ApiException e) {
23
    System.err.println("Exception when calling ConvertDocumentApi#convertDocumentXlsxToCsv");
24
    e.printStackTrace();
25
}


Your main input for this action should be an input file. Optionally, you can also set the output text encoding for the result. This can be UTF-8, ASCII and UTF-32, with a default of UTF-8. This will automatically take your specified spreadsheet and convert it to plain text in CSV.  

To ensure that the API functions properly, you will need to verify that: 

  • Your input file is valid 
  • Your API Key has been properly inserted into the code block. This can be retrieved at no cost on the Cloudmersive website and will provide access to 800 monthly calls across our library of APIs.

If you need to convert multiple worksheets from one Excel file, the following API will be your best option. This can handle Excel documents of any size and will maintain the returned CSV files in the same order of the input file worksheets. 

As shown above, you will need to first install the library using the same references. Then we can once again place our imports at the top of the file and call our function, ConvertDocumentXlsxToCsvMulti: 

Java
xxxxxxxxxx
1
25
 
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;
7
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15
16
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
17
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
18
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.
19
try {
20
    CsvCollection result = apiInstance.convertDocumentXlsxToCsvMulti(inputFile, outputEncoding);
21
    System.out.println(result);
22
} catch (ApiException e) {
23
    System.err.println("Exception when calling ConvertDocumentApi#convertDocumentXlsxToCsvMulti");
24
    e.printStackTrace();
25
}


If you are working with a legacy version of Excel or need to convert an older file for current databasing, this last API will allow you to convert your XLS files to CSV. It follows the same initial steps as our two previous examples: install the library with a Jitpack reference in Maven and input our imports at the top of the file. After this, you can call our function, ConvertDocumentXlsToCsv:  

Java
x
24
 
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;
7
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15
16
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
17
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
18
try {
19
    byte[] result = apiInstance.convertDocumentXlsToCsv(inputFile);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling ConvertDocumentApi#convertDocumentXlsToCsv");
23
    e.printStackTrace();
24
}


If you have any questions about using these APIs or inquiries concerning other API solutions, you can visit the Cloudmersive website where our team is happy to help with anything you might need. 

Convert (command) CSV Java (programming language) Database Data (computing)

Opinions expressed by DZone contributors are their own.

Related

  • The Generic Way To Convert Between Java and PostgreSQL Enums
  • How to Convert CSV to XML in Java
  • Practical Generators in Go 1.23 for Database Pagination
  • Java and MongoDB Integration: A CRUD Tutorial [Video Tutorial]

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!