Skip to content

Novis\Spreadsheet\Workbook::createForStream

Starts a spreadsheet that your program writes to a file it has already opened.

Open the file with Core\IO::open for writing, and pass it to createForStream. Each part of the spreadsheet is written to the file as soon as it is ready. An open file has no name, so the file is an Excel file (xlsx) unless you set format: "csv".

finish writes the end of the spreadsheet and does not close the file. Your program can write more to the file after that, and it closes the file when it is done.

The examples below write a CSV file, write two exports into one file, and write an Excel file and read it back.

Parameters

  • $fileCore\IO\File
  • $optionsOptionsdefault null

Return value

Returns Workbook.

Examples

Write to a file you opened

<?nvs
// Shows how to write a spreadsheet to a file that your program opened.
use Novis\Spreadsheet\Workbook;
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'orders.csv');
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Write);
// An open file has no name to read the format from, so `format: "csv"` sets it.
var $book = Workbook::createForStream($file, { format: 'csv' });
$book->addSheet('Orders')->addRows([
['order' => 1042, 'total' => 19.9],
['order' => 1043, 'total' => 5.0],
]);
$book->finish();
// `finish` does not close the file. Your program closes it.
$file->close();
echo Core\IO::readText($path);
Output
order,total␍
1042,19.9␍
1043,5␍

Two exports in one log file

<?nvs
// Shows how to write two CSV exports, one after the other, into a file that stays open.
use Novis\Spreadsheet\Workbook;
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'daily.log');
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Write);
// Each export goes to the same open file, after a line of your own.
$file->write("# orders\n");
var $orders = Workbook::createForStream($file, { format: 'csv' });
$orders->addSheet('Orders')->addRow(['order' => 1042, 'total' => 19.9]);
$orders->finish();
$file->write("# returns\n");
var $returns = Workbook::createForStream($file, { format: 'csv' });
$returns->addSheet('Returns')->addRow(['order' => 1040, 'reason' => 'damaged']);
$returns->finish();
$file->close();
echo Core\IO::readText($path);
Output
# orders
order,total␍
1042,19.9␍
# returns
order,reason␍
1040,damaged␍

An excel file to an open file

<?nvs
use Novis\Spreadsheet\Reader;
// Shows how to write an Excel file to an open file and read it back.
use Novis\Spreadsheet\Workbook;
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'stock.xlsx');
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Write);
// Without `format`, an open file gets an Excel file (xlsx).
var $book = Workbook::createForStream($file);
$book->addSheet('Stock')->addRows([['item' => 'Pen', 'count' => 120], ['item' => 'Ink', 'count' => 35]]);
var $written = $book->finish();
$file->close();
echo $written->rows['Stock'], " rows\n";
foreach (Reader::open(Core\IO::open($path, Core\IO::FileMode::Read))->rows('Stock', { header: true }) as var $row) {
echo $row->values['item'], ': ', $row->values['count'], "\n";
}
Output
2 rows
Pen: 120
Ink: 35