Excel Tables (named ranges with common formatting) are not loaded automatically to minimize overhead. You must explicitly call load_tables() before attempting to access them.
After loading, you can:
- List all table names in the workbook with
table_names(). - List table names within a specific sheet with
table_names_in_sheet(sheet_name). - Retrieve an owned copy of a table's data using
table_by_name(table_name). - Retrieve a borrowed/referenced copy of a table's data (more efficient for large tables) using
table_by_name_ref(table_name).
Both table_by_name and table_by_name_ref return a Table object containing the table's name, sheet name, columns, and a Range of data.
use calamine::{open_workbook, Data, Error, Xlsx};
fn main() -> Result<(), Error> {
let path = "tests/table-multiple.xlsx";
// Open the workbook.
let mut workbook: Xlsx<_> = open_workbook(path)?;
// Load the tables in the workbook.
workbook.load_tables()?;
// Get the table by name.
// This returns an owned copy of the worksheet data.
let table = workbook.table_by_name("Inventory")?;
// Get the data range of the table. The data type is `&Range<Data>`.
let data_range = table.data();
// Access cell values using the Range API.
assert_eq!(
data_range.get((0, 1)),
Some(&Data::String("Apple".to_string()))
);
Ok(())
}