Convert Excel to CSV in Java

excel to csv
Convert Excel to CSV in Java

We know that Excel workbooks are widely used to organize data, perform financial analysis, data entry, data management, accounting, charting & graphing, etc. However, for importing and exporting data, we rely upon comma-separated values (CSV) files. It’s one of the ways to exchange structured information through a simple text file. In this article, we are going to programmatically convert Excel to CSV using Java SDK.

Excel to CSV Conversion API

Aspose.Cells Cloud SDK for Java is a REST-based API offering the capabilities to create, edit and convert Excel to CSV, Excel to PDF, Excel to HTML and a variety of other supported formats. Now in order to use the SDK, the first step is to add the following reference in pom.xml of the maven build type.

<repositories>
    <repository>
        <id>AsposeJavaAPI</id>
        <name>Aspose Java API</name>
        <url>https://repository.aspose.cloud/repo/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-cells-cloud</artifactId>
        <version>22.8</version>
    </dependency>
</dependencies>

Once the reference of Cloud SDK is added, please register an account over Aspose Cloud and lookup/create Client ID and Client Secret at Cloud Dashboard.

Convert Excel to CSV in Java

Please follow the instructions given below to convert XLSX to CSV format.

  • Create an instance of CellsApi while providing client credentials as input arguments
  • Specify the name of input Excel, resultant format as CSV, and output file name
  • Upload the input Excel workbook to cloud storage using the uploadFile(…) method
  • Finally, call the method cellsWorkbookGetWorkbook(…) to initiate the conversion operation
excel to csv file preview
image1:- Excel to CSV file preview

The sample files used in the above example can be downloaded from myDocument.xlsx and Resultant.csv.

Convert local XLSX to CSV

In this section, we are going to learn the steps on how to convert XLSX to CSV without uploading the input file to cloud storage. So please follow the instructions given below.

  • Create an instance of CellsApi while providing client credentials as input arguments
  • Specify the name of input Excel, resultant format as CSV, and output file name
  • Thirdly, call the method cellsWorkbookPutConvertWorkbook(…) to initiate the XLSX to CSV conversion

XLSX to CSV using cURL Commands

The cURL commands can be used to perform XLSX to CSV conversion. So the first step is to generate a JWT access token while executing the following command (you need to provide your personalized client credential details specified on the Cloud dashboard)

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bbf94a2c-6d7e-4020-b4d2-b9809741374e&client_secret=1c9379bb7d701c26cc87e741a29987bb" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Once the JWT is generated, please execute the following command to convert XLSX to CSV and save the output in cloud storage. Please note that input XLSX is already available in cloud storage.

curl -v -X GET "https://api.aspose.cloud/v3.0/cells/myDocument(1).xlsx?format=CSV&isAutoFit=false&onlySaveTable=false&outPath=converted.csv&checkExcelRestriction=true" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>"

Conclusion

In this article, we have acquainted ourselves with details on how to convert Excel to CSV. We have noticed the conversion can be performed using Java code snippets as well as through cURL commands. If you encounter any issues while using the APIs, please feel free to contact us via the product support forum.

Related Articles

We highly recommend visiting the following links to learn more about: