pub struct Xlsx<RS> { /* private fields */ }
Expand description
A struct representing xml zipped excel file Xlsx, Xlsm, Xlam
Implementations§
Source§impl<RS: Read + Seek> Xlsx<RS>
impl<RS: Read + Seek> Xlsx<RS>
Sourcepub fn load_merged_regions(&mut self) -> Result<(), XlsxError>
pub fn load_merged_regions(&mut self) -> Result<(), XlsxError>
Load the merged regions in the workbook.
A merged region in Excel is a range of cells that have been merged to act as a single cell. It is often used to create headers or titles that span multiple columns or rows.
This method must be called before accessing the merged regions using the methods:
These methods are explained below.
§Errors
Sourcepub fn merged_regions(&self) -> &Vec<(String, String, Dimensions)>
pub fn merged_regions(&self) -> &Vec<(String, String, Dimensions)>
Get the merged regions for all the worksheets in a workbook.
The function returns a ref to a vector of tuples containing the sheet
name, the sheet path, and the Dimensions
of the merged region. The
middle element of the tuple can generally be ignored.
The Xlsx::load_merged_regions()
method must be called before calling
this method.
§Examples
An example of getting all the merged regions in an Excel workbook.
use calamine::{open_workbook, Error, Xlsx};
fn main() -> Result<(), Error> {
let path = "tests/merged_range.xlsx";
// Open the workbook.
let mut workbook: Xlsx<_> = open_workbook(path)?;
// Load the merged regions in the workbook.
workbook.load_merged_regions()?;
// Get all the merged regions in the workbook.
let merged_regions = workbook.merged_regions();
// Print the sheet name and dimensions of each merged region.
for (sheet_name, _, dimensions) in merged_regions {
println!("{sheet_name}: {dimensions:?}");
}
Ok(())
}
Output:
Sheet1: Dimensions { start: (0, 7), end: (1, 7) }
Sheet1: Dimensions { start: (0, 0), end: (1, 0) }
Sheet1: Dimensions { start: (0, 1), end: (1, 1) }
Sheet1: Dimensions { start: (0, 2), end: (1, 3) }
Sheet1: Dimensions { start: (2, 2), end: (2, 3) }
Sheet1: Dimensions { start: (3, 2), end: (3, 3) }
Sheet1: Dimensions { start: (0, 4), end: (1, 4) }
Sheet1: Dimensions { start: (0, 5), end: (1, 5) }
Sheet1: Dimensions { start: (0, 6), end: (1, 6) }
Sheet2: Dimensions { start: (0, 0), end: (3, 0) }
Sheet2: Dimensions { start: (2, 2), end: (3, 3) }
Sheet2: Dimensions { start: (0, 5), end: (3, 7) }
Sheet2: Dimensions { start: (0, 1), end: (1, 1) }
Sheet2: Dimensions { start: (0, 2), end: (1, 3) }
Sheet2: Dimensions { start: (0, 4), end: (1, 4) }
Sourcepub fn merged_regions_by_sheet(
&self,
name: &str,
) -> Vec<(&String, &String, &Dimensions)>
pub fn merged_regions_by_sheet( &self, name: &str, ) -> Vec<(&String, &String, &Dimensions)>
Get the merged regions in a workbook by the sheet name.
The function returns a vector of tuples containing the sheet name, the
sheet path, and the Dimensions
of the merged region. The first two
elements of the tuple can generally be ignored.
The Xlsx::load_merged_regions()
method must be called before calling
this method.
§Parameters
sheet_name
: The name of the worksheet to get the merged regions from.
§Examples
An example of getting the merged regions in an Excel workbook, by individual worksheet.
use calamine::{open_workbook, Error, Reader, Xlsx};
fn main() -> Result<(), Error> {
let path = "tests/merged_range.xlsx";
// Open the workbook.
let mut workbook: Xlsx<_> = open_workbook(path)?;
// Get the names of all the sheets in the workbook.
let sheet_names = workbook.sheet_names();
// Load the merged regions in the workbook.
workbook.load_merged_regions()?;
for sheet_name in &sheet_names {
println!("{sheet_name}: ");
// Get the merged regions in the current sheet.
let merged_regions = workbook.merged_regions_by_sheet(sheet_name);
for (_, _, dimensions) in &merged_regions {
// Print the dimensions of each merged region.
println!(" {dimensions:?}");
}
}
Ok(())
}
Output:
Sheet1:
Dimensions { start: (0, 7), end: (1, 7) }
Dimensions { start: (0, 0), end: (1, 0) }
Dimensions { start: (0, 1), end: (1, 1) }
Dimensions { start: (0, 2), end: (1, 3) }
Dimensions { start: (2, 2), end: (2, 3) }
Dimensions { start: (3, 2), end: (3, 3) }
Dimensions { start: (0, 4), end: (1, 4) }
Dimensions { start: (0, 5), end: (1, 5) }
Dimensions { start: (0, 6), end: (1, 6) }
Sheet2:
Dimensions { start: (0, 0), end: (3, 0) }
Dimensions { start: (2, 2), end: (3, 3) }
Dimensions { start: (0, 5), end: (3, 7) }
Dimensions { start: (0, 1), end: (1, 1) }
Dimensions { start: (0, 2), end: (1, 3) }
Dimensions { start: (0, 4), end: (1, 4) }
Sourcepub fn load_tables(&mut self) -> Result<(), XlsxError>
pub fn load_tables(&mut self) -> Result<(), XlsxError>
Load the worksheet tables from the XLSX file.
Tables in Excel are a way of grouping a range of cells into a single
entity that has common formatting or that can be referenced in formulas.
In calamine
, tables can be read as a Table
object and converted to
a data Range
for further processing.
Calamine does not automatically load table data from a workbook to avoid
unnecessary overhead. Instead you must explicitly load the table data
using the Xlsx::load_tables()
method. Once the tables have been loaded
the following methods can be used to extract and work with individual
tables:
Xlsx::table_by_name()
.Xlsx::table_by_name_ref()
.Xlsx::table_names()
.Xlsx::table_names_in_sheet()
.
These methods are explained below. See also the Table
struct for
additional methods that can be used when working with tables.
§Errors
Sourcepub fn table_names(&self) -> Vec<&String>
pub fn table_names(&self) -> Vec<&String>
Get the names of all the tables in the workbook.
Read all the table names in the workbook. This can be used in
conjunction with Xlsx::table_by_name()
to iterate over the tables in
the workbook.
§Panics
Panics if tables have not been loaded via Xlsx::load_tables()
.
§Examples
An example of getting the names of all the tables in an Excel workbook.
use calamine::{open_workbook, 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 all the table names in the workbook.
let table_names = workbook.table_names();
// Check the table names.
assert_eq!(
table_names,
vec!["Inventory", "Pricing", "Sales_Bob", "Sales_Alice"]
);
Ok(())
}
Sourcepub fn table_names_in_sheet(&self, sheet_name: &str) -> Vec<&String>
pub fn table_names_in_sheet(&self, sheet_name: &str) -> Vec<&String>
Get the names of all the tables in a worksheet.
Read all the table names in a worksheet. This can be used in conjunction
with Xlsx::table_by_name()
to iterate over the tables in the
worksheet.
§Parameters
sheet_name
: The name of the worksheet to get the table names from.
§Panics
Panics if tables have not been loaded via Xlsx::load_tables()
.
§Examples
An example of getting the names of all the tables in an Excel workbook, sheet by sheet.
use calamine::{open_workbook, Error, Reader, Xlsx};
fn main() -> Result<(), Error> {
let path = "tests/table-multiple.xlsx";
// Open the workbook.
let mut workbook: Xlsx<_> = open_workbook(path)?;
// Get the names of all the sheets in the workbook.
let sheet_names = workbook.sheet_names();
// Load the tables in the workbook.
workbook.load_tables()?;
for sheet_name in &sheet_names {
// Get the table names in the current sheet.
let table_names = workbook.table_names_in_sheet(sheet_name);
// Print the associated table names.
println!("{sheet_name} contains tables: {table_names:?}");
}
Ok(())
}
Output:
Sheet1 contains tables: ["Inventory"]
Sheet2 contains tables: ["Pricing"]
Sheet3 contains tables: ["Sales_Bob", "Sales_Alice"]
Sourcepub fn table_by_name(
&mut self,
table_name: &str,
) -> Result<Table<Data>, XlsxError>
pub fn table_by_name( &mut self, table_name: &str, ) -> Result<Table<Data>, XlsxError>
Get a worksheet table by name.
This method retrieves a Table
from the workbook by its name. The
table will contain an owned copy of the worksheet data in the table
range.
§Parameters
table_name
: The name of the table to retrieve.
§Errors
§Panics
Panics if tables have not been loaded via Xlsx::load_tables()
.
§Examples
An example of getting an Excel worksheet table by its name. The file in this example contains 4 tables spread across 3 worksheets. This example gets an owned copy of the worksheet data in the table area.
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.
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();
// Do something with the data using the `Range` APIs. In this case
// we will just check for a cell value.
assert_eq!(
data_range.get((0, 1)),
Some(&Data::String("Apple".to_string()))
);
Ok(())
}
Sourcepub fn table_by_name_ref(
&mut self,
table_name: &str,
) -> Result<Table<DataRef<'_>>, XlsxError>
pub fn table_by_name_ref( &mut self, table_name: &str, ) -> Result<Table<DataRef<'_>>, XlsxError>
Get a worksheet table by name, with referenced data.
This method retrieves a Table
from the workbook by its name. The
table will contain an borrowed/referenced copy of the worksheet data in
the table range. This is more efficient than Xlsx::table_by_name()
for large tables.
§Parameters
table_name
: The name of the table to retrieve.
§Errors
§Panics
Panics if tables have not been loaded via Xlsx::load_tables()
.
§Examples
An example of getting an Excel worksheet table by its name. The file in this example contains 4 tables spread across 3 worksheets. This example gets a borrowed/referenced copy of the worksheet data in the table area.
use calamine::{open_workbook, DataRef, 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.
let table = workbook.table_by_name_ref("Inventory")?;
// Get the data range of the table. The data type is `&Range<DataRef<'_>>`.
let data_range = table.data();
// Do something with the data using the `Range` APIs. In this case
// we will just check for a cell value.
assert_eq!(
data_range.get((0, 1)),
Some(&DataRef::SharedString("Apple"))
);
Ok(())
}
Sourcepub fn worksheet_merge_cells(
&mut self,
name: &str,
) -> Option<Result<Vec<Dimensions>, XlsxError>>
pub fn worksheet_merge_cells( &mut self, name: &str, ) -> Option<Result<Vec<Dimensions>, XlsxError>>
Get the merged cells/regions in a workbook by the sheet name.
Merged cells in Excel are a range of cells that have been merged to act as a single cell. It is often used to create headers or titles that span multiple columns or rows.
The function returns a vector of Dimensions
of the merged region.
This is wrapped in a Result
and an Option
.
§Parameters
sheet_name
: The name of the worksheet to get the merged regions from.
§Errors
§Examples
An example of getting the merged regions/cells in an Excel workbook, by individual worksheet.
use calamine::{open_workbook, Error, Reader, Xlsx};
fn main() -> Result<(), Error> {
let path = "tests/merged_range.xlsx";
// Open the workbook.
let mut workbook: Xlsx<_> = open_workbook(path)?;
// Get the names of all the sheets in the workbook.
let sheet_names = workbook.sheet_names();
for sheet_name in &sheet_names {
println!("{sheet_name}: ");
// Get the merged cells in the current sheet.
let merge_cells = workbook.worksheet_merge_cells(sheet_name);
if let Some(dimensions) = merge_cells {
let dimensions = dimensions?;
// Print the dimensions of each merged region.
for dimension in &dimensions {
println!(" {dimension:?}");
}
}
}
Ok(())
}
Output:
Sheet1:
Dimensions { start: (0, 7), end: (1, 7) }
Dimensions { start: (0, 0), end: (1, 0) }
Dimensions { start: (0, 1), end: (1, 1) }
Dimensions { start: (0, 2), end: (1, 3) }
Dimensions { start: (2, 2), end: (2, 3) }
Dimensions { start: (3, 2), end: (3, 3) }
Dimensions { start: (0, 4), end: (1, 4) }
Dimensions { start: (0, 5), end: (1, 5) }
Dimensions { start: (0, 6), end: (1, 6) }
Sheet2:
Dimensions { start: (0, 0), end: (3, 0) }
Dimensions { start: (2, 2), end: (3, 3) }
Dimensions { start: (0, 5), end: (3, 7) }
Dimensions { start: (0, 1), end: (1, 1) }
Dimensions { start: (0, 2), end: (1, 3) }
Dimensions { start: (0, 4), end: (1, 4) }
Sourcepub fn worksheet_merge_cells_at(
&mut self,
sheet_index: usize,
) -> Option<Result<Vec<Dimensions>, XlsxError>>
pub fn worksheet_merge_cells_at( &mut self, sheet_index: usize, ) -> Option<Result<Vec<Dimensions>, XlsxError>>
Get the merged cells/regions in a workbook by the sheet index.
Merged cells in Excel are a range of cells that have been merged to act as a single cell. It is often used to create headers or titles that span multiple columns or rows.
The function returns a vector of Dimensions
of the merged region.
This is wrapped in a Result
and an Option
.
§Parameters
sheet_index
: The zero index of the worksheet to get the merged regions from.
§Errors
§Examples
An example of getting the merged regions/cells in an Excel workbook, by worksheet index.
use calamine::{open_workbook, Error, Xlsx};
fn main() -> Result<(), Error> {
let path = "tests/merged_range.xlsx";
// Open the workbook.
let mut workbook: Xlsx<_> = open_workbook(path)?;
// Get the merged cells in the first worksheet.
let merge_cells = workbook.worksheet_merge_cells_at(0);
if let Some(dimensions) = merge_cells {
let dimensions = dimensions?;
// Print the dimensions of each merged region.
for dimension in &dimensions {
println!("{dimension:?}");
}
}
Ok(())
}
Output:
Dimensions { start: (0, 7), end: (1, 7) }
Dimensions { start: (0, 0), end: (1, 0) }
Dimensions { start: (0, 1), end: (1, 1) }
Dimensions { start: (0, 2), end: (1, 3) }
Dimensions { start: (2, 2), end: (2, 3) }
Dimensions { start: (3, 2), end: (3, 3) }
Dimensions { start: (0, 4), end: (1, 4) }
Dimensions { start: (0, 5), end: (1, 5) }
Dimensions { start: (0, 6), end: (1, 6) }
Trait Implementations§
Source§impl<RS: Read + Seek> Reader<RS> for Xlsx<RS>
impl<RS: Read + Seek> Reader<RS> for Xlsx<RS>
Source§fn with_header_row(&mut self, header_row: HeaderRow) -> &mut Self
fn with_header_row(&mut self, header_row: HeaderRow) -> &mut Self
header_row
is None
, the first non-empty row will be used as header rowSource§fn vba_project(&mut self) -> Option<Result<Cow<'_, VbaProject>, XlsxError>>
fn vba_project(&mut self) -> Option<Result<Cow<'_, VbaProject>, XlsxError>>
VbaProject