Generating excel file using PHP

Suresh Kamrushi
Dec 21, 2020

--

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:

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 “hello world.xlsx” in same folder.

Originally published at http://sforsuresh.in.

--

--

No responses yet