简体   繁体   中英

Can't read old file.xls excel file with PHPExcel

My code works perfect with file.xlsx but I can't make it work with old file.xls . I tried every code I found and nothing works... I tried change the version of Excel2007 to 2003,2004 but nothing... Don't know what else to try.
Here is my current code (working with file.xlsx but I need to work with file.xls too).

 <?php 
 function load_table(){
    require_once('Classes/PHPExcel.php');

    $objReader = PHPExcel_IOFactory::createReader('Excel2007');
    $objReader->setReadDataOnly(false);

    $objPHPExcel = $objReader->load("SampleData.xlsx");
    $objWorksheet = $objPHPExcel->getActiveSheet();
    $highestRow = $objWorksheet->getHighestRow(); // e.g. 10
    $highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'

    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5

    echo '<table class="table">' . "\n";
    for ($row = 1; $row <= $highestRow; ++$row) {
      echo '<tr>' . "\n";

      for ($col = 0; $col <= $highestColumnIndex; ++$col) {
        echo '<td>';
        $first =   $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
        if($first[0] == '='){

            echo $objWorksheet->getCellByColumnAndRow($col, $row)->getCalculatedValue();
        }
        else
            echo $first;

        echo '</td>' . "\n";

      }

      echo '</tr>' . "\n";
    }
    echo '</table>' . "\n"; 
}

?>

The Reader for xls files is the Excel5 Reader.

But you really should simply use the IOFactory's load() method, and let PHPExcel identify the filetype for itself and select the correct reader, because a lot of files (particularly those downloaded from the internet) that have an .xls extension aren't really BIFF format files.... PHPExcel can identify and load these correctly when using the load() method, because it can pick the correct Reader for itself.

But when you specify a Reader such as Excel2007 or Excel5 yourself, you're telling PHPExcel which Reader it must use, even though it may not be the correct Reader for the actual file.

Document link , which lists all of the different Readers by name

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