ExcelCreator is a modern wrapper around PHPSpreadsheet that simplifies the most commonly used tasks in reading and writing Excel files. It provides two main classes: Writer for creating and formatting spreadsheets, and Reader for loading and reading spreadsheet content.
Install ExcelCreator using Composer:
{
"require": {
"adnzaki/excel-creator": "^2.0.1"
}
}Then run:
composer updatecomposer require adnzaki/excel-creatorIf you prefer the latest development version:
{
"require": {
"adnzaki/excel-creator": "dev-main"
}
}Then run composer update.
This library provides two main classes under the namespace ExcelTools:
use ExcelTools\Writer;
$excel = new Writer();- Saving file to browser:
$excel->save('hello_world.xlsx');- Apply styles to cell range:
$style = [
'font' => [
'name' => 'Arial',
'size' => 10,
],
'borders' => [
'allBorders' => [
'borderStyle' => $excel->border::BORDER_THIN,
'color' => ['argb' => $excel->color::COLOR_BLACK],
],
]
];
$excel->applyStyle($style, 'A2:D10');- Fill data:
$data = [
['Name', 'City'],
['Zaki', 'Jakarta'],
['Dien', 'Bojonegoro']
];
$excel->fillCell($data, 'A1');- Text wrapping:
$excel->wrapText('B5');- Merge & unmerge:
$excel->mergeCells('A1:B1');
$excel->unmergeCells('A1:B1');- Column widths:
$excel->setColumnWidth('A', 20);
$excel->setMultipleColumnsWidth(['B', 'C'], 25);
$excel->setDefaultColumnWidth(15);- Row heights:
$excel->setRowHeight(3, 25);
$excel->setMultipleRowsHeight(['1' => 40, '3-5' => 25]);
$excel->setDefaultRowHeight(20);- Set default font:
$excel->setDefaultFont('Calibri', 11);use ExcelTools\Reader;
$reader = new Reader();
$reader->loadFromFile('SampleFile.xlsx');- Get all data from the active sheet:
$data = $reader->getSheetData(true); // true = first row as headers
print_r($data);- Get sheet names:
$sheetNames = $reader->getSheetNames();- Switch to another sheet:
$reader->setActiveSheet('Sheet2');MIT
Adnan Zaki – https://github.com/adnzaki