首页 > 解决方案 > PHPSpreadsheet 不适用于 PHP 版本 7.3.26

问题描述

我已经在 7.2 上对此进行了测试,并且可以正常工作。当我将此代码移至 7.3 时,它不起作用。我在不使用作曲家的情况下安装了 PHPSpreadsheet

<?php
namespace PhpOffice;
include ".\PhpOffice\autoload.php";
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color;

$htmlString = "<p>hello</p>";
$fileName = "myExcel";
$CntDisposition = "Content-Disposition: attachment;filename=";
$CntDisposition = $CntDisposition . $fileName . ".xls";
header($CntDisposition);
header('Cache-Control: max-age=0');
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Html();
$spreadsheet = $reader->loadFromString($htmlString);
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('php://output');
?>

失败并出现以下错误, 在此处输入图像描述

当我ini_set('display_errors', 1); error_reporting(E_ALL);在命名空间之前添加它时,它以 500 失败。但是当我在命名空间行下方添加它时,文件被下载了。但是在文件上有followin stacktrack,

<br />
<b>Warning</b>:  include(.\PhpOffice\autoload.php): failed to open stream: No such file or directory in <b>/home/u527331572/domains/thestory.host/public_html/adminPanel/adminPages/excel.php</b> on line <b>8</b><br />
<br />
<b>Warning</b>:  include(): Failed opening '.\PhpOffice\autoload.php' for inclusion (include_path='.:/opt/alt/php73/usr/share/pear') in <b>/home/u527331572/domains/thestory.host/public_html/adminPanel/adminPages/excel.php</b> on line <b>8</b><br />
<br />
<b>Fatal error</b>:  Uncaught Error: Class 'PhpOffice\PhpSpreadsheet\Reader\Html' not found in /home/u527331572/domains/thestory.host/public_html/adminPanel/adminPages/excel.php:20
Stack trace:
#0 {main}
  thrown in <b>/home/u527331572/domains/thestory.host/public_html/adminPanel/adminPages/excel.php</b> on line <b>20</b><br />

提到的文件可以在提到的地方找到

标签: phpphpspreadsheetphpoffice-phpspreadsheet

解决方案


你更新了 composer.json 吗?

我在 PHPOffice 项目中看到了这一点:

"require": {
        "php": "^7.2 || ^8.0",

推荐阅读