Skip to main content

Crate umya_spreadsheet

Crate umya_spreadsheet 

Source
Expand description

§umya-spreadsheet

A pure rust library for reading and writing Microsoft Excel (xlsx) files.

§Example

Result Image

§Reader or New File

use umya_spreadsheet::*;

// Reader
let path = std::path::Path::new("./tests/test_files/aaa.xlsx");
let mut book = reader::xlsx::read(path).unwrap();

// Lazy Reader
// Delays loading of worksheets until they are needed.
// Can improve performance when loading large files.
let path = std::path::Path::new("./tests/test_files/aaa.xlsx");
let mut book = reader::xlsx::lazy_read(path).unwrap();

// New file
let mut book = new_file();

§New Worksheet

use umya_spreadsheet::*;

let mut book = new_file();

// New worksheet
let _ = book.new_sheet("Sheet2");

§Copy Worksheet

use umya_spreadsheet::*;

let mut book = new_file();

let mut clone_sheet = book.sheet(0).unwrap().clone();
clone_sheet.set_name("New Sheet");
let _ = book.add_sheet(clone_sheet);

§Change Value

use umya_spreadsheet::*;

let mut book = new_file();
let _unused = book.new_sheet("Sheet2");

// Change value using string cell address
book.sheet_by_name_mut("Sheet2")
    .unwrap()
    .cell_mut("A1")
    .set_value("TEST1");
book.sheet_by_name_mut("Sheet2")
    .unwrap()
    .cell_mut("B2")
    .set_value_number(1);
book.sheet_by_name_mut("Sheet2")
    .unwrap()
    .cell_mut("C3")
    .set_value_bool(true);

// Change value using tuple cell address
book.sheet_mut(1)
    .unwrap()
    .cell_mut((1, 1))
    .set_value("TEST1");
book.sheet_mut(1)
    .unwrap()
    .cell_mut((2, 2))
    .set_value_number(1);
book.sheet_mut(1)
    .unwrap()
    .cell_mut((3, 3))
    .set_value_bool(true);

§Read Value

use umya_spreadsheet::*;

let mut book = new_file();
let _unused = book.new_sheet("Sheet2");
book.sheet_by_name_mut("Sheet2")
    .unwrap()
    .cell_mut("A1")
    .set_value("TEST1");

// Read value by string cell address
let a1_value = book.sheet_by_name("Sheet2").unwrap().value("A1");

// Read value by tuple cell address
let a1_value = book.sheet(1).unwrap().value((1, 1));

// Read formatted value by string cell address
let a1_value = book.sheet(1).unwrap().formatted_value("A1");

assert_eq!("TEST1", a1_value);

§Change Style

More examples can be found in the Style module.

use umya_spreadsheet::*;

let mut book = new_file();
let _unused = book.new_sheet("Sheet2");

// Add a bottom border using string cell address
book.sheet_by_name_mut("Sheet2")
    .unwrap()
    .style_mut("A1")
    .borders_mut()
    .bottom_mut()
    .set_border_style(Border::BORDER_MEDIUM);

// Add a bottom border using tuple cell address
book.sheet_mut(1)
    .unwrap()
    .style_mut((1, 1))
    .borders_mut()
    .bottom_mut()
    .set_border_style(Border::BORDER_MEDIUM);

§Insert or Remove Rows/Columns

Result Image

use umya_spreadsheet::*;

let mut book = new_file();

// Insert rows
book.insert_new_row("Sheet1", 2, 3);

// Insert columns by column name
book.insert_new_column("Sheet1", "B", 3);

// Insert columns by index
book.insert_new_column_by_index("Sheet1", 2, 3);

// Remove rows
book.remove_row("Sheet1", 6, 2);

// Remove columns by column name
book.remove_column("Sheet1", "F", 2);

// Remove columns by index
book.remove_column_by_index("Sheet1", 6, 2);

§Writer

use umya_spreadsheet::*;

let mut book = new_file();
let _unused = book.new_sheet("Sheet2");

// Write to a file
let path = std::path::Path::new("C:/spread_test_data/ccc.xlsx");
let _unused = writer::xlsx::write(&book, path);

Re-exports§

pub use self::structs::*;

Modules§

helper
reader
file reader library.
structs
traits
store structs.
writer
file writer library.

Macros§

from_err
A macro that implements the From trait for converting from one error type to another.
set_string_from_xml
xml_read_loop

Constants§

VERSION
The version of the crate, as specified in the Cargo.toml file.
VERSION_MAJOR
The major version of the crate, as specified in the Cargo.toml file.
VERSION_MINOR
The minor version of the crate, as specified in the Cargo.toml file.
VERSION_PATCH
The patch version of the crate, as specified in the Cargo.toml file.
VERSION_PRE
The pre-release flag of the crate, if any, as specified in the Cargo.toml file.

Functions§

new_file
Creates a new workbook with default settings.
new_file_empty_worksheet
Creates a new empty workbook without any worksheets.