Writing XLSX Workbooks
Create simple exports, styled reports, validated templates, protected workbooks, and browser downloads.
Updated
Xlsx::write()Xlsx::writeImportTemplate()WorkbookBuilder::save()What you will learn
- Choose the simple writer or the richer workbook builder.
- Generate import templates with headers and validation.
- Apply encryption and protection at the correct layer.
Use the simple writer for row exports#
Xlsx::write($orders, 'orders.xlsx', [
'sheet_name' => 'Orders',
'with_header' => true,
'password' => getenv('REPORT_PASSWORD'),
'encryption_mode' => 'agile',
]);Use a builder for presentation features#
use Mnb\PHPExcel\Core\WorkbookBuilder;
WorkbookBuilder::fromWorkbookArray(['Orders' => $orders])
->withHeader()
->freezeHeader()
->autoFilter()
->styleHeader([
'font' => ['bold' => true, 'color' => 'FFFFFF'],
'fill' => ['color' => '137A43'],
])
->save('styled-orders.xlsx');The detailed writer guides cover layout, styles, validation, comments, links, images, charts, pivots, and browser delivery.
Generate an import template#
Xlsx::writeImportTemplate([
'sku' => ['label' => 'SKU', 'required' => true, 'example' => 'SKU-1001'],
'name' => ['label' => 'Product name', 'required' => true],
'price' => ['label' => 'Price', 'type' => 'decimal', 'example' => 49.95],
], 'product-import-template.xlsx', [
'sheet_name' => 'Products',
]);Separate encryption from document protection#
| Control | Purpose | API |
|---|---|---|
| Password to open | Encrypts the XLSX package contents. | password / Xlsx::encryptFile() |
| Workbook protection | Restricts structure changes after opening. | protection_password with workbook settings |
| Worksheet protection | Restricts editing actions inside sheets. | Sheet protection definitions or writer options |
Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue