Get all Sheets in Workbook

Topics: Developer Forum, User Forum
Feb 19, 2009 at 12:02 PM
Is is possible to read all the sheets that are in a xls file?

My users have exel files with multiple sheets in them. If they upload the file to the intranet it is the idea to have the scripts ask them what Sheet they want to make show.
This i want to do by having (if possible) phpexcel returns all the sheets in  a workbook.


Developer
Feb 19, 2009 at 12:27 PM
Edited Feb 19, 2009 at 12:30 PM
This is certainly possible, and in fact all sheets are loaded by default when you read an Excel file with PHPExcel.

You can access sheet by name:

$sheet = $objPHPExcel->getSheetByName('Sheet1');

by index (0-based). (Throws exception if sheet does not exist.)

$sheet = $objPHPExcel->getSheet(0);

or loop through sheets in a familiar foreach loop

foreach ($objPHPExcel->getWorksheetIterator() as $sheet) {
   ...
}

The last will be working with forthcoming PHPExcel 1.6.6, but you can already now try it out by downloading latest source code.