Home > Enterprise >  PHPSpreadsheet doesn't work with PHP version 7.3.26
PHPSpreadsheet doesn't work with PHP version 7.3.26

Time:10-06

I have tested this on 7.2 and it's working. When I move this code to 7.3 it's not working. I installed PHPSpreadsheet without using composer

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

fails with the following error, enter image description here

When I add ini_set('display_errors', 1); error_reporting(E_ALL); befor the namespace it failed with 500. but when I add this below the namespace line the file got downloaded. But with followin stacktrack on the file,

<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 />

The mentioned files are available at the mentioned places though

CodePudding user response:

did you update composer.json ?

I saw that on the project PHPOffice:

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

CodePudding user response:

You can try with install it via composer composer require phpoffice/phpspreadsheet on separate folder / project

  1. Make sure your PHP server set to 7.3.
  2. try with following test code and then compare it with your solution and you will see what are the changes required as phpspreadsheet version which was working in 7.2 is not compatible in PHP 7.3. as there are many native function of PHP 7.2 was deprecated
<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'This new hello World !');

$writer = new Xlsx($spreadsheet);
$writer->save('ThisMyExcelExample.xlsx');
?>
  • Related