How to read and write excel files in 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.


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.