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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use std::any::Any;
use std::borrow::Borrow;
use std::env;
use std::path::PathBuf;
use json::{array, JsonValue, object};
use office::{DataType, Excel};
use office::DataType::{Bool, Empty, Error, Float, String};
use crate::Head;
pub struct Read {}
impl Read {
pub fn export(filename: &str, page: usize, heads: Vec<Head>) -> JsonValue {
let mut excel = Excel::open(&filename).expect("cannot open excel file");
let data = excel.sheet_names().unwrap();
let sheet_name = data[page].as_str();
let range = excel.worksheet_range(sheet_name).unwrap();
let mut r = range.rows();
let mut row_len = 0;
let mut col_len = 0;
let mut headlist = object! {};
let mut list = array![];
let mut list_index = 0;
r.for_each(|row| {
if col_len == 0 {
col_len = row.len();
let mut index = 0;
for item in row.iter() {
let mut title = "";
match item {
Float(e) => {}
Empty => {
println!("{}", 1111);
}
String(e) => {
title = e;
}
Error(e) => {
println!("{:?}", e.to_owned());
}
Bool(e) => {
println!("{}", e);
}
_ => {}
}
for head in heads.clone() {
if head.title == title {
headlist[index] = head.field.into();
index += 1;
}
}
}
} else {
let mut index = 0;
for item in row.iter() {
let field = headlist[index].as_str().unwrap();
if field.is_empty() {
index += 1;
continue;
}
match item {
Float(e) => {
list[list_index][field] = JsonValue::from(e.to_string());
}
Empty => {
list[list_index][field] = JsonValue::from("");
}
String(e) => {
list[list_index][field] = JsonValue::from(e.to_string());
}
Error(e) => {
list[list_index][field] = JsonValue::from("");
}
Bool(e) => {
list[list_index][field] = JsonValue::from(e.to_string());
}
_ => {}
}
index += 1;
}
list_index += 1;
}
row_len += 1;
});
list
}
}