Novis\Spreadsheet\Workbook::createForDownload
Starts a spreadsheet that your program sends to the browser as a file to download.
$name is the file name the browser saves. A name that ends in .csv makes a CSV file, and any other name makes an Excel file (xlsx). format: "csv" or format: "xlsx" sets the format whatever the name is. The response headers are sent at once. After that, each part of the file is sent as soon as it is ready, so a large export does not have to fit in memory.
Add a sheet with addSheet, add rows to it, and call finish at the end.
Good to know: set any other response header before you call createForDownload, because it sends the headers. In a CSV file, a value that starts with =, +, - or @ gets a ' in front. A spreadsheet program then shows it as text and does not run it as a formula.
Parameters
$namestring$optionsOptionsdefault null
Return value
Returns Workbook.
Examples
An orders export as a download
<?nvs// Shows how to send a list of orders to the browser as a file to download.use Novis\Spreadsheet\Workbook;
// The name ends in `.csv`, so the file is CSV. A name such as `orders.xlsx` makes an Excel file.var $book = Workbook::createForDownload('orders.csv');
// The keys of the first row are the header of the file.$book->addSheet('Orders')->addRows([ ['order' => 1042, 'customer' => 'Ada', 'total' => 19.9], ['order' => 1043, 'customer' => 'Alan', 'total' => 5.0], ['order' => 1044, 'customer' => 'Grace', 'total' => 12.5],]);
// `finish` sends the end of the file. The output below is the file the browser gets.$book->finish();order,customer,total1042,Ada,19.91043,Alan,51044,Grace,12.5Customer text in a csv download
<?nvs// Shows how a CSV download writes text that customers typed.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createForDownload('customers.csv');$book->addSheet('Customers')->addRows([ // A value with a comma or a quote is put in quotes, so it stays one field. ['name' => 'Example, Ltd', 'note' => "Call \"before\" noon"], // A value that starts with `=`, `+`, `-` or `@` gets a `'` in front. // A spreadsheet program then shows it as text and does not run it as a formula. ['name' => "=HYPERLINK(\"https://example.com\")", 'note' => '-5 points'],]);$book->finish();name,note"Example, Ltd","Call ""before"" noon""'=HYPERLINK(""https://example.com"")",'-5 pointsA large export one page at a time
<?nvs// Shows how to send a large export that never has to fit in memory.use Novis\Spreadsheet\Workbook;
class Orders{ // This generator gives one order at a time. A real program reads them from a database, page by page. public static function all(int $count): Iterator<array<mixed>> { int $id = 1; while ($id <= $count) { yield ['order' => 1000 + $id, 'total' => $id * 2.5]; $id = $id + 1; } }}
// `addRows` takes the rows from the generator. Each part of the file is sent when it is ready.// Add `format: "csv"` when the name does not end in `.csv`.var $book = Workbook::createForDownload('export', { format: 'csv' });$book->addSheet('Orders')->addRows(Orders::all(5));$book->finish();order,total1001,2.51002,51003,7.51004,101005,12.5