How to Split XLSX Files into Separate Worksheets in Node.js
Not every page of an Excel spreadsheet is intended to be viewed by multiple stakeholders. Dividing up our spreadsheets can help focus content for relevant audiences.
Using the below code, we can easily convert multi-worksheet Excel documents into separate files — exactly document per worksheet — with a free API. We can even elect to return our documents as URL links by configuring the optional returnDocumentContents parameter in our request.
To structure our API call, let’s begin by installing the SDK. We can either run the following command:
npm install cloudmersive-convert-api-client --save
Or we can add the following snippet to our package.json instead:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Now we can include the ready-to-run code examples below into our file and customize our request. We can authorize our requests with a free-tier API key (this allows up to 800 API calls per month with no additional commitments):
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.SplitDocumentApi();
var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.
var opts = {
'returnDocumentContents': true // Boolean | Set to true to return the contents of each Worksheet directly, set to false to only return URLs to each resulting worksheet. Default is true.
};
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.splitDocumentXlsx(inputFile, opts, callback);
All done!