如何解决错误 - 错误加载文件" Excel2007":无法打开Excel2007进行阅读!使用PHPExcelFile读取excel文件时?

时间:2014-11-04 13:05:27

标签: php excel

  

myfile.xlsx是要打开的文件的名称。我使用过PHPExcel   库(一个非常常见的库),但我找不到代码   读取excel文件并将其显示在句子或browser.Plz中   帮助。

try {
        $inputFileType = PHPExcel_IOFactory::identify('myfile.xlsx');
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcel = $objReader->load($inputFileType);
    } catch (Exception $e) {
        die('Error loading file "' . pathinfo($inputFileType, PATHINFO_BASENAME) . '": ' . $e->getMessage());
    }

    $rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
    $array_data = array();
    foreach ($rowIterator as $row) {
        $cellIterator = $row->getCellIterator();
        $cellIterator->setIterateOnlyExistingCells(false);
        $rowIndex = $row->getRowIndex();
        $array_data[$rowIndex] = array('A' => '', 'B' => '', 'C' => '', 'D' => '', 'E' => '', 'F' => '', 'G' => '');

        foreach ($cellIterator as $cell) {
            if ('A' == $cell->getColumn()) {
                $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
            } else if ('B' == $cell->getColumn()) {
                $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
            } else if ('C' == $cell->getColumn()) {
                $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
            } else if ('D' == $cell->getColumn()) {
                $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
            } else if ('E' == $cell->getColumn()) {
                $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
            } else if ('F' == $cell->getColumn()) {
                $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
            } else if ('G' == $cell->getColumn()) {
                $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
            }
        }}

1 个答案:

答案 0 :(得分:1)

当您的文件被调用Excel2007

时,请勿尝试打开名为$inputFileType的文件(myfile.xlsx的值)
$objPHPExcel = $objReader->load('myfile.xlsx');