Crate calamine

source ·
Expand description

Rust Excel/OpenDocument reader

Status

calamine is a pure Rust library to read Excel and OpenDocument Spreadsheet files.

Read both cell values and vba project.

Examples

use calamine::{Reader, open_workbook, Xlsx, DataType};

// opens a new workbook
let mut workbook: Xlsx<_> = open_workbook(path).expect("Cannot open file");

// Read whole worksheet data and provide some statistics
if let Some(Ok(range)) = workbook.worksheet_range("Sheet1") {
    let total_cells = range.get_size().0 * range.get_size().1;
    let non_empty_cells: usize = range.used_cells().count();
    println!("Found {} cells in 'Sheet1', including {} non empty cells",
             total_cells, non_empty_cells);
    // alternatively, we can manually filter rows
    assert_eq!(non_empty_cells, range.rows()
        .flat_map(|r| r.iter().filter(|&c| c != &DataType::Empty)).count());
}

// Check if the workbook has a vba project
if let Some(Ok(mut vba)) = workbook.vba_project() {
    let vba = vba.to_mut();
    let module1 = vba.get_module("Module 1").unwrap();
    println!("Module 1 code:");
    println!("{}", module1);
    for r in vba.get_references() {
        if r.is_missing() {
            println!("Reference {} is broken or not accessible", r.name);
        }
    }
}

// You can also get defined names definition (string representation only)
for name in workbook.defined_names() {
    println!("name: {}, formula: {}", name.0, name.1);
}

// Now get all formula!
let sheets = workbook.sheet_names().to_owned();
for s in sheets {
    println!("found {} formula in '{}'",
             workbook
                .worksheet_formula(&s)
                .expect("sheet not found")
                .expect("error while getting formula")
                .rows().flat_map(|r| r.iter().filter(|f| !f.is_empty()))
                .count(),
             s);
}

Modules

Parse vbaProject.bin file

Structs

A struct to hold cell position and value
Common file metadata
An OpenDocument Spreadsheet document parser
A struct which represents a squared selection of cells
A configured Range deserializer.
Builds a Range deserializer with some configuration options.
An iterator to read Range struct row by row
A struct to iterate over used cells
A struct representing an old xls format file (CFB)
A Xlsb reader
A struct representing xml zipped excel file Xlsx, Xlsm, Xlam

Enums

An enum to represent all different errors that can appear as a value in a worksheet cell
An enum to represent all different data types that can appear as a value in a worksheet cell
A cell deserialization specific error enum
A struct to handle any error and a message
An enum for ods specific errors
A wrapper over all sheets when the file type is not known at static time
An enum to handle Xls specific errors
A Xlsb specific error
An enum for Xlsx specific errors

Traits

A trait to constrain cells
A trait to share spreadsheets reader functions accross different FileTypes
Constructs a deserializer for a CellType.

Functions

Convenient function to open a file with a BufReader
Opens a workbook and define the file type at runtime.