Skip to content

Novis\Spreadsheet\Workbook::fromRows

Makes a spreadsheet with one sheet from a list of rows.

$rows is an array of rows, or a generator that gives one row at a time. Each value in a row is one cell. When the rows have keys, the sheet's first row shows them.

fromRows returns an Export. Call one of its methods to write the file. download sends it to the browser, and save writes it to a path. to writes it to an open file, and bytes returns it.

It takes the options of createInMemory and of addSheet. format is xlsx, ods or csv. Without it, download and save take the format from the file name, and the others write xlsx. For CSV, csv sets the separator, the quote and the charset.

Good to know: a generator can be read only once, so a second write of its Export throws a LogicError. For more sheets, use one of the create methods, such as createInMemory, and call addSheet for each sheet.

Parameters

  • $rowsarray<array<mixed>>|Iterable<array<mixed>>|Iterator<array<mixed>>
  • $optionsExportOptionsdefault null

Return value

Returns Export.

Examples

A price list in one call

<?nvs
use Novis\Spreadsheet\Reader;
// Shows how to turn an array of rows into an Excel file in one call.
use Novis\Spreadsheet\Workbook;
array<array<mixed>> $prices = [
['item' => 'Pen', 'price' => 1.5],
['item' => 'Ink', 'price' => 4.25],
['item' => 'Pad', 'price' => 2.0],
];
// `fromRows` makes a workbook with one sheet. `bytes` returns the whole xlsx file.
// `sheet` names the sheet. Without it, the sheet is `Sheet1`.
bytes $file = Workbook::fromRows($prices, { sheet: 'Prices' })->bytes();
foreach (Reader::open($file)->rows('Prices') as var $row) {
echo Core\Json::encode($row->values), "\n";
}
Output
{"A":"item","B":"price"}
{"A":"Pen","B":1.5}
{"A":"Ink","B":4.25}
{"A":"Pad","B":2}

An orders download in one line

<?nvs
// Shows how to send rows to the browser as a CSV file to download, in one line.
use Novis\Spreadsheet\Workbook;
array<array<mixed>> $orders = [
['order' => 1042, 'customer' => 'Ada', 'total' => 19.9],
['order' => 1043, 'customer' => 'Alan', 'total' => 5.0],
];
// `download` sends the file with the name `orders.csv`. The name sets the format.
Workbook::fromRows($orders)->download('orders.csv');
Output
order,customer,total
1042,Ada,19.9
1043,Alan,5

Save rows from a generator

<?nvs
// Shows how to save rows from a generator to a file, one row at a time.
use Novis\Spreadsheet\Workbook;
class Visits
{
// This generator gives one row for each day. A real program reads the rows from a database.
public static function perDay(): Iterator<array<mixed>>
{
int $day = 1;
while ($day <= 7) {
yield ['day' => Core\Time\Date::at(2024, 6, $day as uint), 'visits' => 100 + $day * 7];
$day = $day + 1;
}
}
}
// With `freeze: false`, the header row does not stay at the top when a person scrolls.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'visits.xlsx');
var $written = Workbook::fromRows(Visits::perDay(), { sheet: 'Visits', freeze: false })->save($path);
// `save` returns the number of rows of each sheet.
echo $written->rows['Visits'], " rows saved\n";
Output
7 rows saved

A tab separated file

<?nvs
// Shows how to write rows as a file with a tab between the values.
use Novis\Spreadsheet\Workbook;
var $rows = [
['Item' => 'Widget', 'Note' => 'red, large'],
['Item' => 'Gadget', 'Note' => 'blue'],
];
// A comma is not special in this file, so `red, large` has no quotes.
echo Workbook::fromRows($rows, { format: 'csv', csv: { separator: "\t" } })->bytes() as string;
Output
Item Note␍
Widget red, large␍
Gadget blue␍

The same rows as an ods file

<?nvs
// Shows how to write rows as an OpenDocument file (ods).
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
array<array<string|float>> $rows = [
['Item' => 'Widget', 'Price' => 2.5],
['Item' => 'Gadget', 'Price' => 10.0],
];
// `format: 'ods'` makes an ods file. `bytes` returns the whole file.
bytes $file = Workbook::fromRows($rows, { format: 'ods', sheet: 'Prices' })->bytes();
// The reader reads it back. The first sheet is `Prices`.
foreach (Reader::open($file)->rows('Prices', { header: true }) as var $row) {
echo $row->values['Item'], ' costs ', $row->values['Price'], "\n";
}
Output
Widget costs 2.5
Gadget costs 10