setLoadSheetsOnly with sheet index instead of name

Topics: User Forum
Mar 8, 2012 at 4:36 PM


I needed a way to load the first sheet only of an Excel2007 file, without knowing its name, so I've changed this single line of code in Reader/Excel2007.php (line 611 in PHPExcel 1.7.6):

// Check if sheet should be skipped
if (isset($this->_loadSheetsOnly) && (
!in_array((string) $eleSheet["name"], $this->_loadSheetsOnly)
: !in_array($oldSheetId, $this->_loadSheetsOnly)
)) {

I.E.: check if setLoadSheetsOnly was called with a string parameter. If it was, compare names. Otherwise compare sheet indexes.

This should work (haven't tested thoroughly). I've posted this patch in case someone else faces the same problem.

Mar 8, 2012 at 5:42 PM

The other alternative is to get the list of names before loading the workbook using the reader's listWorksheetNames() method, and then pass the first entry in that list of names to setLoadSheetsOnly()