partial_batch_reader/
partial_batch_reader.rs

1
2use std::collections::HashMap;
3
4use xlsx_batch_reader::{get_ord_from_tuple, read::XlsxBook, MAX_COL_NUM};
5
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let mut book = XlsxBook::new("xlsx/test.xlsx", true)?;
8    for shname in book.get_visible_sheets().clone() {
9        // left_ncol should not be 0
10        // the tail empty cells 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
11        let mut sheet = book.get_sheet_by_name(&shname, 100, 0, 1, MAX_COL_NUM, true)?;
12
13        let mut skip_until = HashMap::new();
14        skip_until.insert("A".into(), "col1".into());
15        skip_until.insert("C".into(), "col3".into());
16        sheet.with_skip_until(&skip_until);
17        let mut read_before = HashMap::new();
18        read_before.insert("B".into(), "sum".into());
19        sheet.with_read_before(&read_before);
20        // only rows after skip_until-row(included) and before read_before(excluded) will be returned
21
22        let captures = vec!["B2".into(), "C1".into()].into_iter().collect();
23        sheet.with_capture_vals(captures);
24        println!("captures: {:?}", sheet.get_captured_vals());
25
26        for batch in sheet {
27            let (rows_nums, rows_data) = batch?;
28            // empty rows will be skiped
29            for (row, cells) in rows_nums.into_iter().zip(rows_data) {
30                for (col, cel) in cells.into_iter().enumerate() {
31                    let val: String = cel.get()?.unwrap();   // supprted types: String, i64, f64, bool, NaiveDate
32                    println!("the value of {} is {val}; raw cell is {:?}", get_ord_from_tuple(row, (col+1) as u16)?, cel);  
33                }
34            }
35            // println!("captured values: {:?}", sheet.get_captured_vals());
36        };
37
38    }
39    Ok(())
40}