Writing XLSX Workbooks
Create simple exports, styled reports, validated templates, protected workbooks, and browser downloads.
Updated
Xlsx::write()Xlsx::writeImportTemplate()XlsxWriter::write()What you will learn
- Choose the simple facade or the lower-level workbook data model and writer.
- 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 WorkbookData for presentation features#
use Mnb\PHPExcel\Core\WorkbookData;
use Mnb\PHPExcel\Core\WorkbookFactory;
use Mnb\PHPExcel\Writer\XlsxWriter;
$sheet = WorkbookFactory::worksheet($orders, 'Orders', true);
$sheet->freezeHeader = true;
$sheet->autoFilter = true;
$sheet->headerStyle = [
'font' => ['bold' => true, 'color' => 'FFFFFF'],
'fill' => ['color' => '137A43'],
];
(new XlsxWriter())->write(
new WorkbookData([$sheet]),
'styled-orders.xlsx'
);WorkbookData and WorksheetData come from the Core dependency installed with the XLSX package. XlsxWriter serializes their styles, validation, comments, links, images, charts, pivots, and protection definitions.
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