This is:
- [X] a bug report
$filePath = __DIR__ . "/data.xls";
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($filePath);
$spreadSheet = $reader->load($filePath);
$dataAsAssocArray = $spreadSheet->getActiveSheet()->toArray();
Should read the the active sheet into an array
Throws and excpetion with message : Unable to identify a reader for this file in -[REMOVED]/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/IOFactory.php on line 153
See what is expected behaviour . Use a xls file .
<?php
require_once __DIR__ . "/vendor/autoload.php";
$filePath = __DIR__ . "/data.xls";
try {
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($filePath);
$spreadSheet = $reader->load($file);
$dataAsAssocArray = $spreadSheet->getActiveSheet()->toArray();
}
catch (\Exception $exception){
//exception occurs
}
// Trying the same thing with the predecessor library PHPExcel produces no error at all.
$oldReader = PHPExcel_IOFactory::createReaderForFile($filePath);
$oldSpreadSheet = $oldReader->load($filePath);
$dataAsAssocArray = $oldSpreadSheet->getActiveSheet()->toArray();
Packages used.
{
"require": {
"phpoffice/phpspreadsheet": "^1.2",
"phpoffice/phpexcel": "^1.8"
}
}
The canRead method of Xls class returns false in the new library while same method returns true in the old library that uses Excel5 class for the xls files.
possibly related to issue #402
The same code is giving the 500 error on my ubuntu machine. The $reader->load("example2.xls"); line giving error. Any idea why I am getting such error?
Maybe you can check your php logs and post here as well as the code that you are executing ...
<?php
require 'autoload.php';
try{
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xls');
// print_r($reader);
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load("example2.xls");
$worksheet = $spreadsheet->getActiveSheet();
echo '<table>' . PHP_EOL;
foreach ($worksheet->getRowIterator() as $row) {
echo '<tr>' . PHP_EOL;
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(FALSE);
foreach ($cellIterator as $cell) {
echo '<td>' .
$cell->getValue() .
'</td>' . PHP_EOL;
}
echo '</tr>' . PHP_EOL;
}
echo '</table>' . PHP_EOL;
}
catch(Exception $e)
{
print_r($e);
}
Here is my code. If I print the $reader object, it is printing but code is giving 500 Error.
The autoload.php is in vendor directory unless you run this code inside the vendor directory itself which is not the case i guess.
I got the solution. It was issue of the extensions.
Thanks @banstola for your support
@salman-nadsoft , what's the solution?
what did you do to solve this problem?
I have same problem..
thank you..
@ipzelhakim , There are some extensions required by this library like xml, mbstring etc. That extensions was not there in my php. I have installed those extensions and it worked. You will get the required extension list here: https://phpspreadsheet.readthedocs.io/en/develop/
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.
@banstola I have the extensions installed and am using require "vendor/autoload.php"; and I still get the same exception thrown. Any idea what I could do to fix this?
@arya108 Heya, I think I got the same issue as you have (been seeing your username for the last hour or so lol) if you find the answer, would you mind tagging me? I ll do the same of course
have a nice day
edit : heya again @arya108, it was an error in my dataset (that comes from a work related app) something went wrong in a line. removing the affected characters solved my issue. hope this helps
Most helpful comment
Here is my code. If I print the $reader object, it is printing but code is giving 500 Error.