如何解决加载文件“Excel2007”的错误错误错误:无法打开Excel2007阅读!使用PHPExcelFile读取excel文件时?

时间:2021-01-23 21:51:02

myfile.xlsx is the name of file to be opened.I have used PHPExcel library(a very common library) but i could not not find the code to read the excel file and display it into sentence or in browser.Plz help .

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

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 个解决方案

#1


1  

Don't try to open a file called Excel2007 (the value of $inputFileType) when your file is called myfile.xlsx

当您的文件名为myfile.xlsx时,不要尝试打开名为Excel2007的文件($inputFileType的值)。

$objPHPExcel = $objReader->load('myfile.xlsx');

#1


1  

Don't try to open a file called Excel2007 (the value of $inputFileType) when your file is called myfile.xlsx

当您的文件名为myfile.xlsx时,不要尝试打开名为Excel2007的文件($inputFileType的值)。

$objPHPExcel = $objReader->load('myfile.xlsx');