Sheetjs: Read Multiple worksheets in the same Excel

Created on 11 Jun 2015  路  4Comments  路  Source: SheetJS/sheetjs

Hi .. Is is possible to read multiple worksheet under same excel(.xlsx) file which is uploaded ? Please reply if there is a way to do this

Most helpful comment

Yes, the default behavior of the XLSX module is to return a workbook object that has a "Sheets" property.

First let's get the workbook object.
var workbook = XLSX.readFile('test.xlsx');

Now we can access an object whose properties (keys) are sheet names from the workbook.
var sheets = workbook.Sheets;

We can also access an array of strings called SheetNames. Each string is the name of the sheet.
var sheetNames = workbook.SheetNames;

Finally, we can put it all together and access specific spreadsheet from the supplied excel file. For this example I will just access the first sheet.
var sheetName = workbook.SheetNames[0];
var sheet = workbook.Sheets[sheetName ];

So if you want to read multiple sheets you can use the list of sheet names (workbook.SheetNames) and the sheets object (workbook.Sheets). I would use a for-loop to go through each sheet name in the SheetNames array, then access the Sheets object by passing in the sheet name to get the sheet.
for (var i = 0; i < workbook.SheetNames.length; ++i) {
var sheet = workbook.Sheets[workbook.SheetNames[i]];
}

Hopefully this helps. You can refer to the documentation as well.

All 4 comments

Yes, the default behavior of the XLSX module is to return a workbook object that has a "Sheets" property.

First let's get the workbook object.
var workbook = XLSX.readFile('test.xlsx');

Now we can access an object whose properties (keys) are sheet names from the workbook.
var sheets = workbook.Sheets;

We can also access an array of strings called SheetNames. Each string is the name of the sheet.
var sheetNames = workbook.SheetNames;

Finally, we can put it all together and access specific spreadsheet from the supplied excel file. For this example I will just access the first sheet.
var sheetName = workbook.SheetNames[0];
var sheet = workbook.Sheets[sheetName ];

So if you want to read multiple sheets you can use the list of sheet names (workbook.SheetNames) and the sheets object (workbook.Sheets). I would use a for-loop to go through each sheet name in the SheetNames array, then access the Sheets object by passing in the sheet name to get the sheet.
for (var i = 0; i < workbook.SheetNames.length; ++i) {
var sheet = workbook.Sheets[workbook.SheetNames[i]];
}

Hopefully this helps. You can refer to the documentation as well.

As @harbhub pointed out, the standard XLSX.read and XLSX.readFile functions will read every worksheet by default. The names are available in the workbook's SheetNames property and the actual sheet objects are in the Sheets object.

how to pass the name of sheet if multiple sheet available in workbook

I have the same issue that "How to pass the name and id of the sheet if multiple sheets available in the workbook?" @MarziyaSarwar if you find the solution please help me to solve this...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HachimDev picture HachimDev  路  3Comments

dullin picture dullin  路  3Comments

nishthasb picture nishthasb  路  4Comments

magtuan picture magtuan  路  3Comments

gustavosimil picture gustavosimil  路  3Comments