简体   繁体   中英

PHPExcel XLS not recognised as an OLE file

I developed a PHP web application using PHPExcel libraries. All is working properly except with some Excel documents which strangely cannot be recognized as an Excel doc. When I open these files with Microsoft Excel and save them as xls, csv, xlsx, then everything works well. So... I think some Excel files are not exactly what their extension says. Perhaps they are disguised XML? Is it possible to get XML support with PHPExcel?

Here's the error:

Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'The filename /tmp/phphrnIR2 is not recognised as an OLE file' in /home/www/text/excel/reader/Classes/PHPExcel/Shared/OLERead.php:89 Stack trace: #0 /home/test/excel/reader/Classes/PHPExcel/Reader/Excel5.php(1164): PHPExcel_Shared_OLERead->read('/tmp/phphrnIR2') #1 /home/www/text/excel/reader/Classes/PHPExcel/Reader/Excel5.php(612): PHPExcel_Reader_Excel5->_loadOLE('/tmp/phphrnIR2') #2 /home/www/test/excel/actions.php(60): PHPExcel_Reader_Excel5->load('/tmp/phphrnIR2') #3 /home/www/test/excel/index.php(77): include_once('/home/www/test/ex...') #4 {main} thrown in /home/www/test/excel/reader/Classes/PHPExcel/Shared/OLERead.php on line 89

And the php code for file recognition:

 $name     = $_FILES['file']['name'];
                    $tname    = $_FILES['file']['tmp_name'];
                    $type     = $_FILES['file']['type'];

                    if($type == 'application/vnd.ms-excel')
                    {
                            // Excel 97 extension
                            $ext = 'xls';
                    }
                    else if($type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
                    {
                            // Excel 2007 and 2010 extension
                            $ext = 'xlsx';
                    }


                    else if($type == 'text/csv')
                    {
                            // Excel CSV extension
                            $ext = 'csv';


                    }else{

                            // Invalid Extension   
                    ?> 
                            <script languaje="javascript">
                                  swal("Bad file...", "A valid extension must be XLS, XLSX o CSV!", "error");
                            </script> 
                            <?php
                            exit();
                    }
 //reader creation
                    $objReader = PHPExcel_IOFactory::createReader($$ext);

                    //uploading file
                    $objPHPExcel = $objReader->load($tname);


                    $dim = $objPHPExcel->getActiveSheet()->calculateWorksheetDimension();

Well, despite the fact that it has a .xls extension, it's entirely possible that it isn't a BIFF-format xls file.

A lot of people give a file an xls extension when it's a csv format file, or even HTML markup..... never trust the extension; and as the mime type will be based on the file extension, you can't take that at face value either.

Rather than specifically creating a Reader based on the extension/mime type.... use the PHPExcel IOFactory identify() method to see what file format PHPExcel believes the file to be. Or call the IOFactory's load() method so that it can try to identify the file format for itself.

Clearly this file isn't BIFF-format (or was created using a very early version of the BIFF-format, before version 5), otherwise it would be recognised as an OLE file

I came across the same problem you have faced, anonymous user. This is how I fixed it:

Have PHPExcel discover the type

Load quickly and simply

require_once('Classes'.DIRECTORY_SEPARATOR.'PHPExcel.php');

// Tell PHPExcel to load this file and make its best guess as to its type.
$objPHPExcel = PHPExcel_IOFactory::load($_FILES['uploaded_file']['tmp_name']);

Load with options

require_once('Classes'.DIRECTORY_SEPARATOR.'PHPExcel.php');

// Tell PHPExcel that you will be loading a file.
$objReader = PHPExcel_IOFactory::createReaderForFile($_FILES['uploaded_file']['tmp_name']);

// Set your options.
$objReader->setReadDataOnly(true);

// Tell PHPExcel to load this file and make its best guess as to its type.
$objPHPExcel = $objReader->load($_FILES['uploaded_file']['tmp_name']);

source

Now that you have a PHPExcel object you can work with the worksheets this way:

    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
        $worksheets[] = $worksheet->toArray();
    }

    foreach($worksheets as $sheet) {

        foreach($sheet as $row) {
            print_r($row);
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM