Skip to main content

uiuifree_text_data/
excel.rs

1use calamine::{open_workbook, Reader, Xlsx};
2
3pub struct Excel {
4    path: String,
5}
6
7impl Excel {
8    pub fn new(path: &str) -> Excel {
9        Excel {
10            path: path.to_string()
11        }
12    }
13
14    pub fn to_csv(self, sheet: &str, output_file: &str) {
15        let mut excel: Xlsx<_> = open_workbook(self.path).unwrap();
16        let mut outputs = Vec::new();
17        if let Some(Ok(r)) = excel.worksheet_range(sheet) {
18            for row in r.rows() {
19                let mut output_row = vec![];
20                for value in row {
21                    output_row.push(value.to_string().replace("_x000D_", ""));
22                }
23                outputs.push(output_row);
24            }
25        }
26        let mut writer = csv::Writer::from_path(output_file).unwrap();
27        for row in outputs {
28            writer.write_record(row.clone()).unwrap();
29        }
30    }
31}