skip to Main Content

I use the following small PHP script using the PhpSpreadsheet library 1.29. Unfortunately I can’t get the script to open an excel sheet. I don’t get any error messages.

On my Windows PC I use XAMPP Version: 8.2.4
On the Server I use PHP 8.2
Composer version 2.6.6 2023-12-08 18:32:26

I installed the PhpSpreadsheet library with composer require phpoffice/phpspreadsheet

Using version ^1.29 for phpoffice/phpspreadsheet

I uploaded the files in the vendor/phpoffice directory to the server.

<?php

// Autoloader für Composer
require 'vendor/autoload.php';

use PhpOfficePhpSpreadsheetSpreadsheet;
use PhpOfficePhpSpreadsheetIOFactory;

$spreadsheet = new Spreadsheet();

//Specify the properties for this document
$spreadsheet->getProperties()
    ->setTitle('PHP Download Example')
    ->setSubject('A PHPExcel example')
    ->setDescription('A simple example for PhpSpreadsheet. This class replaces the PHPExcel class')
    ->setCreator('php-download.com')
    ->setLastModifiedBy('php-download.com');

//Adding data to the excel sheet
$spreadsheet->setActiveSheetIndex(0)
    ->setCellValue('A1', 'This')
    ->setCellValue('A2', 'is')
    ->setCellValue('A3', 'only')
    ->setCellValue('A4', 'an')
    ->setCellValue('A5', 'example');


$writer = IOFactory::createWriter($spreadsheet, "Xlsx"); //Xls is also possible
$writer->save("zz_excel_file.xlsx");

?>

I’m expecting that $spreadsheet = new Spreadsheet(); will open the spreadsheet, but it doesn’t

2

Answers


  1. Chosen as BEST ANSWER

    I do want to create a new spreadsheet and not to open an existing one.

    I also tried to open an existing spreadsheet, but this also didn't work.

    It looks to me, that

    $spreadsheet = new Spreadsheet();

    doesn't somehow communicate with

    use PhpOfficePhpSpreadsheetSpreadsheet;


  2. new Spreadsheet() doesn’t open a spreadsheet, it creates a new one.

    To open a spreadsheet with PhpSpreadsheet, you can use IOFactory::load():

    $spreadsheet = IOFactory::load("path/to/your/file");.

    Have a read of the docs.

    There’s also a good example here in the samples directory of the PhpSpreadsheet repository.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search