在PHPSReadSheet中读取Xlsx文件

我想读取在Microsoft Excel中创建的xlsx文件,但当我运行以下代码时

$Source\u File=“test.xlsx”;
$Spreadsheet=\PhpOffice\PhpSpreadsheet\IOFactory::load($Source\u File);

…我收到以下错误:

致命错误:未捕获PhpOffice\PhpSpreadsheet\Reader\Exception:无法在/var/www/html/vendor/PhpOffice/PhpSpreadsheet/src/PhpSpreadsheet/IOFactory.php:163中识别此文件的读取器
堆栈跟踪:
#0/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/IOFactory.php(93):phpoffice\phpspreadsheet\IOFactory::createReaderForFile('file:///home/ar...')
#1/var/www/html/Function_Spreadsheet.php(480):PhpOffice\PhpSpreadsheet\IOFactory::load('file:///home/ar...')
#2/var/www/html/Function_Home.php(3747):电子表格读取器_1('/var/www/html/F...,3745,数组,数组)
#3{main}在第163行的/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/IOFactory.php中抛出

如果改用$Spreadsheet=IOFactory::load($Source\u File),则会出现相同的错误

如果改用$Spreadsheet=$reader->加载($Source_文件)

警告:ZipArchive::getFromName():第311行/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/Reader/Xlsx.php中的Zip对象无效或未初始化
警告:ZipArchive::getFromName():第313行/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/Reader/Xlsx.php中的Zip对象无效或未初始化
注意:尝试在第350行的/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/Reader/Xlsx.php中获取非对象的属性“Relationship”
警告:第350行/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/Reader/Xlsx.php中为foreach()提供的参数无效
警告:ZipArchive::getFromName():第311行/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/Reader/Xlsx.php中的Zip对象无效或未初始化
警告:ZipArchive::getFromName():第313行/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/Reader/Xlsx.php中的Zip对象无效或未初始化
注意:试图在第397行的/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/Reader/Xlsx.php中获取非对象的属性“Relationship”
警告:为第397行/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/Reader/Xlsx.php中的foreach()提供的参数无效
警告:ZipArchive::getFromName():第311行/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/Reader/Xlsx.php中的Zip对象无效或未初始化
警告:ZipArchive::getFromName():第313行/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/Reader/Xlsx.php中的Zip对象无效或未初始化
注意:试图在第1855行的/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/Reader/Xlsx.php中获取非对象的属性“Override”
警告:为第1855行/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/Reader/Xlsx.php中的foreach()提供的参数无效
警告:ZipArchive::close():第1883行/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/Reader/Xlsx.php中的Zip对象无效或未初始化

该文件是可读的,并且可以通过我的PHPV7.2脚本在Ubuntu 18.04上的Apache中打开。我读了几篇论坛帖子,其中提出了以下建议,我已经做到了:

  • 安装所需的扩展并重新启动apache
  • 检查文件权限

我尝试在LibreOffice中打开文件并将其保存为xlsx,但出现了相同的错误(如果保存为xls,则没有错误)

我可以创建一个读卡器$reader=new\PhpOffice\PhpSpreadsheet\reader\Xlsx(),但当我这样做时,$Spreadsheet=$reader->加载($Source_文件)$Spreadsheet=IOFactory::load($Source\u文件)我得到了相同的错误

此外,我还可以创建一个xls阅读器,它可以读取xls文件。我还可以创建一个xlsx读取器,但它不会读取xlsx文件,在尝试读取xlsx文件时会出现相同的错误那么,为什么xlsx文件会出现错误?

另外,我阅读了错误消息指向的源代码(IOFactory.php),找到了错误发生的以下位置(第139行附近)

//让我们看看我们是否幸运
if(isset($reader)&$reader->canRead($filename))
{
返回$reader;
}

