Novis\Spreadsheet\Export::save
Writes the spreadsheet to a file at $path.
The file is created, or replaced if it exists. A path that ends in .csv makes a CSV file, and any other path makes an Excel file (xlsx). save returns a Written: its rows has the number of rows of each sheet, and its size is the length of the file in bytes. Its bytes is null, because the file is on the disk.
Good to know: a program you start from the command line may save to any path. On a server, a program needs the fs.write capability in nvs.toml for the folder of the path. Without it, save throws an error and writes nothing.
Parameters
$pathstring
Return value
Returns Written.
Examples
Save an orders export
<?nvsuse Novis\Spreadsheet\Reader;// Shows how to save a list of orders as an Excel file and read it back.use Novis\Spreadsheet\Workbook;
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'orders.xlsx');array<array<mixed>> $orders = [ ['order' => 1042, 'customer' => 'Ada', 'total' => 19.9], ['order' => 1043, 'customer' => 'Alan', 'total' => 5.0],];
// `save` returns a `Written`. Its `rows` has the number of rows of each sheet.var $written = Workbook::fromRows($orders, { sheet: 'Orders' })->save($path);echo $written->rows['Orders'], " orders saved\n";
// `header: true` uses the first row as the keys of each row.foreach (Reader::open(Core\IO::open($path, Core\IO::FileMode::Read))->rows('Orders', { header: true }) as var $row) { echo $row->values['order'], ': ', $row->values['customer'], "\n";}2 orders saved1042: Ada1043: AlanThe same orders as a csv file
<?nvs// Shows how to save the same orders as a CSV file.use Novis\Spreadsheet\Workbook;
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'orders.csv');array<array<mixed>> $orders = [ ['order' => 1042, 'customer' => 'Ada', 'total' => 19.9], ['order' => 1043, 'customer' => 'Alan', 'total' => 5.0],];
// The path ends in `.csv`, so the file is CSV. `size` is the length of the file in bytes.var $written = Workbook::fromRows($orders)->save($path);echo $written->size, " bytes\n";echo Core\IO::readText($path);53 bytesorder,customer,total␍1042,Ada,19.9␍1043,Alan,5␍A nightly report with two sheets
<?nvs// Shows how to save a report with two sheets through one `Export`.use Novis\Spreadsheet\Export;use Novis\Spreadsheet\Workbook;
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'nightly.xlsx');
// The function adds the sheets. It runs when `save` is called.var $report = new Export(fn(Workbook $book): void => { $book->addSheet('Orders')->addRows([['order' => 1042, 'total' => 19.9], ['order' => 1043, 'total' => 5.0]]); $book->addSheet('Returns')->addRow(['order' => 1042, 'reason' => 'damaged']);}, null);
var $written = $report->save($path);echo 'Orders: ', $written->rows['Orders'], " rows\n";echo 'Returns: ', $written->rows['Returns'], " rows\n";Orders: 2 rowsReturns: 1 rows