How to Convert Multiple CSV Files into a Single XLSX Spreadsheet using Node.js
Converting CSV files to XLSX files is a common need — and with a free API, we can make that conversion on a large scale.
Using the below code, we can take advantage of a free API that can convert 10+ CSV documents to a single XLSX spreadsheet, with one resulting worksheet per CSV document entered. We can make up to 800 free API calls per month with a free-tier API key.
We can begin by installing the SDK. Let’s either run the following command:
npm install cloudmersive-convert-api-client --save
Or add the following snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Lastly, let’s add the following code into our file to call the function, and then we can easily convert our CSV documents:
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';
var apiInstance = new CloudmersiveConvertApiClient.ConvertDocumentApi();
var inputFile1 = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | First input file to perform the operation on.
var inputFile2 = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Second input file to perform the operation on.
var opts = {
'inputFile3': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Third input file to perform the operation on.
'inputFile4': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Fourth input file to perform the operation on.
'inputFile5': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Fifth input file to perform the operation on.
'inputFile6': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Sixth input file to perform the operation on.
'inputFile7': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Seventh input file to perform the operation on.
'inputFile8': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Eighth input file to perform the operation on.
'inputFile9': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Ninth input file to perform the operation on.
'inputFile10': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Tenth input file to perform the operation on.
'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.
};
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertDocumentCsvMultiToXlsx(inputFile1, inputFile2, opts, callback);
That’s all there is to it — no more code required!