简体   繁体   English

无法使用PHPExcel读取旧的file.xls excel文件

[英]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 . 我的代码可与file.xlsx完美配合,但无法使其与旧的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. 我尝试了找到的每个代码,但没有任何效果。我尝试将Excel2007的版本更改为2003,2004,但是什么也没有。。。不知道还要尝试什么。
Here is my current code (working with file.xlsx but I need to work with file.xls too). 这是我当前的代码(使用file.xlsx但我也需要使用file.xls )。

 <?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. 用于xls文件的Reader是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. 但是您实际上应该只使用IOFactory的load()方法,然后让PHPExcel自己识别文件类型并选择正确的阅读器,因为很多扩展名为.xls的文件(尤其是从Internet下载的文件)并不是真正的BIFF格式的文件。...使用load()方法时,PHPExcel可以识别并正确加载这些文件,因为它可以自行选择正确的Reader。

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. 但是,当您自己指定Excel2007Excel5类的Reader时,就是在告诉PHPExcel它必须使用哪个Reader,即使它可能不是实际文件的正确Reader。

Document link , which lists all of the different Readers by name 文档链接 ,按名称列出所有不同的Reader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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