MNB PHPExcelDeveloper Guide
JSON module

Writing JSON Output

Write row arrays, serialize complete workbook objects, and return JSON strings for APIs without saving files.

Updated

LevelIntermediateReading time7 minPackagemnb/mnb-phpexcel
Json::write()JsonWriter::writeWorkbook()JsonWriter::payloadToString()

What you will learn

  • Write normalized rows with stable scalar conversion.
  • Choose rows, single-sheet wrapper, or multi-sheet workbook shapes.
  • Return prebuilt structured payloads as JSON strings.

Write rows with the format facade#

PHP
Json::write($rows, 'products.json', [
    'pretty' => true,
    'preserve_zero_fraction' => true,
    'trailing_newline' => true,
]);

Serialize a complete workbook#

PHP
use Mnb\PHPExcel\Writer\JsonWriter;

$writer = new JsonWriter();
$json = $writer->workbookToString($workbook, [
    'mode' => 'auto',
    'include_metadata' => true,
    'include_sheet_names' => true,
    'preserve_associative_rows' => true,
]);

Return structured output directly#

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

$json = (new JsonWriter())->payloadToString($payload, [
    'pretty' => false,
]);