office-rs 0.1.1

A Rust library for reading and writing XML Office files
Documentation
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! Excel (XLSX) 文件写入器
//!
//! 提供创建和写入 XLSX 文件的功能,包括生成工作簿结构、工作表数据和单元格内容。

use crate::common::xml_utils::{ XmlElement, XmlGenerator };
use crate::common::zip_utils::ZipWriter;
use crate::context::ErrorContext;
use crate::error::Result;
use crate::xlsx::{ Cell, CellValue, Workbook, Worksheet };
use std::collections::HashMap;
use std::fs::File;
use std::path::Path;

/// XLSX 文件写入器
pub struct XlsxWriter {
    zip_writer: ZipWriter<File>,
    shared_strings: Vec<String>,
    shared_strings_map: HashMap<String, usize>,
}

impl XlsxWriter {
    /// 创建新的 XLSX 文件
    pub fn create<P: AsRef<Path>>(path: P) -> Result<Self> {
        let zip_writer = ZipWriter::create_file(&path).map_err(|e| {
            e.with_context(ErrorContext {
                operation: Some("创建XLSX文件".to_string()),
                file_path: Some(path.as_ref().to_string_lossy().to_string()),
                ..Default::default()
            })
        })?;

        Ok(Self {
            zip_writer,
            shared_strings: Vec::new(),
            shared_strings_map: HashMap::new(),
        })
    }

    /// 写入工作簿
    pub fn write_workbook(&mut self, workbook: &Workbook) -> Result<()> {
        // 收集所有共享字符串
        self.collect_shared_strings(workbook)?;

        // 写入基础文件结构
        self.write_content_types()?;
        self.write_app_properties()?;
        self.write_core_properties()?;
        self.write_relationships()?;
        self.write_workbook_relationships(workbook)?;

        // 写入共享字符串
        if !self.shared_strings.is_empty() {
            self.write_shared_strings()?;
        }

        // 写入工作簿XML
        self.write_workbook_xml(workbook)?;

        // 写入工作表
        for (index, worksheet) in workbook.worksheets().enumerate() {
            self.write_worksheet(worksheet, index + 1)?;
        }

        // 完成写入
        // 完成写入操作,这里需要消费zip_writer
        // 由于finish()需要获取所有权,我们需要重新设计这个方法

        Ok(())
    }

