1
2
3
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use json::{JsonValue};
use rust_xlsxwriter::{Format, Workbook, Worksheet, FormatAlign};
use crate::Head;
pub struct Excel {
filepath: String,
page: usize,
excel: Workbook,
}
impl Excel {
pub fn new(filepath: &str) -> Self {
Self {
filepath: filepath.to_string(),
page: 0,
excel: Workbook::new(),
}
}
pub fn set_page(&mut self, page: usize, nane: &str, heads: Vec<Head>, data: JsonValue) -> &mut Self {
self.page = page;
let mut worksheet = Worksheet::new();
worksheet.set_name(nane).unwrap();
let format1 = Format::new().set_align(FormatAlign::Center);
let mut line = 0;
let mut col = 0;
for head in heads.clone() {
worksheet.set_column_width(col, head.width).unwrap();
worksheet.write_string_with_format(line, col, &*head.title, &format1.clone()).unwrap();
col += 1;
}
line += 1;
for row in data.members() {
col = 0;
for head in heads.clone() {
if row[head.field.clone()].is_empty() {
col += 1;
continue;
}
worksheet.write_string_with_format(line, col, &*row[head.field].to_string(), &format1).unwrap();
col += 1;
}
line += 1;
}
self.excel.push_worksheet(worksheet);
self
}
pub fn save(&mut self) {
self.excel.save(self.filepath.clone()).unwrap();
}
}