使用 Composer 在 PHPOffice PHPSpreadSheet 上安装错误

Installation error on PHPOffice PHPSpreadSheet with Composer

我正在使用 Windows 10 和 Laragon 5.0 WAMP(PHP 7.4.19 和 Apache 2.4.47)。

正在尝试使用以下命令安装 PHPOffice/PHPSpreadSheet Composer:

composer require phpoffice/phpspreadsheet

Composer 安装没有问题,但是当尝试 运行 这个 PHP 代码时:

<?php
$spreadsheet = \PhpSpreadsheet\IOFactory::load('template.xlsx');

$worksheet = $spreadsheet->getActiveSheet();

$worksheet->getCell('A1')->setValue('John');
$worksheet->getCell('A2')->setValue('Smith');

$writer = \PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('write.xls');
?>

出现错误:

Fatal error: Uncaught Error: Class 'PhpSpreadsheet\IOFactory' not found in C:\laragon\www\excel.php:2 Stack trace: #0

作曲家秀:

C:\Users\wieb>composer show
ezyang/htmlpurifier       v4.14.0 Standards compliant HTML filter written in PHP
maennchen/zipstream-php   2.1.0   ZipStream is a library for dynamically streaming dynamic zip files from PHP without writing to the disk at all on the server.
markbaker/complex         3.0.1   PHP Class for working with complex numbers
markbaker/matrix          3.0.0   PHP Class for working with matrices
myclabs/php-enum          1.8.3   PHP Enum implementation
phpoffice/phpspreadsheet  1.22.0  PHPSpreadsheet - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
psr/http-client           1.0.1   Common interface for HTTP clients
psr/http-factory          1.0.1   Common interfaces for PSR-7 HTTP message factories
psr/http-message          1.0.1   Common interface for HTTP messages
psr/simple-cache          1.0.1   Common interfaces for simple caching
symfony/polyfill-mbstring v1.25.0 Symfony polyfill for the Mbstring extension

作曲家诊断:

C:\Users\wieb>composer diagnose
Checking composer.json: WARNING
No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.
Checking platform settings: OK
Checking git settings: OK
Checking http connectivity to packagist: OK
Checking https connectivity to packagist: OK
Checking github.com rate limit: OK
Checking disk free space: OK
Checking pubkeys:
Tags Public Key Fingerprint: 57815BA2 7E54DC31 7ECC7CC5 573090D0  87719BA6 8F3BB723 4E5D42D0 84A14642
Dev Public Key Fingerprint: 4AC45767 E5EC2265 2F0C1167 CBBB8A2B  0C708369 153E328C AD90147D AFE50952
OK
Checking composer version: OK
Composer version: 2.3.4
PHP version: 7.4.19
PHP binary path: C:\laragon\bin\php\php-7.4.19-Win32-vc15-x64\php.exe
OpenSSL version: OpenSSL 1.1.1k  25 Mar 2021
cURL version: 7.70.0 libz 1.2.11 ssl OpenSSL/1.1.1k
zip: extension present, unzip not available, 7-Zip not available

我哪里做错了?

请帮忙,谢谢

正确代码:

<?php

require __DIR__ .  '/vendor/autoload.php'; /* will vary depending upon your environment */
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('template.xlsx');

$worksheet = $spreadsheet->getActiveSheet();

$worksheet->getCell('A1')->setValue('John');
$worksheet->getCell('A2')->setValue('Smith');

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('write.xls');

?>

使用 Windows 文件资源管理器、PHPOffice 文件夹和安装在 C:\Users\user\vendor

上的 autoload.php 文件进行搜索

C:\laragon\www

中的 Laragon Apache htdocs 文件夹

require __DIR__ . '/vendor/autoload.php'; 更改为 require "C:/Users/user/vendor/autoload.php";,它终于起作用了

感谢 oleibmanPHPOffice/PhpSpreadsheet Github