Novis\Spreadsheet\Export::download
Sends the spreadsheet to the browser as a file to download.
$name is the file name the browser shows. It also sets the format: a name that ends in .csv makes a CSV file, and any other name makes an Excel file (xlsx). The file is sent in parts while it is written, so a large export does not wait until every row is ready. download returns nothing.
Good to know: a name with characters outside plain ASCII, such as März.csv, is sent in a form every browser reads. A quote or a line break in the name cannot add a header to the response.
Parameters
$namestring
Return value
Returns void.
Examples
An orders export as a download
<?nvs// Shows how to send a list of orders to the browser as a CSV file to download.use Novis\Spreadsheet\Workbook;
array<array<mixed>> $orders = [ ['order' => 1042, 'customer' => 'Ada', 'total' => 19.9], ['order' => 1043, 'customer' => 'Alan', 'total' => 5.0], ['order' => 1044, 'customer' => 'Grace', 'total' => 12.5],];
// The name ends in `.csv`, so the file is CSV. The output below is the file the browser gets.Workbook::fromRows($orders)->download('orders.csv');order,customer,total1042,Ada,19.91043,Alan,51044,Grace,12.5A file name with the month
<?nvs// Shows how to name a download after the month it covers.use Novis\Spreadsheet\Workbook;
Core\Time\Date $month = Core\Time\Date::at(2024, 5, 1);array<array<mixed>> $sales = [ ['day' => Core\Time\Date::at(2024, 5, 1), 'sales' => 1200], ['day' => Core\Time\Date::at(2024, 5, 2), 'sales' => 950],];
// The browser saves the file as `sales-2024-05.csv`. A date is written as `yyyy-mm-dd` in CSV.Workbook::fromRows($sales)->download('sales-' . $month->format('yyyy-MM') . '.csv');day,sales2024-05-01,12002024-05-02,950A large export from a generator
<?nvs// Shows how to send rows from a generator, so the program never has every row in memory.use Novis\Spreadsheet\Workbook;
class Shop{ // This generator gives one order at a time. A real program reads them from a database. public static function orders(int $count): Iterator<array<mixed>> { for (int $order = 1; $order <= $count; $order++) { yield ['order' => $order, 'total' => $order * 2.5]; } }}
// The browser gets each part of the file while the rows are still being written.Workbook::fromRows(Shop::orders(4))->download('orders.csv');order,total1,2.52,53,7.54,10