Create Workbooks and Worksheets
Build one-sheet reports or multi-sheet workbooks from PHP iterables and arrays.
Updated
PHP 8.1+Full packageNative XLSX
MnbExcel::fromArray()MnbExcel::fromWorkbookArray()MnbExcel::report()WorkbookBuilder::save()What you will learn
- Create a workbook from associative data.
- Create several worksheets in one file.
- Choose a report-oriented or raw-data builder.
Create one worksheet from rows#
use Mnb\PHPExcel\MnbExcel;
MnbExcel::fromArray($rows)
->withHeader()
->save('data.xlsx');Use report defaults for business output#
MnbExcel::report($rows, template: 'business')
->title('Monthly Sales')
->creator('Finance Application')
->company('Example Company')
->freezeHeader()
->autoFilter()
->autoWidth()
->save('monthly-sales.xlsx');Create a multi-sheet workbook#
$workbook = MnbExcel::fromWorkbookArray([
'Orders' => $orders,
'Customers' => $customers,
'Summary' => $summary,
]);
$workbook
->withHeader()
->autoWidth()
->save('operations.xlsx');Save to an application-controlled path#
$path = MnbExcel::storagePath('exports', 'monthly-sales.xlsx');
$workbook->save($path);Do not write directly to a filename received from a browser. Use safeFileName(), controlled directories, and explicit extensions.
Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue