我想读取在Microsoft中创建的xlsx文件,但是当我运行以下代码时.
$Source_File = "test.xlsx";
$Spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($Source_File);...I收到以下错误:
Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Reader\Exception: Unable to identify a reader for this file in /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/IOFactory.php:163
Stack trace:
#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): Spreadsheet_Reader_1('/var/www/html/F...', 3745, Array, Array)
#3 {main} thrown in /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/IOFactory.php on line 163如果我使用$Spreadsheet = IOFactory::load($Source_File);,我会得到同样的错误
如果我使用$Spreadsheet = $reader->load($Source_File);,我会得到以下错误
Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 311
Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 313
Notice: Trying to get property 'Relationship' of non-object in /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 350
Warning: Invalid argument supplied for foreach() in /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 350
Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 311
Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 313
Notice: Trying to get property 'Relationship' of non-object in /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 397
Warning: Invalid argument supplied for foreach() in /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 397
Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 311
Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 313
Notice: Trying to get property 'Override' of non-object in /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 1855
Warning: Invalid argument supplied for foreach() in /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 1855
Warning: ZipArchive::close(): Invalid or uninitialized Zip object in /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 1883我的PHP v7.2脚本在Ubuntu18.04上的Apache中,这个文件是可读的和可打开的。我读了几篇论坛文章,其中提出了以下几点,我已经这样做了:
我尝试在LibreOffice中打开该文件并将其保存为xlsx,但也会发生相同的错误(如果我将文件保存为xls,则没有错误)。
我可以创建一个读取器$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();,但是当我执行$Spreadsheet = $reader->load($Source_File);或$Spreadsheet = IOFactory::load($Source_File);时,也会得到相同的错误。
此外,我还可以创建一个xls读取器,它可以读取xls文件。我也可以创建一个xlsx读取器,但是它不会读取xlsx文件,在尝试读取xlsx文件时会出现同样的错误。那么,为什么xlsx 文件会出现错误呢?
此外,我还阅读了错误消息指向的源代码(IOFactory.php),并找到了发生错误的以下位置(第139号行附近).
//Let's see if we are lucky
if (isset($reader) && $reader->canRead($filename))
{
return $reader;
}...and我搜索了canRead的定义,但是在/vendor/phpoffice/phpspreadsheet/中没有找到它。canRead 定义在哪里?--如果我能读懂canRead的定义,也许我会理解问题的根本原因。
更新:
我从评论和讨论中了解到,canRead()是从第65行开始在\PhpSpreadsheet\Reader\Xlsx.php中定义的。在canRead()中,$zip->open($pFilename)返回一个错误代码ZipArchive::ER_NOENT,意思是"没有这类档案“。但是,文件是存在的。那么,为什么这个错误会发生呢?
更新- 2018-12-18
这个网页建议存在多种类型的xlsx文件。所以,我运行了file test.xlsx,它显示了Microsoft Excel 2007+。然后,我在LibreOffice Calc中打开了电子表格,并将它保存为一个OOXML类型的xlsx文件,并重新运行了显示Microsoft OOXML的file test.xlsx。然后我重新运行PHP脚本,但是得到了同样的错误。因此,我的xlsx文件类型似乎不是问题所在。
因此,我决定使用PHPExcel (尽管不推荐)来完成一些必要的工作。当我使用PHPExcel运行脚本时,我收到了一个类似的错误,即canRead()无法检测到xlsx文件。
因此,我继续阅读这个网页,并遵循了wesood的最后一个建议,该建议来源于这个网页上公认的答案。这个解决方案对我有用:在文件/PHPExcel/IOFactory.php中,我在if (isset($reader) && $reader->canRead($filename))之前添加了PHPExcel_Settings::setZipClass(\PHPExcel_Settings::PCLZIP);。
但是,我仍然想知道如何在PhpSpreadsheet中解决这个问题。我似乎需要更多地了解pclzip是如何工作的,以及是否需要对PhpSpreadsheet执行类似的操作。
更新2019-02-10:
我今天试着运行这个脚本,似乎添加的PHPExcel_Settings::setZipClass(\PHPExcel_Settings::PCLZIP);不再有效。所以我又被困住了..。
,我做错了什么?我欢迎任何帮助!
更新2019-02-18:
根据注释中的建议,我使用通过谷歌搜索结果(例如这个文件)找到的随机XLSX文件对脚本进行了测试,这些文件要么是Excel 2007+类型,要么是Microsoft OOXML类型,以及用于PhpSpreadsheet的相同错误显示:
致命错误: Uncaught \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\PhpSpreadsheet\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_html.var(3748):Spreadsheet_Reader_1(‘/var/www/html/f.’,在第176行的/var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/IOFactory.php中抛出#3 {main}
发布于 2019-02-18 07:49:23
据我所知,你少了一块。为什么不先创建一个读取器,然后加载文件。
试试下面的代码。它可以识别扩展并相应地创建该类型的读取器.
$inputFileName = "Text.xlsx";
/** Identify the type of $inputFileName **/
$inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($inputFileName);
/** Create a new Reader of the type that has been identified **/
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $reader->load($inputFileName);
/** Convert Spreadsheet Object to an Array for ease of use **/
$schdeules = $spreadsheet->getActiveSheet()->toArray();现在,您可以简单地在结果数组上运行一个foreach循环。
foreach( $schdeules as $single_schedule )
{
echo '<div class="row">';
foreach( $single_schedule as $single_item )
{
echo '<p class="item">' . $single_item . '</p>';
}
echo '</div>';
}这是测试和工作代码。
发布于 2020-04-09 21:12:47
在将.xlsx文件添加到Mac上的git存储库后,我也遇到了同样的问题。
问题是git自动转换了线路的结尾。
解决方案是将这些行添加到.gitattributes文件中:
*.xls binary
*.xlsx binary发布于 2019-03-31 03:41:53
在试图加载XLSX文件时,我也遇到了同样的错误。对我个人来说,我发现了一个非常简单的解决方案,解决了我的问题。我正在手动抓取文件名为xlsx的扩展名。我注意到我使用旧的PHP电子表格库使用扩展Xls的其他一些代码。所以我试着用Xlsx加载,它工作得很好。
下面是我用来正确加载扩展中的代码。它只需在最后一个句点之后抓取所有字符,然后将该子字符串的第一个字符括起来。ucfirst只是简单地大写字符串的第一个字母传递给它。substr返回一个子字符串,其中第一个参数是要从中获取的字符串,第二个参数是在给定字符串中启动子字符串的索引。最后,strrpos会在给定字符串中找到子字符串的最后一次出现。
https://www.php.net/manual/en/function.ucfirst.php
https://www.php.net/manual/en/function.strrpos
https://www.php.net/manual/en/function.substr.php
$inputFileType = ucfirst(substr($cccFile, strrpos($cccFile, '.') + 1));
/** Create a new Reader of the type defined in $inputFileType **/
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);有一次,我添加了ucfirst命令,它为我解决了这个问题。
https://stackoverflow.com/questions/53823233
复制相似问题