MNB PHPExcelDeveloper Guide
XLSX library

Writing XLSX Workbooks

Create simple exports, styled reports, validated templates, protected workbooks, and browser downloads.

Updated

LevelIntermediateReading time10 minPackagemnb/mnb-phpexcel-xlsx
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#

PHP
Xlsx::write($orders, 'orders.xlsx', [
    'sheet_name' => 'Orders',
    'with_header' => true,
    'password' => getenv('REPORT_PASSWORD'),
    'encryption_mode' => 'agile',
]);

Use WorkbookData for presentation features#

PHP
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#

PHP
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#

ControlPurposeAPI
Password to openEncrypts the XLSX package contents.password / Xlsx::encryptFile()
Workbook protectionRestricts structure changes after opening.protection_password with workbook settings
Worksheet protectionRestricts editing actions inside sheets.Sheet protection definitions or writer options