Categories : PHP

How to install phpspreadsheet library with composer

composer require phpoffice/phpspreadsheet

How to Write and save excel files (CSV, XLSX, XLS) Just change Xlsx to other types if you need.

A1 is a cell address for column 1 and row 1. If you want to fill other row and columns you can chanve them as :

A2 -> Column 2 and row 1

C3 -> Column 3 and row 3


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

How to read excel files as array


require 'vendor/autoload.php';

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();

$spreadsheet = $reader->load(__DIR__ . '/data.xlsx');
$sheetData = $spreadsheet->getActiveSheet()->toArray();

Leave a Reply

Your email address will not be published. Required fields are marked *