Getting 'Active sheet index is out of bounds.' error

Topics: User Forum
Oct 17, 2012 at 5:46 PM

I am using Windows XP SP3 with PHPExcel 1.76.

I am writing an application that reads in an XLS file that was created by another program.  Whenever I get to the line:

$objWorksheet = $objPHPExcel->setActiveSheetIndex('0');

it errors out on me with this error: "PHP Fatal error:  Uncaught exception 'Exception' with message 'Active sheet index is out of bounds.'"

After much tinkering, I found that if I open that XLS file in Excel and resave it, the application will have no problem with the file.  I noticed that it does add a few KB when I resave it, so it must be changing something.  Any ideas of how I can get around this?  I tried to open and save it using PHPExcel, but it corrupted the file instead.  Here is the code I tried to do that with.

$objPHPExcel = $objReader->load($filename);
	try {
		$objWorksheet = $objPHPExcel->setActiveSheetIndex('0');
	} catch(Exception $e) {
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objPHPExcel = $objReader->load($filename);
		$objWorksheet = $objPHPExcel->setActiveSheetIndex('0');

Any help would be appreciated.



Oct 17, 2012 at 5:50 PM

One idea might be to upgrade to PHPExcel 1.7.8

Oct 17, 2012 at 7:18 PM

Bummer, that didn't do it.  Any other thoughts?


Oct 17, 2012 at 10:03 PM

If that doesn't work, then I have no ideas.... 1.7.8 fixed one known bug that triggered that particular error.

Besides the fact that the active sheet index should be an integer (though using a string containing a 0 shouldn't cause this error)

Can you try dumping the results of an array_keys($objPHPExcel->getAllSheets()); and $objPHPExcel->getActiveSheet(); immediately after the load.