…我搜索了canRead的定义,但在/vendor/phpoffice/phpspreadsheet/中没有找到它哪里定义了canRead我想如果我能读一下canRead的定义,也许我就能理解问题的根本原因

更新:

我从评论和讨论中了解到,canRead()在从第65行开始的\PhpSpreadsheet\Reader\Xlsx.php中定义。在canRead()$zip->open($pFilename)返回一个错误代码,ZipArchive::ER\u NOENT,意思是“没有这样的文件”。但是,该文件存在那么,为什么会发生此错误

更新-2018-12-18

此网页表明存在多种类型的xlsx文件。因此,我运行了文件test.xlsx,它显示了MicrosoftExcel2007+。然后,我在LibreOffice Calc中打开了电子表格,并将其保存为OOXML类型的xlsx文件,然后重新运行文件test.xlsx,其中显示了Microsoft OOXML。然后我重新运行了PHP脚本,但得到了相同的错误。因此,似乎我的xlsx文件类型不是问题所在

因此,我决定使用PHPExcel(尽管它已被弃用)来完成一些必要的工作。当我使用PHPExcel运行脚本时,我收到了一个类似的错误,关于canRead()无法检测xlsx文件

因此,我继续阅读本网页,并遵循了wesood的最后一条建议,该建议来源于本网页上的公认答案。这个解决方案对我有效:在文件/PHPExcel/IOFactory.php中,我添加了PHPExcel\u设置::setZipClass(\PHPExcel\u设置::PCLZIP)紧跟在if(isset($reader)&$reader->canRead($filename))之前

然而,我仍然想知道如何在PHPSReadSheet中解决这个问题。似乎我需要更多地了解pclzip是如何工作的,以及是否需要对PHPSReadSheet执行类似的操作

更新2019-02-10:

我今天试着运行脚本,似乎添加了PHPExcel\u设置::setZipClass(\PHPExcel\u设置::PCLZIP)不再工作。所以,我又被卡住了

我做错了什么?欢迎任何帮助

更新2019-02-18:

根据评论中的建议,我使用通过Google搜索结果(例如,此文件)找到的随机XLSX文件测试了脚本,这些文件是Excel 2007+Microsoft OOXML类型,PHPSReadSheet显示相同的错误:

致命错误:未捕获的PhpOffice\PhpSpreadsheet\Reader\Exception:
无法在中标识此文件的读取器
/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/IOFactory.php:176
堆栈跟踪:#0
/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/IOFactory.php(113):
PhpOffice\PHPSReadSheet\IOFactory::createReaderForFile('file:///var/www...”)#1/var/www/html/Function_Spreadsheet.php(798):PhpOffice\PhpSpreadsheet\IOFactory::identify('file:///var/www...') #2
/var/www/html/Function_Home.php(3748):
电子表格阅读器1('/var/www/html/F..,3746,数组,数组)#3
{main}在第176行的/var/www/html/vendor/phpoffice/phpspreadsheet/src/phpspreadsheet/IOFactory.php中抛出

据我所知,你少了一块。为什么不先创建一个读卡器,然后加载文件呢

请尝试以下代码它可以识别扩展并相应地创建该类型的读卡器。

$inputFileName="Text.xlsx“;;
/**标识$inputFileName的类型**/
$inputFileType=\PhpOffice\PhpSpreadsheet\IOFactory::identify($inputFileName);
/**创建已标识类型的新读取器**/
$reader=\PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/**将$inputFileName加载到电子表格对象**/
$spreadsheet=$reader->加载($inputFileName);
/**将电子表格对象转换为数组以便于使用**/
$schdeules=$spreadsheet->getActiveSheet()->toArray();

现在,您只需在结果数组上运行foreach循环

foreach($schdeules作为$single\u计划)
{               
回显“<div class=”行“>”;
foreach($single_计划作为$single_项目)
{
回显“<p class=”项“.$single”项“</p>””;
}
回音“</div>”;
}

这是经过测试和运行的代码

发表评论