php - 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 />
提到的文件可以在提到的地方找到
解决方案
你更新了 composer.json 吗?
我在 PHPOffice 项目中看到了这一点:
"require": {
"php": "^7.2 || ^8.0",
推荐阅读
- c - 为什么我在不同的笔记本电脑上得到相同代码的不同输出?
- javascript - 这个标签是如何快速添加到 DOM 中的?
- node.js - VueJS:axios 在 POST 请求上返回 404 Not Found 错误
- c - 无法将逗号分隔的字符串写入/读取到文件中
- python - 如何在 Python Kivy 中的每个小部件中画线
- macos - 我可以从我的 macOS 应用程序控制另一个应用程序吗?
- php - 填写数组中缺失的日期
- node.js - 在 node.js 中保存文件时,如果文件名已经存在,则增加文件名?
- reactjs - react-router with context - 为什么它与锚点和链接不一样
- css - 悬停在 SVG 上更改颜色