'PHPExcel sheet index out of bounds exception
I'm using PHPExcel to read an xlsx workbook, the following is the code I'm using so far just to set the active sheet
date_default_timezone_set('Europe/London');
set_include_path(get_include_path() . PATH_SEPARATOR . './Classes/');
include 'PHPExcel/IOFactory.php';
$reader = PHPExcel_IOFactory::createReader('Excel2007');
$reader->setReadDataOnly(true);
$xlWB = $reader->load('/' . $target_file);
$xlWB->setActiveSheetIndex(0); //EXCEPTION OCCURS HERE
but I'm getting an exception:
Fatal error: Uncaught exception 'PHPExcel_Exception' with message 'You tried to set a sheet active by the out of bounds index: 0. The actual number of sheets is 0.' in /[my root]/Classes/PHPExcel.php:695 Stack trace: #0 /[my-root]/conversion-demo.php(211): PHPExcel->setActiveSheetIndex(0) #1 {main} thrown in /[my root]/Classes/PHPExcel.php on line 695
The file is an .xlsx file, it does exist (the path is correct) and there are 3 sheets in the file.
Am I doing something fundamentally wrong here?
Solution 1:[1]
Check the permissions on where you're getting the file from - I just had this problem on a file upload and changing the permissions in Windows\Temp fixed it.
Solution 2:[2]
Try to upload file manually.
Had the same issue right now. Excel template was commited in git repository, and git changed sheet somehow. So, I uploaded local version to server - now it works.
Better way: this may be resolved with .gitattributes file, just write this *.xls binary there, rename file and recommit
Solution 3:[3]
Just in case someone get this error when using $reader->setLoadSheetsOnly($sheetName$); to get only a specific sheet of the workbook, double check the sheet name 'cause the it is case-sensitive.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dan B |
| Solution 2 | |
| Solution 3 | sdlins |
