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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use std::collections::HashMap;
use json::{array, JsonValue, object};
use log::info;
use umya_spreadsheet::{Alignment, EnumTrait, RowReference};
use crate::Head;


pub struct Read {}

impl Read {
    /// 导出指定头内容的对象组
    pub fn export(filename: &str, page: usize, header_line: u32, headers: Vec<Head>) -> JsonValue {
        let path = std::path::Path::new(filename);
        let book = match umya_spreadsheet::reader::xlsx::read(path) {
            Ok(e) => e,
            Err(e) => {
                println!("加载xlsx错误: {}", e);
                return array![];
            }
        };
        let total_row = match book.get_sheet(&page) {
            Ok(e) => e.get_highest_row(),
            Err(e) => {
                println!("加载xlsx行数错误: {}", e);
                return array![];
            }
        };
        let total_col = book.get_sheet(&page).unwrap().get_highest_column();

        let mut head = HashMap::new();
        for row in header_line..=header_line {
            let row_info = book.get_sheet(&page).unwrap().get_row_dimension(&row).is_none();
            if row_info {
                continue;
            }
            for col in 1..=total_col {
                let value = book.get_sheet(&page).unwrap().get_value((col, row));
                for header in headers.iter() {
                    if header.title == value {
                        head.insert(col, header.clone());
                    }
                }
            }
        }
        let mut list = array![];
        for row in header_line + 1..=total_row {
            let row_info = book.get_sheet(&page).unwrap().get_row_dimension(&row).is_none();
            if row_info {
                continue;
            }
            let mut data = object! {};
            for col in 1..=total_col {
                let value = book.get_sheet(&page).unwrap().get_value((col, row));
                if !head.get(&col).is_none() {
                    let info = head.get(&col).unwrap().clone();
                    data[info.field] = JsonValue::from(value);
                }
            }
            list.push(data).unwrap();
        }
        list
    }


