Crate calamine [] [src]

Rust Excel/OpenDocument reader

Status

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

Read both cell values and vba project.

Examples

use calamine::{Sheets, DataType};

// opens a new workbook
let mut workbook = Sheets::open(path).expect("Cannot open file");

// Read whole worksheet data and provide some statistics
if let 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 workbook.has_vba() {
    let mut vba = workbook.vba_project().expect("Cannot find VbaProject");
    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 &(ref name, ref formula) in workbook.defined_names().expect("Cannot get defined names!") {
    println!("name: {}, formula: {}", name, formula);
}

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

Modules

vba

Parse vbaProject.bin file

Structs

Cell

A struct to hold cell position and value

Error

The Error type.

Range

A struct which represents a squared selection of cells

Rows

An iterator to read Range struct row by row

Sheets

A wrapper struct over the spreadsheet file

UsedCells

A struct to iterate over used cells

Enums

CellErrorType

An enum to represent all different errors that can appear as a value in a worksheet cell

DataType

An enum to represent all different data types that can appear as a value in a worksheet cell

ErrorKind

The kind of an error.

Traits

ResultExt

Additional methods for Result, for easy interaction with this crate.

Type Definitions

Result

Convenient wrapper around std::Result.