Home > database >  Read a single worksheet from an XLSX file without reading the entire file?
Read a single worksheet from an XLSX file without reading the entire file?

Time:12-30

I am using the NPM 'xlsx' library to process large XLSX files (10-20MB) in Node.js / Javascript. This has slow performance because it reads the entire XLSX file before operating, rather than only reading what it needs. In this case, I only need to read a single worksheet with the title 'target-sheet' and the rest of the file can be ignored.

Is there a way to delete the sheets other than 'target-sheet' without reading the rest of the file? Overall, I am looking for a way to ignore the other worksheets at the outset, before their contents are read, in order to improve efficiency.

Thanks so much

CodePudding user response:

Assuming you are talking about this package and using XLSX.read or XLSX.readFile, then you want to pass the sheets option (docs here). So, something like:

XLSX.readFile(filename, { sheets: [ 'target-sheet' ] })

CodePudding user response:

const XLSX = require('xlsx');// Load the XLSX file const workbook = XLSX.readFile('file.xlsx');// Get the list of sheet names in the workbook const sheetNames =workbook.SheetNames;// Iterate through the sheet names and delete the sheets that are not 'target-sheet'for (const sheetName of sheetNames) {if (sheetName !== 'target-sheet'){delete workbook.Sheets[sheetName];}}// Save the modified workbookXLSX.writeFile(workbook, 'file.xlsx');

  • Related