    /// 导出表结构
    pub fn export_array(filename: &str, page: usize) -> JsonValue {
        let path = std::path::Path::new(filename);
        let book = match umya_spreadsheet::reader::xlsx::read(path) {
            Ok(e) => e,
            Err(e) => {
                println!("加载xlsx错误: {}", e);
                return array![];
            }
        };
        let total_row = book.get_sheet(&page).unwrap().get_highest_row();
        let total_col = book.get_sheet(&page).unwrap().get_highest_column();
        info!("{} {}",total_row,total_col);

        let mut data = array![];

        for row in 1..=total_row {
            if book.get_sheet(&page).unwrap().get_row_dimension(&row).is_none() {
                continue;
            }
            // 获取行信息
            let row_info = book.get_sheet(&page).unwrap().get_row_dimension(&row).unwrap();

            // 获取行高
            let row_height = row_info.clone().get_height().clone();

            let mut row_data = array![];
            for col in 1..=total_col {
                let value = book.get_sheet(&page).unwrap().get_value((col, row));

                let mut col_width = 20.0;

                let tt = book.get_sheet(&page).unwrap().get_column_dimension_by_number(&col).is_none();
                if !tt {
                    let col_info = book.get_sheet(&page).unwrap().get_column_dimension_by_number(&col).unwrap();
                    col_width = col_info.clone().get_width().clone();
                }


                let background_color = "";

                let style = book.get_sheet(&page).unwrap().get_style((col, row)).clone();

                // // font
                // let font_color = style.get_font().clone().unwrap().get_color().clone().clone().get_argb().to_string();
                // let font_color = style.clone();
                // println!("{:#?}", font_color);


                let mut style_size = 0;
                if !style.get_font().is_none() {
                    style_size = style.get_font().clone().unwrap().get_size().clone() as i32;
                }


                let alignment = style.get_alignment().clone().unwrap_or(Alignment::default());
                let alignment = alignment.get_horizontal().clone();
                let alignment = alignment.get_value_string().clone();
                let horizontal = if alignment == "general" { "center" } else { alignment };


                let alignment = style.get_alignment().clone().unwrap_or(Alignment::default());
                let alignment = alignment.get_vertical().clone();
                let vertical = alignment.get_value_string();

                let alignment = style.get_alignment().clone().unwrap_or(Alignment::default());
                let alignment = alignment.get_wrap_text().clone();
                let wrap_text = if alignment { "normal" } else { "none" };

                // let get_fill = book.get_sheet(&page).unwrap().get_style((col, row)).get_fill().clone();
                // let get_fill = get_fill.unwrap().get_pattern_fill().clone();
                // let get_fill = get_fill.unwrap().get_background_color().clone();
                // let get_fill = get_fill.unwrap_or(Color::default());
                // get_fill = get_fill.get_indexed().clone().to_string().as_str().clone();

                let mut style_left = false;
                let mut style_right = false;
                let mut style_top = false;
                let mut style_bottom = false;

                let is_borders = style.get_borders().is_none();
                if !is_borders {
                    let borders = style.get_borders().clone().unwrap();
                    style_left = if borders.get_left().get_color().get_indexed().clone() == 8 as u32 { true } else { false };
                    if !style_left {
                        style_left = if borders.get_left().get_color().get_indexed().clone() == 64 as u32 { true } else { false };
                    }
                    style_right = if borders.get_right().get_color().get_indexed().clone() == 8 as u32 { true } else { false };
                    if !style_right {
                        style_right = if borders.get_right().get_color().get_indexed().clone() == 64 as u32 { true } else { false };
                    }
                    style_top = if borders.get_top().get_color().get_indexed().clone() == 8 as u32 { true } else { false };
                    if !style_top {
                        style_top = if borders.get_top().get_color().get_indexed().clone() == 64 as u32 { true } else { false };
                    }
                    style_bottom = if borders.get_bottom().get_color().get_indexed().clone() == 8 as u32 { true } else { false };
                    if !style_bottom {
                        style_bottom = if borders.get_bottom().get_color().get_indexed().clone() == 64 as u32 { true } else { false };
                    }
                }


                let col_data = object! {
                    value:value,
                    row:row,
                    col:col,
                    style:object!{
                        height:format!("{}mm",row_height*0.3612),
                        width:format!("{}mm",col_width*2.54),
                        "font-size":style_size.clone(),
                        "background-color":background_color.clone(),
                        "text-align":horizontal.clone(),
                        "vertical-align":vertical.clone(),
                        "text-wrap":wrap_text.clone()
                    },
                    rowspan:1,
                    colspan:1,
                    border:object! {
                        left:style_left.clone(),
                        top:style_top.clone(),
                        bottom:style_bottom.clone(),
                        right:style_right.clone()
                    },
                    state:1
                };
                row_data.push(col_data).unwrap();
            }
            data.push(row_data).unwrap();
        }


        // 获取合并信息
        let merge = book.get_sheet(&page).unwrap().get_merge_cells().clone().clone();
        // println!("{:?}",book.get_sheet(&page).unwrap());
        for item in merge.iter() {
            let start_row =  match item.get_coordinate_start_row().clone() {
                None => {
                    continue;
                }
                Some(e) => {e.get_num().clone()}
            };
            let start_col = match item.get_coordinate_start_col().clone() {
                None => {
                    continue;
                }
                Some(e) => {e.get_num().clone()}
            };
            let end_row = match item.get_coordinate_end_row().clone() {
                None => {
                    continue;
                }
                Some(e) => {e.get_num().clone()}
            };
            let end_col = match item.get_coordinate_end_col().clone() {
                None => {
                    continue;
                }
                Some(e) => {e.get_num().clone()}
            };
            for row in start_row..=end_row {
                for col in start_col..=end_col {
                    data[row as usize - 1][col as usize - 1]["state"] = 0.into();
                }
            }
            data[start_row as usize - 1][start_col as usize - 1]["rowspan"] = JsonValue::from(end_row - start_row + 1);
            data[start_row as usize - 1][start_col as usize - 1]["colspan"] = JsonValue::from(if end_col == start_col { 1 } else { end_col - start_col + 1 });
            data[start_row as usize - 1][start_col as usize - 1]["state"] = 1.into();
        }

        return data;
    }
}