jflib-sheet-io 0.2.2

XLSX and CSV import and export: workbooks and delimited text as typed tables
Documentation

jflib-sheet-io

Spreadsheet import and export for JFLib: Excel workbooks (.xlsx, .xlsm, .xltx, .xltm, transitional and strict Office Open XML) and CSV files, both delivered as the same typed Table, and written back from typed rows. It reads the values a file stores, the cached result of a formula included, and never evaluates or writes a formula. It is plain Rust, without the standard library and without a line of unsafe code.

Why a crate of its own: JFLib keeps one crate per foreign format family, like dxf, gltf and pdf. jfWord's doc_io would pull its document model, layout and text into every user of a spreadsheet reader; archive holds containers, not their content; base::encoding is strict Unicode by design, while CSV files from Excel come in Windows-1252. CSV sits next to XLSX because both deliver the same table.

How it is used

use jflib::sheet_io::{csv, xlsx, Value};

// a workbook: the sheets and names first, the cells of a sheet when asked for
let wb = xlsx::Workbook::open(&bytes, xlsx::Limits::default())?;
for (i, s) in wb.sheets().iter().enumerate() {
    // s.name, s.state (Visible, Hidden, VeryHidden), s.kind (Worksheet, Chartsheet, Other)
}
let first = wb.sheet_table(0, true)?;                  // the used range, first row as names
let area = wb.area("'Tab 1'!$A$1:$C$10").unwrap();     // or a defined name's area
let table = wb.table(&area, true)?;
for name in wb.names() {
    // name.name, name.scope, name.formula, name.area (one rectangle), name.table (a ListObject)
}
let grid = wb.sheet(0)?;                               // sparse: get(row, col), rows(), used()
if let Value::Date(serial) = grid.get(1, 2) {
    let when = jflib::sheet_io::civil(*serial);        // an OLE Automation date
}

// a CSV file: encoding and delimiter detected, Excel's "sep=;" honoured
let file = csv::read(&bytes, &csv::Options::default())?;
let table = csv::to_table(&file, true, true, 4_000_000)?;   // header row, typed, at most 4M values

// writing: a table as a workbook (bold header in a frozen pane, a filter) and as German CSV
let mut sheet = xlsx::SheetOut::from_table("Bericht", &table);
sheet.autofilter = true;
let bytes = xlsx::write(&xlsx::BookOut { sheets: vec![sheet] }, &xlsx::WriteOptions::default())?;
let text = csv::write_table(&table, &csv::WriteOptions::GERMAN)?;   // ; and 1,5, UTF-8 with BOM

Values are Empty, Number, Text, Bool, Date (an OLE Automation date: days since 1899-12-30, as Visio uses them; Excel's 1904 date system and its fake 1900-02-29 are converted) and Error (#N/A, #DIV/0! ...). Value::text gives the display form, with dates in ISO 8601. Column names of a table are trimmed; an empty one is F<n> as Excel's OLE DB driver names it, a repeated one gets a number (Name1).

What the XLSX reader knows: shared strings with rich runs (phonetic runs left out), xml:space, Excel's _xHHHH_ escapes, inline strings, booleans, errors, ISO dates (t="d"), dates by built-in and custom number formats ([h]:mm is an elapsed time), rows and cells without positions or out of order, defined names global and sheet-local (Excel's _xlnm.* included), the tables (ListObjects) of the worksheets, hidden sheets and chartsheets. The workbook part is found through the package relationship and the sheets through the workbook's relationships, never by guessing paths. Macros of an .xlsm are never read. A binary .xlsb or an old .xls is Error::NotXlsx.

What the CSV reader knows: RFC 4180 and what Excel writes -- quoted fields with "", delimiters and line breaks inside quotes, CR LF, LF and CR records, a last line with or without a line break, Excel's sep= line, UTF-8 with and without BOM, UTF-16 (Excel's "Unicode text"), Windows-1252. The delimiter is detected among TAB, ;, , and | (csv::detect_delimiter says how). It is lenient where Excel is: a quote inside an unquoted field is kept, text after a closing quote too. German numbers (1.234,5) are read when ; is the delimiter.

What the writers do: xlsx::write makes a package Excel opens without repair -- texts as shared strings with Excel's _xHHHH_ escapes, dates as serials with the number formats Excel picks itself, booleans, errors, a bold header kept in view, column widths given or estimated, a filter -- and refuses what Excel cannot hold (rows, columns, 32 767 characters a cell, sheet names) with xlsx::WriteError instead of writing a file Excel would repair. csv::write writes RFC 4180 with the delimiter, line end, encoding (UTF-8 with or without BOM, UTF-16, Windows-1252, which refuses a character it lacks instead of writing ?) and decimal separator asked for, dates in ISO 8601; WriteOptions::guard_formulas puts a ' before texts a spreadsheet would run as a formula (off by default: it changes the data). Both are deterministic and bounded (xlsx::WriteOptions, csv::WriteOptions::max_bytes), and what they write reads back with the readers here.

Files are untrusted input

xlsx::Limits bounds the ZIP container before anything is inflated (parts, sizes, the compression ratio), the nesting of the XML, the rows, columns and cells of a sheet, the shared strings and their bytes, the text one sheet may hold (one long shared string in a million cells is refused, not copied), and the sheets and defined names of the workbook; names find their sheet in a map, and a worksheet or table part that several sheets share is read once. The XML reader (jflib-formats' strict formats::xml::Reader) takes no DTD and no entity but the five predefined ones, so no entity expansion of any kind. No size a file advertises (<dimension>, count, uniqueCount) sizes an allocation; cells are stored sparsely, so a cell at A1 and one at XFD1048576 are two cells, and a table of an area is refused when it would hold more than Limits::cells values. csv::CsvLimits bounds the bytes, records, fields and the bytes of a field; csv::to_table takes a count of values too, since one long record pads all others. Nothing recurses; a broken file is an error, never a panic.

How to get it

Like all of JFLib, the crate comes through the one dependency jflib, as jflib::sheet_io. All of JFLib's crates share one version and are released together. It runs on every platform JFLib supports.

What's new in 0.2.2

The crate is new: it reads XLSX workbooks and CSV files as typed tables, within limits for crafted files, and writes typed rows as XLSX. Everything is in the changelog.

Documentation

License

MIT or Apache-2.0, at your choice.