Generating Excel file in PHP

Posted on February 5, 2019 by Suresh Kamrushi in PHP

Here i am sharing how one can generate Excel file using php library “PhpSpreadsheet“.
To use library you need to install using composer. Open command window and excecute below command in your project folder:

composer require phpoffice/phpspreadsheet

It will create a new folder as “vendor” in your project and copy all files and folders as below:installing_PhpSpreadsheet

Next create a file as “sample.php” and copy below code:

<?php 
require 'vendor/autoload.php'; 
use PhpOffice\PhpSpreadsheet\Spreadsheet; 
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; 
$spreadsheet = new Spreadsheet(); 
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');

$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');

Now open your browser and open the file “sample.php” it will create a excel file as “hellow world.xlsx” in same folder.

Source: https://phpspreadsheet.readthedocs.io/en/develop/#getting-started