Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IOFactory::createReaderForFile throws an exception for xls file #408

Closed
greensunrise opened this issue Mar 7, 2018 · 10 comments
Closed

Comments

@greensunrise
Copy link

greensunrise commented Mar 7, 2018

This is:

- [X] a bug report

What is the expected behavior?

    $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

What is the current behavior?

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

What are the steps to reproduce?

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();

Which versions of PhpSpreadsheet and PHP are affected?

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

@salman-nadsoft
Copy link

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?

@greensunrise
Copy link
Author

greensunrise commented Apr 3, 2018

Maybe you can check your php logs and post here as well as the code that you are executing ...

@salman-nadsoft
Copy link

salman-nadsoft commented Apr 3, 2018

<?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.

@greensunrise
Copy link
Author

The autoload.php is in vendor directory unless you run this code inside the vendor directory itself which is not the case i guess.

@salman-nadsoft
Copy link

salman-nadsoft commented Apr 3, 2018

I got the solution. It was issue of the extensions.
Thanks @banstola for your support

@ipzelhakim
Copy link

@salman-nadsoft , what's the solution?
what did you do to solve this problem?

I have same problem..

thank you..

@salman-nadsoft
Copy link

@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/

@stale
Copy link

stale bot commented Jun 12, 2018

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.

@stale stale bot added the stale label Jun 12, 2018
@arya108
Copy link

arya108 commented Feb 11, 2019

@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?

@machinchose
Copy link

machinchose commented Feb 21, 2019

@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

@oleibman oleibman removed the stale label Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

6 participants