Skip to content
MNB PHPExcelDeveloper Guide
AI

XLSX library

Writing XLSX Workbooks

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

Level
Intermediate
Reading time
10 min
Package
mnb/mnb-phpexcel-xlsx
Updated
Xlsx::write()Xlsx::writeImportTemplate()XlsxWriter::write()

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