Struct xlsx_batch_reader::read::XlsxBook
source · pub struct XlsxBook { /* private fields */ }Implementations§
source§impl XlsxBook
impl XlsxBook
sourcepub fn new<T: AsRef<Path>>(path: T, load_share: bool) -> Result<XlsxBook>
pub fn new<T: AsRef<Path>>(path: T, load_share: bool) -> Result<XlsxBook>
load_share: if set to false, you should call load_share_strings before reading data. it should usually be true. If you only need to obtain the sheet names, you can set it false to open the file faster.
Examples found in repository?
examples/simple_reader.rs (line 5)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut book = XlsxBook::new("xlsx/test.xlsx", true)?;
for shname in book.get_visible_sheets().clone() {
// left_ncol should not be 0
// the tail empty rows will be ignored, if you want the length of cells in each row is fixed, you can set right_ncol to a number not MAX_COL_NUM
let sheet = book.get_sheet_by_name(&shname, 100, 0, 1, MAX_COL_NUM, false)?;
for batch in sheet {
let (rows_nums, rows_data) = batch?;
// empty rows will be skiped
for (row, cells) in rows_nums.into_iter().zip(rows_data) {
for (col, cel) in cells.into_iter().enumerate() {
let val: String = cel.get()?.unwrap(); // supprted types: String, i64, f64, bool, NaiveDate
println!("the value of {} is {val}", get_ord_from_tuple(row, (col+1) as u16)?);
}
}
};
}
Ok(())
}More examples
examples/merged_range.rs (line 5)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut book = XlsxBook::new("xlsx/test.xlsx", true)?;
for shname in book.get_visible_sheets().clone() {
// left_ncol should not be 0
// each row will have 3 cells.
let mut sheet = book.get_sheet_by_name(&shname, 100, 0, 1, get_num_from_ord("C".as_bytes())?, true)?;
// this is not necessary, if you don't care about the headers.
let (_, _header) = sheet.get_header_row()?;
if let Some((_rows_nums, _rows_data)) = sheet.get_remaining_cells()? {
// some code
};
// should be called when all data have been scaned.
let merged_rngs = sheet.get_merged_ranges()?;
match is_merged_cell(merged_rngs, 2, get_num_from_ord("A".as_bytes())?) {
(true, None) => {
println!("a merged cell(not top left cell)");
},
(true, Some((nrow, ncol))) => {
println!("a merged cell(top left cell), taking {nrow} row(s) and {ncol} column(s)");
},
_ => {
println!("not a merged cell");
}
}
}
Ok(())
}get hidden sheets
sourcepub fn get_visible_sheets(&self) -> &Vec<String>
pub fn get_visible_sheets(&self) -> &Vec<String>
get visible sheets
Examples found in repository?
examples/simple_reader.rs (line 6)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut book = XlsxBook::new("xlsx/test.xlsx", true)?;
for shname in book.get_visible_sheets().clone() {
// left_ncol should not be 0
// the tail empty rows will be ignored, if you want the length of cells in each row is fixed, you can set right_ncol to a number not MAX_COL_NUM
let sheet = book.get_sheet_by_name(&shname, 100, 0, 1, MAX_COL_NUM, false)?;
for batch in sheet {
let (rows_nums, rows_data) = batch?;
// empty rows will be skiped
for (row, cells) in rows_nums.into_iter().zip(rows_data) {
for (col, cel) in cells.into_iter().enumerate() {
let val: String = cel.get()?.unwrap(); // supprted types: String, i64, f64, bool, NaiveDate
println!("the value of {} is {val}", get_ord_from_tuple(row, (col+1) as u16)?);
}
}
};
}
Ok(())
}More examples
examples/merged_range.rs (line 6)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut book = XlsxBook::new("xlsx/test.xlsx", true)?;
for shname in book.get_visible_sheets().clone() {
// left_ncol should not be 0
// each row will have 3 cells.
let mut sheet = book.get_sheet_by_name(&shname, 100, 0, 1, get_num_from_ord("C".as_bytes())?, true)?;
// this is not necessary, if you don't care about the headers.
let (_, _header) = sheet.get_header_row()?;
if let Some((_rows_nums, _rows_data)) = sheet.get_remaining_cells()? {
// some code
};
// should be called when all data have been scaned.
let merged_rngs = sheet.get_merged_ranges()?;
match is_merged_cell(merged_rngs, 2, get_num_from_ord("A".as_bytes())?) {
(true, None) => {
println!("a merged cell(not top left cell)");
},
(true, Some((nrow, ncol))) => {
println!("a merged cell(top left cell), taking {nrow} row(s) and {ncol} column(s)");
},
_ => {
println!("not a merged cell");
}
}
}
Ok(())
}if set load_share to false, you should call load_share_strings before reading data
sourcepub fn get_sheet_by_name(
&mut self,
sht_name: &String,
iter_batch: usize,
skip_rows: u32,
left_ncol: ColNum,
right_ncol: ColNum,
first_row_is_header: bool
) -> Result<XlsxSheet<'_>>
pub fn get_sheet_by_name( &mut self, sht_name: &String, iter_batch: usize, skip_rows: u32, left_ncol: ColNum, right_ncol: ColNum, first_row_is_header: bool ) -> Result<XlsxSheet<'_>>
sht_name: sheet name
iter_batch: The number of rows per batch
skip_rows: number of skipped rows
left_ncol: Starting column (included), with 1 as the starting value
right_ncol: Terminate columns (including), MAX-COL_NUM to get non fixed termination columns
Examples found in repository?
examples/simple_reader.rs (line 9)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut book = XlsxBook::new("xlsx/test.xlsx", true)?;
for shname in book.get_visible_sheets().clone() {
// left_ncol should not be 0
// the tail empty rows will be ignored, if you want the length of cells in each row is fixed, you can set right_ncol to a number not MAX_COL_NUM
let sheet = book.get_sheet_by_name(&shname, 100, 0, 1, MAX_COL_NUM, false)?;
for batch in sheet {
let (rows_nums, rows_data) = batch?;
// empty rows will be skiped
for (row, cells) in rows_nums.into_iter().zip(rows_data) {
for (col, cel) in cells.into_iter().enumerate() {
let val: String = cel.get()?.unwrap(); // supprted types: String, i64, f64, bool, NaiveDate
println!("the value of {} is {val}", get_ord_from_tuple(row, (col+1) as u16)?);
}
}
};
}
Ok(())
}More examples
examples/merged_range.rs (line 9)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut book = XlsxBook::new("xlsx/test.xlsx", true)?;
for shname in book.get_visible_sheets().clone() {
// left_ncol should not be 0
// each row will have 3 cells.
let mut sheet = book.get_sheet_by_name(&shname, 100, 0, 1, get_num_from_ord("C".as_bytes())?, true)?;
// this is not necessary, if you don't care about the headers.
let (_, _header) = sheet.get_header_row()?;
if let Some((_rows_nums, _rows_data)) = sheet.get_remaining_cells()? {
// some code
};
// should be called when all data have been scaned.
let merged_rngs = sheet.get_merged_ranges()?;
match is_merged_cell(merged_rngs, 2, get_num_from_ord("A".as_bytes())?) {
(true, None) => {
println!("a merged cell(not top left cell)");
},
(true, Some((nrow, ncol))) => {
println!("a merged cell(top left cell), taking {nrow} row(s) and {ncol} column(s)");
},
_ => {
println!("not a merged cell");
}
}
}
Ok(())
}Auto Trait Implementations§
impl Freeze for XlsxBook
impl RefUnwindSafe for XlsxBook
impl Send for XlsxBook
impl Sync for XlsxBook
impl Unpin for XlsxBook
impl UnwindSafe for XlsxBook
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more