How to Load a Spreadsheet in Rust

How to Load a Spreadsheet in Rust

Aspose.Cells FOSS for Rust loads Excel XLSX workbooks directly from the package format — no Microsoft Office required. This guide installs the aspose-cells-foss-rust crate via Cargo, loads a workbook with Workbook::load_xlsx, and handles imperfect files with LoadOptions repair flags and LoadDiagnostics.

Step-by-Step Guide

Step 1: Install the Package

Add the crate to your Cargo.toml as a Cargo git dependency (the crate is not yet published on crates.io):

# Cargo.toml
[dependencies]
aspose-cells-foss-rust = { git = "https://github.com/aspose-cells-foss/Aspose.Cells-FOSS-for-Rust" }

Then build the project:

cargo build

Step 2: Import Required Classes

use aspose_cells_foss_rust::{LoadOptions, Workbook};
use std::error::Error;

Step 3: Create a Sample File to Load

If you do not already have a workbook on disk, write one first:

let mut workbook = Workbook::new();
{
    let mut worksheets = workbook.get_worksheets_mut();
    let sheet = worksheets.get(0)?;
    let mut cells = sheet.get_cells_mut();

    cells.get("A1")?.put_value_string("Item")?;
    cells.get("B1")?.put_value_string("Quantity")?;
    cells.get("A2")?.put_value_string("Apples")?;
    cells.get("B2")?.put_value_i32(12)?;
}
workbook.save("inventory.xlsx")?;

Step 4: Load the Workbook

Workbook::load_xlsx opens a well-formed XLSX file:

let loaded = Workbook::load_xlsx("inventory.xlsx")?;
let sheet = loaded.worksheet("Sheet1")?;
let cells = sheet.get_cells();
println!("A2 = {}", cells.get("A2")?.display_string_value());

Step 5: Load Defensively with Repair Options

For files from third parties, enable the repair flags and inspect the diagnostics the loader collected:

let options = LoadOptions {
    try_repair_package: true,
    try_repair_xml: true,
    ..LoadOptions::default()
};

let loaded = Workbook::load_xlsx_with_options("inventory.xlsx", &options)?;
println!(
    "Loaded workbook with {} worksheet(s) and {} diagnostic issue(s).",
    loaded.get_worksheets().count(),
    loaded.get_load_diagnostics().issues().len()
);

Common Issues and Fixes

The load call returns an error for a file that opens in Excel. Enable try_repair_package and try_repair_xml in LoadOptions and load via Workbook::load_xlsx_with_options — Excel tolerates some package irregularities that strict loading rejects.

worksheet("Sheet1") fails after loading. The sheet may have a different name. Enumerate via get_worksheets or fetch by index through the worksheet collection instead.

Values read back as empty strings. Use display_string_value on the cell for its display text and check value_type to see what is actually stored.

Formulas show stale results. The loader preserves the cached values stored in the file; recalculation is the consumer’s concern. When writing, always pass a correct cached value to put_formula_with_cached_value.

Frequently Asked Questions

Which formats can be loaded?

XLSX only in the current release. The LoadFormat enum covers Auto and Xlsx.

Do I need Microsoft Office installed?

No. The crate parses the XLSX package directly; no Office, COM, or external runtime is involved.

How do I know whether the loader repaired anything?

Read get_load_diagnostics on the loaded workbook — LoadDiagnostics lists the issues encountered during loading.

See Also