phpphpofficephpoffice-phpspreadsheet

Installation error on PHPOffice PHPSpreadSheet with Composer


I am using Windows 10 with Laragon 5.0 WAMP (PHP 7.4.19 & Apache 2.4.47).

Trying install PHPOffice/PHPSpreadSheet with Composer using this command:

composer require phpoffice/phpspreadsheet

Nothing wrong with Composer installation, but when trying run this PHP code:

<?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');
?>

it gets error:

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

Composer SHOW:

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

Composer DIAGNOSE:

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

Where am I doing wrong?

Please help, thanks


Solution

  • The correct code:

    <?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');
    
    ?>
    

    Search with Windows File Explorer, the PHPOffice folder and autoload.php file installed on C:\Users\user\vendor

    Laragon Apache htdocs folder in C:\laragon\www

    Change require __DIR__ . '/vendor/autoload.php'; with require "C:/Users/user/vendor/autoload.php"; and it finally works

    Thanks to oleibman on PHPOffice/PhpSpreadsheet Github