PhpSpreadsheet, powered by the PHP Office team, is a robust library written exclusively in PHP. It provides a set of classes that make reading, writing, and working with spreadsheet files (think Excel, LibreOffice Calc etc.) quite seamless. PhpSpreadsheet allows for data extraction from these files as well as data entry, all in a couple of lines of code, making it incredibly versatile and useful for numerous applications.
Using PhpSpreadsheet package is straightforward with PHP's Composer. To use PhpSpreadsheet in your project, you begin by installing it with composer via the command: composer require phpoffice/phpspreadsheet
. Once installed, you can then use it to carry out various spreadsheet related tasks.
For instance, to write data to a spreadsheet, use the following code:
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello');
$sheet->setCellValue('B1', 'World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello_world.xlsx');
This will create a new xlsx file named hello_world.xlsx
with the cell A1 containing the text Hello
and cell B1 containing World !
.
If you're trying to read data from an existing spreadsheet, PhpSpreadsheet provides the IOFactory class to facilitate this process. For example:
use PhpOffice\PhpSpreadsheet\IOFactory;
$spreadsheet = IOFactory::load('Sample.xlsx');
$worksheet = $spreadsheet->getActiveSheet();
$cellData = $worksheet->getCell('A1')->getValue();
echo $cellData;
In this code, we're loading a spreadsheet file by the name Sample.xlsx
and retrieving the value stored in cell A1. The value is then printed to the console.
Detailed information on the workings, installation and other functionality of PhpSpreadsheet can be found on the official documentation page at https://phpspreadsheet.readthedocs.io. The API documentation can be found at https://phpoffice.github.io/PhpSpreadsheet, providing an overview of the classes, functions, constants, and methods employed by the PhpSpreadsheet package.