Write data from array to sheet using phpspreadsheet library

Write data from array to sheet using phpspreadsheet library

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why fromArray Fails for Column Headers and How to Fix It

The code snippet you provided attempts to populate Excel column headers using the fromArray method, but it results in an empty sheet or unexpected behavior. The core issue lies in how fromArray interprets its input data. When you pass a single-dimensional array like $header = array("Customer Number", "Customer Name", ...) to fromArray, the library treats this as a single row of data rather than a header row for subsequent columns.

Specifically, calling $sheet->fromArray($header, NULL, 'A1') places those values into Row 1, starting at Column A. However, because the array is one-dimensional, phpspreadsheet does not automatically infer that this is meant to be a header structure spanning multiple columns in a specific way unless the data is wrapped or the cell range is explicitly defined for multi-cell insertion. More critically, if you intend to write this as a header row while simultaneously writing data rows below it, simply passing the array directly often leads to confusion about whether the array represents rows of data or column definitions.

To correctly create an Excel sheet where the first row acts as headers derived from an array, you must explicitly define the cell range and ensure the data structure matches the library's expectations for multi-cell writes. The fromArray method expects a two-dimensional array to represent rows, or it requires a specific starting position that accommodates the single dimension as a row. If your goal is to have these strings appear in A1 through F1, you are technically creating one row of data, not necessarily setting "headers" in the metadata sense, although visually they function as headers.

If you need to write multiple rows of data where the first row is strictly defined by an array of column names, consider constructing a two-dimensional array manually. This approach gives you granular control over cell positioning and formatting:

$header = ["Customer Number", "Customer Name", "Address", "City", "State", "Zip"];

// Manually define the header row as a single-element array containing the list
// This ensures it is treated as one row of data starting at A1
$sheet->fromArray([$header], null, 'A1');

// Optional: Apply bold formatting to distinguish headers
foreach ($header as $index => $value) {
    $cell = $sheet->getCellByColumnAndRow($index + 1, 1);
    $cell->getStyle()->getFont()->setBold(true);
}

In this revised example, wrapping the $header array inside another array [$header] creates a structure where the first dimension represents rows. Since there is only one element in that outer array, it becomes a single row with six cells. This aligns with how fromArray processes data: the outer array index corresponds to the row number, and the inner array elements correspond to columns.

Alternatively, if you prefer direct cell manipulation, looping through the header array allows you to place each value into its specific column without relying on array dimension inference. This method is often more robust when dealing with complex formatting requirements or when integrating with LaravelCompany's backend systems where precise control over output streams is necessary.

$sheet->fromArray($header, null, 'A1'); // Treats $header as a single row

// Or, for explicit control:
foreach ($header as $index => $columnTitle) {
    $cellAddress = "A" . (1 + $index); // A1, B1, C1...
    $sheet->setCellValue($cellAddress, $columnTitle);
}

When redirecting output to the browser, ensure your headers are set correctly before invoking the writer. The Xlsx writer is required for .xlsx files; using the old application/vnd.ms-excel MIME type is technically incorrect for .xlsx files and may cause compatibility issues in modern browsers or clients that expect Office Open XML formats.

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="myfile.xlsx"');
header('Cache-Control: max-age=0');

$writer = new Xlsx($spreadsheet);
$writer->save('php://output');

Using application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ensures the file is recognized as an Excel 2007+ workbook. If you are building this within a Laravel application, remember that the Xlsx writer class requires the PhpOffice\PhpSpreadsheet package to be installed via Composer. Always verify the version compatibility of your dependencies, as API behaviors can shift between major releases of the library.

Our network