-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicTableExportTests.cs
More file actions
100 lines (83 loc) · 3.88 KB
/
Copy pathDynamicTableExportTests.cs
File metadata and controls
100 lines (83 loc) · 3.88 KB
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
using ClosedXML.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModerBox.Common.Test {
[TestClass]
public class DynamicTableExportTests {
private DynamicTable<int> table;
[TestInitialize]
public void SetUp() {
// 初始化表格并插入测试数据
table = new DynamicTable<int>();
table.InsertData("Row1", "Col1", 1);
table.InsertData("Row1", "Col2", 2);
table.InsertData("Row2", "Col1", 3);
table.InsertData("Row2", "Col2", 4);
}
[TestMethod]
public void TestExportWithoutTranspose() {
// 导出不转置的表格
string filePath = "test_export_no_transpose.xlsx";
table.ExportToExcel(filePath, transpose: false);
// 验证导出的 Excel 文件
using (var workbook = new XLWorkbook(filePath)) {
var worksheet = workbook.Worksheet(1);
// 检查表头
Assert.AreEqual("Col1", worksheet.Cell(1, 2).Value.ToString());
Assert.AreEqual("Col2", worksheet.Cell(1, 3).Value.ToString());
// 检查数据
Assert.AreEqual("1", worksheet.Cell(2, 2).Value.ToString());
Assert.AreEqual("2", worksheet.Cell(2, 3).Value.ToString());
Assert.AreEqual("3", worksheet.Cell(3, 2).Value.ToString());
Assert.AreEqual("4", worksheet.Cell(3, 3).Value.ToString());
}
// 清理生成的文件
File.Delete(filePath);
}
[TestMethod]
public void TestExportWithTranspose() {
// 导出转置后的表格
string filePath = "test_export_transpose.xlsx";
table.ExportToExcel(filePath, transpose: true);
// 验证导出的 Excel 文件
using (var workbook = new XLWorkbook(filePath)) {
var worksheet = workbook.Worksheet(1);
// 检查表头
Assert.AreEqual("Row1", worksheet.Cell(1, 2).Value.ToString());
Assert.AreEqual("Row2", worksheet.Cell(1, 3).Value.ToString());
// 检查数据
Assert.AreEqual("1", worksheet.Cell(2, 2).Value.ToString());
Assert.AreEqual("3", worksheet.Cell(2, 3).Value.ToString());
Assert.AreEqual("2", worksheet.Cell(3, 2).Value.ToString());
Assert.AreEqual("4", worksheet.Cell(3, 3).Value.ToString());
}
// 清理生成的文件
File.Delete(filePath);
}
[TestMethod]
public void TestExportWithCustomOrder() {
// 自定义行和列的顺序
string filePath = "test_export_custom_order.xlsx";
var rowOrder = new List<string> { "Row2", "Row1" };
var colOrder = new List<string> { "Col2", "Col1" };
table.ExportToExcel(filePath, transpose: false, rowOrder: rowOrder, colOrder: colOrder);
// 验证导出的 Excel 文件
using (var workbook = new XLWorkbook(filePath)) {
var worksheet = workbook.Worksheet(1);
// 检查表头
Assert.AreEqual("Col2", worksheet.Cell(1, 2).Value.ToString());
Assert.AreEqual("Col1", worksheet.Cell(1, 3).Value.ToString());
// 检查数据 (顺序被更改了)
Assert.AreEqual("4", worksheet.Cell(2, 2).Value.ToString()); // Row2, Col2
Assert.AreEqual("3", worksheet.Cell(2, 3).Value.ToString()); // Row2, Col1
Assert.AreEqual("2", worksheet.Cell(3, 2).Value.ToString()); // Row1, Col2
Assert.AreEqual("1", worksheet.Cell(3, 3).Value.ToString()); // Row1, Col1
}
// 清理生成的文件
File.Delete(filePath);
}
}
}