Skip to content

Novis\Spreadsheet\Reader::open

Opens a spreadsheet file so that your program can read it.

$source is the bytes of the file, or a Core\IO\File that your program opened for reading. The file can be xlsx, xlsm, xlsb, xls, ods or CSV. Reader::open finds the type from the bytes. Then sheets lists the sheets, and rows reads one of them.

A CSV file is UTF-8 text by default. csv sets its separator, quote, escape and charset. A first line such as sep=; sets the separator and is not a row, unless you set separator. A CSV file has one sheet, Sheet1, and each value in it is a string.

Good to know: a damaged file throws a ParseError. So does a file that is not a spreadsheet, and a CSV file that is not UTF-8. A file saved with a password throws a RuntimeError. Opening a file never runs its macros and never opens its links. If the file changes on disk after Reader::open, the next call throws a LogicError.

The examples below open an upload, read a CSV file, catch the error of a damaged file, and read a CSV file with ; in Windows-1252.

Parameters

  • $sourceCore\Bytes::Source
  • $optionsOpenOptionsdefault null

Return value

Returns Reader.

Examples

Open an uploaded file

<?nvs
// Shows how to open the bytes of an uploaded xlsx file and read its first sheet.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
class Import
{
// An upload is `tainted bytes`. `Reader::open` accepts it directly.
public static function count(tainted bytes $upload): int
{
int $count = 0;
foreach (Reader::open($upload)->rows(0, { header: true }) as var $row) {
$count++;
}
return $count;
}
}
// This makes the upload. In a real program the bytes come from the request.
bytes $upload = Workbook::fromRows([['item' => 'Widget'], ['item' => 'Gadget'], ['item' => 'Gizmo']])->bytes();
echo Import::count($upload), " items in the upload\n";
Output
3 items in the upload

A csv file

<?nvs
// Shows that `Reader::open` also reads a CSV file, with the same calls as an xlsx file.
use Novis\Spreadsheet\Reader;
// A CSV file has one sheet, `Sheet1`.
bytes $file = "Item,Price\nWidget,2.50\nGadget,4\n" as bytes;
var $reader = Reader::open($file);
echo $reader->sheets()[0]->name, "\n";
// Each value in a CSV file is a string. So `2.50` stays `2.50`.
foreach ($reader->rows(0, { header: true }) as var $row) {
echo $row->values['Item'], ' costs ', $row->values['Price'], "\n";
}
Output
Sheet1
Widget costs 2.50
Gadget costs 4

A file that is not a workbook

<?nvs
// Shows what happens when an upload is a damaged spreadsheet file.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
// This file is cut after its first 100 bytes, as an upload that did not finish.
bytes $whole = Workbook::fromRows([['item' => 'Widget']])->bytes();
bytes $cut = Core\Bytes::slice($whole, 0, 100);
// `Reader::open` throws a `ParseError`. You can catch it and tell the person what is wrong.
try {
Reader::open($cut);
echo "opened\n";
} catch (ParseError $e) {
echo 'The upload could not be read: ', $e->message, "\n";
}
Output
The upload could not be read: The file is not a valid workbook: it has no zip directory.

A csv file with semicolons in windows 1252

<?nvs
// Shows how to read a CSV file that uses `;` between values and is not UTF-8 text.
use Novis\Spreadsheet\Reader;
// Excel in many European countries saves CSV files like this one.
bytes $file = Core\Encoding::encodeText("Artikel;Preis\nMüsli;3,50\nKäse;4,20\n", Core\Charset::Windows1252);
// `csv` sets the separator and the charset of the file.
var $reader = Reader::open($file, { csv: { separator: ';', charset: Core\Charset::Windows1252 } });
foreach ($reader->rows(0, { header: true }) as var $row) {
echo $row->values['Artikel'], ' costs ', $row->values['Preis'], "\n";
}
Output
Müsli costs 3,50
Käse costs 4,20