0

Trying to read from a XLSX file with PhpSpreadsheet however getting below error:

PHP Fatal error:  Uncaught PhpOffice\PhpSpreadsheet\Exception: Your requested sheet index: -1 is out of bounds. The actual number of sheets is 0. in C:\php\projects\stock\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Spreadsheet.php:678

There is definitely a worksheet and my code below to pull a single cell works fine when I remove the table and just have unformatted data.

$spreadsheet = new PhpOffice\PhpSpreadsheet\Spreadsheet();

$inputFileName = "path/to/temp_file_download.xlsx";
$inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($inputFileName);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$spreadsheet = $reader->load($inputFileName);
echo $cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 2)->getValue();

It seems that saving the file with Excel allows it to work just fine however excel does not complain that it is corrupt etc. I have no control over the actual spreadsheet as generated from an external source.

Just wondering if anyone has had similar issues and have any ideas for working around or what I may be doing wrong?

Tried to use the older PhpExcel also to get this to work however similar issues occur until I save the file again.

Have also tried XLSXReader with similar issues:

PHP Fatal error:  Uncaught Exception: File /xl/workbook.xml does not exist in the Excel file in C:\php\projects\stock\vendor\XLSXReader\XLSXReader.php:57
Stack trace:
#0 C:\php\projects\stock\vendor\XLSXReader\XLSXReader.php(70): XLSXReader->getEntryData()
#1 C:\php\projects\stock\vendor\XLSXReader\XLSXReader.php(47): XLSXReader->parse()
#2 C:\php\projects\stock\stock.php(17): XLSXReader->__construct()
#3 {main}
  thrown in C:\php\projects\stock\vendor\XLSXReader\XLSXReader.php on line 57

And have seen many issues on web for people having issues reading these files that area created like so however still not sure of a simple workaround.

harri
  • 494
  • 11
  • 26

1 Answers1

0

Adding the workaround mentioned below hack for non standard xlsx seemed to do the trick!

https://github.com/PHPOffice/PHPExcel/issues/1187

Just need to add the class extending the xlsx class to phpspreadsheet then call that class instead and all works!

harri
  • 494
  • 11
  • 26