Convert Excel XLSX to CSV in Node.js
Learn how to convert Excel XLSX documents to CSV format using ready-to-run code examples.
While it’s simple enough to export individual Excel XLSX files as CSV documents, it’s a little more challenging and time-consuming to make that same formatting conversion at scale using code.
If you ask me, however, document format conversions as common as Excel to CSV should always be straightforward, consistent, and easy. So, why don’t we simply automate our conversions with a dependable document conversion API?
In this tutorial, we’ll learn how to easily convert our Excel files to CSV format in Node.js using a couple of specialized document conversion APIs.
Depending on the scope of our document conversion needs (i.e., the size and complexity of the Excel files we’re converting), we can choose from one of two slightly different Excel to CSV conversion API iterations: one optimized to convert Excel files with only one worksheet, and one optimized to convert Excel files with multiple worksheets. Conveniently, we can set the encoding (UTF-8, ASCII, or UTF-32) of our resulting CSV strings when we call either API iteration.
Let’s get started, shall we?
Step 1: Install the Package
The first thing we need to do is install the SDK. To do that, we can use the following NPM command:
npm install cloudmersive-convert-api-client --save
Alternatively, we can simply add the following snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Step 2: Configure the API key
With installation out of the way, we can get ready to configure our API requests. For that, we’ll need our own personal Cloudmersive API key.
If we have a Cloudmersive account already, we can copy an API key from our account portal on the Cloudmersive website. If we don’t have an account yet, we can follow that link to sign up for a free account and get a free API key. With our free API key, we’ll be able to make up to 800 API calls per month with zero additional commitments (we can easily scale our plan up if/when the need arises).
We can now add the following snippet to our file and replace the ‘YOUR API KEY’
placeholder text with our own API key string:
var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
Step 3: Create an API Instance
Now we can create an instance of the API we’re calling. To take care of that, let’s copy the following code into our file:
var apiInstance = new CloudmersiveConvertApiClient.ConvertDocumentApi();
var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.
Step 4: Call the Excel to CSV Conversion Functions
As mentioned earlier, we can choose from one of two unique Excel to CSV document conversion API iterations depending on the number of worksheets our Excel documents contain.
If we’re converting Excel files with exactly one worksheet, we can use the following code:
var opts = {
'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.
};
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertDocumentXlsxToCsv(inputFile, opts, callback);
And if we’re converting Excel files with multiple worksheets, we can use the following code instead:
var opts = {
'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.
};
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertDocumentXlsxToCsvMulti(inputFile, opts, callback);
Understanding the API Response
Our single worksheet API call will return a simple string containing our CSV contents in the text encoding we specified.
Conversely, our multiple worksheet API call will return a more complex response object to accommodate multiple CSV string results.
As depicted in the example JSON response model below, we’ll find the title and converted CSV contents for each of our original Excel document’s worksheets returned within the “CsvFiles”
array. We’ll also find the total number of converted worksheets represented in the numeric property “FileCount”
.
{
"Successful": true,
"CsvFiles": [
{
"Title": "string",
"FileContents": "string"
}
],
"FileCount": 0
}
Now we’re all done - that’s all the code we’ll need to start making our Excel to CSV conversions!
As always, remember to hit “subscribe” if you found this article helpful. If you have any questions for us, feel free to contact a member of our sales team!