Create Your First Workbook
Create a styled XLSX workbook from PHP arrays, format report columns, and save the generated file safely with an atomic write.
Updated
MnbExcel::report()WorkbookBuilder::withHeader()WorkbookBuilder::autoWidth()WorkbookBuilder::save()What you will learn
- Generate a valid XLSX from associative rows.
- Apply a report template and column formatting.
- Save output into a controlled directory.
Create a complete report#
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use Mnb\PHPExcel\MnbExcel;
$rows = [
['Product' => 'Keyboard', 'Quantity' => 4, 'Amount' => 199.96],
['Product' => 'Mouse', 'Quantity' => 8, 'Amount' => 159.92],
];
$output = __DIR__ . '/storage/exports/sales-report.xlsx';
MnbExcel::report($rows)
->withHeader()
->currencyColumns(['Amount'], '$')
->integerColumns(['Quantity'])
->freezeHeader()
->autoFilter()
->autoWidth()
->save($output);
printf("Saved: %s\n", $output);What the builder does#
- Derives columns from associative-array keys.
- Writes numeric values as numbers rather than text.
- Applies workbook styles without changing the source array.
- Writes to the destination using the library’s safe save path.
- Can later add images, charts, pivots, encryption, and protection through the same fluent builder.
Inspect the generated workbook during development#
$validation = MnbExcel::validateXlsx($output);
if (($validation['valid'] ?? false) !== true) {
throw new RuntimeException('Generated XLSX failed integrity validation.');
}Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue