MNB PHPExcelDeveloper Guide
v2.0.5
XML module

Writing XML Output

Write row arrays, workbook wrappers, metadata, typed values, and arbitrary structured payloads as XML.

Updated

LevelIntermediateReading time7 minPackagemnb/mnb-phpexcel
Xml::write()XmlWriter::writeWorkbook()XmlWriter::payloadToString()

What you will learn

  • Configure root, row, cell, and sheet element names.
  • Choose a compact or pretty document with an XML declaration.
  • Serialize structured workbook arrays directly for API responses.

Write associative rows#

PHP
Xml::write($products, 'products.xml', [
    'root' => 'products',
    'row_tag' => 'product',
    'include_row_number' => true,
    'declaration' => true,
    'pretty' => true,
]);

Write a complete workbook#

PHP
use Mnb\PHPExcel\Writer\XmlWriter;

$xml = (new XmlWriter())->workbookToString($workbook, [
    'root' => 'workbook',
    'sheet_tag' => 'sheet',
    'row_tag' => 'row',
    'include_metadata' => true,
    'workbook_wrapper' => true,
]);

Serialize any structured payload#

PHP
$payload = MnbExcel::read('report.xlsx')
    ->withHeaderRow(1)
    ->toStructuredWorkbookArray();

$xml = (new XmlWriter())->payloadToString($payload, [
    'root' => 'spreadsheet',
    'pretty' => true,
]);