-
Notifications
You must be signed in to change notification settings - Fork 1
/
Workbook.cs
222 lines (206 loc) · 7.51 KB
/
Workbook.cs
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
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using XiaoFeng.Xml;
/****************************************************************
* Copyright © (2022) www.fayelf.com All Rights Reserved. *
* Author : jacky *
* QQ : 7092734 *
* Email : [email protected] *
* Site : www.fayelf.com *
* Create Time : 2022-10-04 11:48:00 *
* Version : v 1.0.0 *
* CLR Version : 4.0.30319.42000 *
*****************************************************************/
namespace XiaoFeng.Excel
{
/// <summary>
/// 数据工作本
/// </summary>
public class Workbook
{
#region 构造器
/// <summary>
/// 无参构造器
/// </summary>
public Workbook() { }
/// <summary>
/// 打开文件
/// </summary>
/// <param name="option">配置</param>
public Workbook(WorkbookOption option)
{
this.Option = option;
}
#endregion
#region 属性
/// <summary>
/// 工作本配置
/// </summary>
public WorkbookOption Option { get; set; } = new WorkbookOption();
/// <summary>
/// 文件路径
/// </summary>
public string FilePath { get; set; }
/// <summary>
/// 基础年
/// </summary>
public int BaseYear { get; set; } = 1900;
/// <summary>
/// 数据表集
/// </summary>
private List<Sheet> CurrentSheets { get; set; } = new List<Sheet>();
/// <summary>
/// 数据表集
/// </summary>
public IEnumerable<Sheet> Sheets => this.CurrentSheets.Where(a => this.Option.IncludeHiddenSheet || a.Hidden == false).OrderBy(a => a.Id);
/// <summary>
/// 值
/// </summary>
public Dictionary<int, string> SharedStrings = new Dictionary<int, string>();
/// <summary>
/// 数据值类型
/// </summary>
public Dictionary<int, NumberFormat> NumberFormats = new Dictionary<int, NumberFormat>();
#endregion
#region 方法
#region 打开文件
/// <summary>
/// 打开文件
/// </summary>
/// <param name="path">文件路径</param>
/// <param name="option">配置</param>
public void Open(string path, WorkbookOption option = null)
{
if (option != null)
this.Option = option;
this.FilePath = path;
this.Open();
}
/// <summary>
/// 打开
/// </summary>
private void Open()
{
using (ZipArchive archive = ZipFile.OpenRead(this.FilePath))
{
var sheetsEntry = archive.Entries.FirstOrDefault(e => e.FullName == Common.XL_WORKBOOK_XML);
LoadWorkbookXml(sheetsEntry);
var sheetPathsEntry = archive.Entries.FirstOrDefault(e => e.FullName == Common.XL_RELS_WORKBOOK_XML_RELS);
LoadWorkbookXmlRels(sheetPathsEntry);
var sharedStringsEntry = archive.Entries.FirstOrDefault(e => e.FullName == Common.XL_SHAREDSTRINGS_XML);
LoadSharedStrings(sharedStringsEntry);
var stylesEntry = archive.Entries.FirstOrDefault(e => e.FullName == Common.XL_STYLES_XML);
LoadStyles(stylesEntry);
if (Option.LoadSheets)
{
this.CurrentSheets.Each(sheet => sheet.Open());
}
}
}
#endregion
#region 加载WorkbookXml数据
/// <summary>
/// 加载WorkbookXml数据
/// </summary>
/// <param name="entry">压缩包实体</param>
private void LoadWorkbookXml(ZipArchiveEntry entry)
{
if (entry == null) return;
var doc = XDocument.Load(entry.Open());
var root = doc.Root;
var pr = root.GetXElement("workbookPr");
var date1904 = pr.Attribute("date1904");
if (date1904 != null && date1904.Value == "1")
this.BaseYear = 1904;
root.GetXElement("sheets").Elements().Each(e =>
{
var state = e.GetXAttribute("state");
this.CurrentSheets.Add(new Sheet(e.GetXAttribute("name").Value, e.GetXAttribute("r:id").Value, state.IsNotNullOrEmpty() && state.Value == "hidden")
{
Workbook = this
});
});
}
#endregion
#region 加载workbook资源
/// <summary>
/// 加载workbook资源
/// </summary>
/// <param name="entry">实体</param>
private void LoadWorkbookXmlRels(ZipArchiveEntry entry)
{
if (entry == null) return;
var doc = XDocument.Load(entry.Open());
var root = doc.Root;
root.GetXElements("Relationship").Each(e =>
{
var target = e.GetXAttribute("Target");
if (!target.Value.StartsWith("worksheets")) return;
var id = e.GetXAttribute("Id");
var sheet = this.CurrentSheets.Find(a => a.Id == id.Value);
if (sheet != null)
{
sheet.Path = $"xl/{target.Value}";
}
});
}
#endregion
#region 加载SharedStrings资源
/// <summary>
/// 加载SharedStrings资源
/// </summary>
/// <param name="entry">实体</param>
private void LoadSharedStrings(ZipArchiveEntry entry)
{
if (entry == null) return;
var doc = XDocument.Load(entry.Open());
var root = doc.Root;
int index = 0;
root.GetXElements("si").Each(e =>
{
var vals = e.GetXDescendants("t");
if (vals == null || !vals.Any()) return;
var sbr = new StringBuilder();
vals.Each(v => sbr.Append(v.Value));
this.SharedStrings.Add(index++, sbr.ToString());
});
}
#endregion
#region 加载样式
/// <summary>
/// 加载样式
/// </summary>
/// <param name="entry">实体</param>
private void LoadStyles(ZipArchiveEntry entry)
{
if (entry == null) return;
var doc = XDocument.Load(entry.Open());
var cellXfs = doc.Root.GetXElement("cellXfs");
int index = 0;
doc.Root.GetXElement("cellXfs").Elements().Each(e =>
{
var fmtId = e.GetXAttribute("numFmtId");
if (fmtId.IsNotNullOrEmpty())
{
var fmt = fmtId.Value.ToCast<int>().ToEnum<NumberFormat>();
this.NumberFormats.Add(index++, fmt);
}
});
}
#endregion
#region 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="sheetName">表名</param>
/// <returns></returns>
public Sheet GetSheet(string sheetName) => this.CurrentSheets.SingleOrDefault(a => a.Name.EqualsIgnoreCase(sheetName));
#endregion
#endregion
}
}