    /// 收集所有共享字符串
    fn collect_shared_strings(&mut self, workbook: &Workbook) -> Result<()> {
        for worksheet in workbook.worksheets() {
            if let Some(dim) = worksheet.dimension() {
                for row in 0..=dim.max_row {
                    for col in 0..=dim.max_column {
                        if let Some(cell) = worksheet.get_cell(row, col) {
                            if let CellValue::Text(text) = &cell.value {
                                if !self.shared_strings_map.contains_key(text) {
                                    let index = self.shared_strings.len();
                                    self.shared_strings.push(text.clone());
                                    self.shared_strings_map.insert(text.clone(), index);
                                }
                            }
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// 写入 [Content_Types].xml
    fn write_content_types(&mut self) -> Result<()> {
        let mut root = XmlElement::new("Types");
        root.add_attribute("xmlns", "http://schemas.openxmlformats.org/package/2006/content-types");

        // 默认扩展名
        let mut default_rels = XmlElement::new("Default");
        default_rels.add_attribute("Extension", "rels");
        default_rels.add_attribute(
            "ContentType",
            "application/vnd.openxmlformats-package.relationships+xml"
        );
        root.add_child(default_rels);

        let mut default_xml = XmlElement::new("Default");
        default_xml.add_attribute("Extension", "xml");
        default_xml.add_attribute("ContentType", "application/xml");
        root.add_child(default_xml);

        // 覆盖类型
        let mut override_workbook = XmlElement::new("Override");
        override_workbook.add_attribute("PartName", "/xl/workbook.xml");
        override_workbook.add_attribute(
            "ContentType",
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"
        );
        root.add_child(override_workbook);

        if !self.shared_strings.is_empty() {
            let mut override_shared_strings = XmlElement::new("Override");
            override_shared_strings.add_attribute("PartName", "/xl/sharedStrings.xml");
            override_shared_strings.add_attribute(
                "ContentType",
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"
            );
            root.add_child(override_shared_strings);
        }

        let mut override_app = XmlElement::new("Override");
        override_app.add_attribute("PartName", "/docProps/app.xml");
        override_app.add_attribute(
            "ContentType",
            "application/vnd.openxmlformats-officedocument.extended-properties+xml"
        );
        root.add_child(override_app);

        let mut override_core = XmlElement::new("Override");
        override_core.add_attribute("PartName", "/docProps/core.xml");
        override_core.add_attribute(
            "ContentType",
            "application/vnd.openxmlformats-package.core-properties+xml"
        );
        root.add_child(override_core);

        let generator = XmlGenerator::new();
        let xml_content = generator.generate_string(&root)?;
        self.zip_writer.add_file_from_string("[Content_Types].xml", &xml_content)?;

        Ok(())
    }

    /// 写入应用程序属性
    fn write_app_properties(&mut self) -> Result<()> {
        let mut root = XmlElement::new("Properties");
        root.add_attribute(
            "xmlns",
            "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"
        );
        root.add_attribute(
            "xmlns:vt",
            "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"
        );

        let mut application = XmlElement::new("Application");
        application.set_text_content("office-rs");
        root.add_child(application);

        let mut doc_security = XmlElement::new("DocSecurity");
        doc_security.set_text_content("0");
        root.add_child(doc_security);

        let mut scale_crop = XmlElement::new("ScaleCrop");
        scale_crop.set_text_content("false");
        root.add_child(scale_crop);

        let generator = XmlGenerator::new();
        let xml_content = generator.generate_string(&root)?;
        self.zip_writer.add_file_from_string("docProps/app.xml", &xml_content)?;

        Ok(())
    }

    /// 写入核心属性
    fn write_core_properties(&mut self) -> Result<()> {
        let mut root = XmlElement::new("cp:coreProperties");
        root.add_attribute(
            "xmlns:cp",
            "http://schemas.openxmlformats.org/package/2006/metadata/core-properties"
        );
        root.add_attribute("xmlns:dc", "http://purl.org/dc/elements/1.1/");
        root.add_attribute("xmlns:dcterms", "http://purl.org/dc/terms/");
        root.add_attribute("xmlns:dcmitype", "http://purl.org/dc/dcmitype/");
        root.add_attribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

        let mut creator = XmlElement::new("dc:creator");
        creator.set_text_content("office-rs");
        root.add_child(creator);

        let mut created = XmlElement::new("dcterms:created");
        created.add_attribute("xsi:type", "dcterms:W3CDTF");
        created.set_text_content(chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string());
        root.add_child(created);

        let generator = XmlGenerator::new();
        let xml_content = generator.generate_string(&root)?;
        self.zip_writer.add_file_from_string("docProps/core.xml", &xml_content)?;

        Ok(())
    }

    /// 写入根关系文件
    fn write_relationships(&mut self) -> Result<()> {
        let mut root = XmlElement::new("Relationships");
        root.add_attribute("xmlns", "http://schemas.openxmlformats.org/package/2006/relationships");

        let mut rel_workbook = XmlElement::new("Relationship");
        rel_workbook.add_attribute("Id", "rId1");
        rel_workbook.add_attribute(
            "Type",
            "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
        );
        rel_workbook.add_attribute("Target", "xl/workbook.xml");
        root.add_child(rel_workbook);

        let mut rel_core = XmlElement::new("Relationship");
        rel_core.add_attribute("Id", "rId2");
        rel_core.add_attribute(
            "Type",
            "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"
        );
        rel_core.add_attribute("Target", "docProps/core.xml");
        root.add_child(rel_core);

        let mut rel_app = XmlElement::new("Relationship");
        rel_app.add_attribute("Id", "rId3");
        rel_app.add_attribute(
            "Type",
            "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"
        );
        rel_app.add_attribute("Target", "docProps/app.xml");
        root.add_child(rel_app);

        let generator = XmlGenerator::new();
        let xml_content = generator.generate_string(&root)?;
        self.zip_writer.add_file_from_string("_rels/.rels", &xml_content)?;

        Ok(())
    }

    /// 写入工作簿关系文件
    fn write_workbook_relationships(&mut self, workbook: &Workbook) -> Result<()> {
        let mut root = XmlElement::new("Relationships");
        root.add_attribute("xmlns", "http://schemas.openxmlformats.org/package/2006/relationships");

        let mut rel_id = 1;

        // 工作表关系
        for (index, _) in workbook.worksheets().enumerate() {
            let mut rel_worksheet = XmlElement::new("Relationship");
            rel_worksheet.add_attribute("Id", format!("rId{}", rel_id));
            rel_worksheet.add_attribute(
                "Type",
                "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"
            );
            rel_worksheet.add_attribute("Target", format!("worksheets/sheet{}.xml", index + 1));
            root.add_child(rel_worksheet);
            rel_id += 1;
        }

        // 共享字符串关系
        if !self.shared_strings.is_empty() {
            let mut rel_shared_strings = XmlElement::new("Relationship");
            rel_shared_strings.add_attribute("Id", format!("rId{}", rel_id));
            rel_shared_strings.add_attribute(
                "Type",
                "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"
            );
            rel_shared_strings.add_attribute("Target", "sharedStrings.xml");
            root.add_child(rel_shared_strings);
        }

        let generator = XmlGenerator::new();
        let xml_content = generator.generate_string(&root)?;
        self.zip_writer.add_file_from_string("xl/_rels/workbook.xml.rels", &xml_content)?;

        Ok(())
    }

    /// 写入共享字符串
    fn write_shared_strings(&mut self) -> Result<()> {
        let mut root = XmlElement::new("sst");
        root.add_attribute("xmlns", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
        root.add_attribute("count", self.shared_strings.len().to_string());
        root.add_attribute("uniqueCount", self.shared_strings.len().to_string());

        for string in &self.shared_strings {
            let mut si = XmlElement::new("si");
            let mut t = XmlElement::new("t");
            t.set_text_content(string.clone());
            si.add_child(t);
            root.add_child(si);
        }

        let generator = XmlGenerator::new();
        let xml_content = generator.generate_string(&root)?;
        self.zip_writer.add_file_from_string("xl/sharedStrings.xml", &xml_content)?;

        Ok(())
    }

    /// 写入工作簿XML
    fn write_workbook_xml(&mut self, workbook: &Workbook) -> Result<()> {
        let mut root = XmlElement::new("workbook");
        root.add_attribute("xmlns", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
        root.add_attribute(
            "xmlns:r",
            "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
        );

        // 文件版本
        let mut file_version = XmlElement::new("fileVersion");
        file_version.add_attribute("appName", "xl");
        file_version.add_attribute("lastEdited", "7");
        file_version.add_attribute("lowestEdited", "7");
        file_version.add_attribute("rupBuild", "24816");
        root.add_child(file_version);

        // 工作簿属性
        let mut workbook_pr = XmlElement::new("workbookPr");
        workbook_pr.add_attribute("defaultThemeVersion", "166925");
        root.add_child(workbook_pr);

        // 工作表列表
        let mut sheets = XmlElement::new("sheets");
        for (index, worksheet) in workbook.worksheets().enumerate() {
            let mut sheet = XmlElement::new("sheet");
            sheet.add_attribute("name", &worksheet.properties.name);
            sheet.add_attribute("sheetId", &(index + 1).to_string());
            sheet.add_attribute("r:id", &format!("rId{}", index + 1));
            sheets.add_child(sheet);
        }
        root.add_child(sheets);

        let generator = XmlGenerator::new();
        let xml_content = generator.generate_string(&root)?;
        self.zip_writer.add_file_from_string("xl/workbook.xml", &xml_content)?;

        Ok(())
    }

    /// 写入工作表
    fn write_worksheet(&mut self, worksheet: &Worksheet, sheet_id: usize) -> Result<()> {
        let mut root = XmlElement::new("worksheet");
        root.add_attribute("xmlns", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
        root.add_attribute(
            "xmlns:r",
            "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
        );

        // 工作表属性
        let mut sheet_pr = XmlElement::new("sheetPr");
        sheet_pr.add_attribute("tabColor", "auto");
        root.add_child(sheet_pr);

        // 维度
        if let Some(dim) = worksheet.dimension() {
            if dim.max_row > 0 && dim.max_column > 0 {
                let mut dimension = XmlElement::new("dimension");
                let range = format!(
                    "{}:{}",
                    self.cell_reference(1, 1),
                    self.cell_reference(dim.max_row + 1, dim.max_column + 1)
                );
                dimension.add_attribute("ref", range);
                root.add_child(dimension);
            }
        }

        // 工作表视图
        let mut sheet_views = XmlElement::new("sheetViews");
        let mut sheet_view = XmlElement::new("sheetView");
        sheet_view.add_attribute("tabSelected", "1");
        sheet_view.add_attribute("workbookViewId", "0");
        sheet_views.add_child(sheet_view);
        root.add_child(sheet_views);

        // 工作表格式
        let mut sheet_format_pr = XmlElement::new("sheetFormatPr");
        sheet_format_pr.add_attribute("defaultRowHeight", "15");
        root.add_child(sheet_format_pr);

        // 工作表数据
        let mut sheet_data = XmlElement::new("sheetData");

        if let Some(dim) = worksheet.dimension() {
            for row_idx in 0..=dim.max_row {
                let mut has_data = false;
                let mut row = XmlElement::new("row");
                let row_num = row_idx + 1; // Excel行号从1开始
                row.add_attribute("r", row_num.to_string());

                for col_idx in 0..=dim.max_column {
                    if let Some(cell) = worksheet.get_cell(row_idx, col_idx) {
                        if !matches!(cell.value, CellValue::Empty) {
                            let col_num = col_idx + 1; // Excel列号从1开始
                            let cell_element = self.create_cell_element(cell, row_num, col_num)?;
                            row.add_child(cell_element);
                            has_data = true;
                        }
                    }
                }

                if has_data {
                    sheet_data.add_child(row);
                }
            }
        }

        root.add_child(sheet_data);

        let generator = XmlGenerator::new();
        let xml_content = generator.generate_string(&root)?;
        let file_path = format!("xl/worksheets/sheet{}.xml", sheet_id);
        self.zip_writer.add_file_from_string(&file_path, &xml_content)?;

        Ok(())
    }

    /// 创建单元格XML元素
    fn create_cell_element(&self, cell: &Cell, row: u32, col: u32) -> Result<XmlElement> {
        let mut cell_element = XmlElement::new("c");
        cell_element.add_attribute("r", self.cell_reference(row, col));

        match &cell.value {
            CellValue::Text(text) => {
                cell_element.add_attribute("t", "s");
                let mut v = XmlElement::new("v");
                if let Some(&index) = self.shared_strings_map.get(text) {
                    v.set_text_content(index.to_string());
                }
                cell_element.add_child(v);
            }
            CellValue::Number(num) => {
                let mut v = XmlElement::new("v");
                v.set_text_content(num.to_string());
                cell_element.add_child(v);
            }
            CellValue::Boolean(b) => {
                cell_element.add_attribute("t", "b");
                let mut v = XmlElement::new("v");
                v.set_text_content((if *b { "1" } else { "0" }).to_string());
                cell_element.add_child(v);
            }
            CellValue::Formula(formula) => {
                let mut f = XmlElement::new("f");
                f.set_text_content(formula.clone());
                cell_element.add_child(f);
            }
            CellValue::Empty => {
                // 空单元格不需要值
            }
            CellValue::DateTime(dt) => {
                cell_element.add_attribute("t", "n");
                let mut v = XmlElement::new("v");
                v.set_text_content(dt.to_string());
                cell_element.add_child(v);
            }
            CellValue::Error(err) => {
                cell_element.add_attribute("t", "e");
                let mut v = XmlElement::new("v");
                v.set_text_content(err.clone());
                cell_element.add_child(v);
            }
        }

        Ok(cell_element)
    }

    /// 生成单元格引用 (如 (1, 1) -> "A1")
    fn cell_reference(&self, row: u32, col: u32) -> String {
        let mut col_str = String::new();
        let mut col_num = col;

        while col_num > 0 {
            col_num -= 1;
            col_str.insert(0, (b'A' + ((col_num % 26) as u8)) as char);
            col_num /= 26;
        }

        format!("{}{}", col_str, row)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::xlsx::CellValue;

    // 创建一个测试用的结构体,不包含zip_writer字段
    struct TestXlsxWriter {
        shared_strings: Vec<String>,
        shared_strings_map: HashMap<String, usize>,
    }

    impl TestXlsxWriter {
        fn new() -> Self {
            Self {
                shared_strings: Vec::new(),
                shared_strings_map: HashMap::new(),
            }
        }

        fn cell_reference(&self, row: u32, col: u32) -> String {
            let mut col_str = String::new();
            let mut col_num = col;

            while col_num > 0 {
                col_num -= 1;
                col_str.insert(0, (b'A' + ((col_num % 26) as u8)) as char);
                col_num /= 26;
            }

            format!("{}{}", col_str, row)
        }
    }

    #[test]
    fn test_cell_reference() {
        let writer = TestXlsxWriter::new();

        assert_eq!(writer.cell_reference(1, 1), "A1");
        assert_eq!(writer.cell_reference(2, 2), "B2");
        assert_eq!(writer.cell_reference(26, 26), "Z26");
        assert_eq!(writer.cell_reference(27, 27), "AA27");
    }

    #[test]
    fn test_shared_strings_collection() {
        // 创建临时文件用于测试
        let temp_file = std::env::temp_dir().join("test_xlsx_writer.xlsx");
        let mut writer = XlsxWriter::create(&temp_file).unwrap();

        let mut workbook = Workbook::new();

        // 添加一些文本单元格到默认工作表
        if let Some(ws) = workbook.get_worksheet_mut("Sheet1") {
            ws.set_cell_value(1, 1, CellValue::Text("Hello".to_string()));
            ws.set_cell_value(1, 2, CellValue::Text("World".to_string()));
            ws.set_cell_value(2, 1, CellValue::Text("Hello".to_string())); // 重复文本
        }

        writer.collect_shared_strings(&workbook).unwrap();

        assert_eq!(writer.shared_strings.len(), 2); // 只有两个唯一字符串
        assert!(writer.shared_strings.contains(&"Hello".to_string()));
        assert!(writer.shared_strings.contains(&"World".to_string()));

        // 清理临时文件
        let _ = std::fs::remove_file(&temp_file);
    }
}