skip to Main Content

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 ".PhpOfficeautoload.php";
use PhpOfficePhpSpreadsheetSpreadsheet;
use PhpOfficePhpSpreadsheetWriterXlsx;
use PhpOfficePhpSpreadsheetStyleBorder;
use PhpOfficePhpSpreadsheetStyleColor;

$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 PhpOfficePhpSpreadsheetReaderHtml();
$spreadsheet = $reader->loadFromString($htmlString);
$writer = PhpOfficePhpSpreadsheetIOFactory::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(.PhpOfficeautoload.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 '.PhpOfficeautoload.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 'PhpOfficePhpSpreadsheetReaderHtml' 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

2

Answers


  1. did you update composer.json ?

    I saw that on the project PHPOffice:

    "require": {
            "php": "^7.2 || ^8.0",
    
    Login or Signup to reply.
  2. 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 PhpOfficePhpSpreadsheetSpreadsheet;
    use PhpOfficePhpSpreadsheetWriterXlsx;
    use PhpOfficePhpSpreadsheetStyleBorder;
    use PhpOfficePhpSpreadsheetStyleColor;
    
    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue('A1', 'This new hello World !');
    
    $writer = new Xlsx($spreadsheet);
    $writer->save('ThisMyExcelExample.xlsx');
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search