forked from szgit/Helper.Core.Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExcelTemplateHelper.cs
270 lines (244 loc) · 12.6 KB
/
ExcelTemplateHelper.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
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
/*
* 作用:利用 NPOI 根据模板生成 Excel 文档。
* 以 $ 前缀:通过匿名类型替换数据
* 以 # 前缀:通过实体属性值替换数据
* 以 &= 前缀:公式计算
* 以 i 后缀:行数,会根据行索引进行替换
* 以 _dataBegin 后缀:数据行开始,计算过程中会替换
* 以 _dataEnd 后缀:数据行结束,计算过程中会替换
* 联系:QQ 100101392
* 来源:https://github.com/snipen/Helper.Core.Library
* */
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Helper.Core.Library
{
public class ExcelTemplateHelper
{
#region 对外公开方法
/// <summary>
/// 按模板生成 Excel 文档
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="templatePath">模板路径</param>
/// <param name="templateSheetName">模板表单名称</param>
/// <param name="excelPath">Excel 路径</param>
/// <param name="dataMatchList">数据匹配,Dictionary<string, object> 或 new {}</param>
/// <param name="dataList">数据列表</param>
/// <param name="mergeCellDataList">合并单元格数据列表</param>
/// <param name="reflectionType">反射类型</param>
public static void ToExcel<T>(string templatePath, string templateSheetName, string excelPath, object dataMatchList, List<T> dataList, List<ExcelMergeCell> mergeCellDataList = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class
{
Dictionary<string, object> propertyDict = CommonHelper.GetParameterDict(dataMatchList);
FileStream fileStream = null;
IWorkbook workbook = null;
try
{
using (fileStream = new FileStream(templatePath, FileMode.Open, FileAccess.ReadWrite))
{
workbook = ExcelHelper.ExecuteIWorkBookGet(fileStream);
if (workbook == null) throw new Exception(ExcelHelper.ExcelWorkbookNullException);
}
Dictionary<int, string> propertyMapperDict = new Dictionary<int, string>();
List<ExcelTemplateFormulaItem> formulaItemList = new List<ExcelTemplateFormulaItem>();
int lastRowIndex = 0;
int insertRowIndex = -1;
ISheet sheet = workbook.GetSheet(templateSheetName);
if (sheet != null)
{
lastRowIndex = sheet.LastRowNum;
for (int rowIndex = 0; rowIndex <= lastRowIndex; rowIndex++)
{
IRow iRow = sheet.GetRow(rowIndex);
if (iRow != null)
{
for (int colIndex = 0; colIndex <= iRow.LastCellNum; colIndex++)
{
ICell iCell = iRow.GetCell(colIndex);
if (iCell != null && !string.IsNullOrEmpty(iCell.ToString()))
{
string cellText = iCell.ToString();
if (cellText.StartsWith("$"))
{
cellText = cellText.TrimStart(new char[] { '$' });
if (propertyDict != null && propertyDict.ContainsKey(cellText))
{
iCell.SetCellValue(propertyDict[cellText].ToString());
}
}
else if (cellText.StartsWith("#"))
{
if (insertRowIndex == -1) insertRowIndex = rowIndex;
cellText = cellText.TrimStart(new char[] { '#' });
propertyMapperDict.Add(colIndex, cellText);
}
else if (cellText.StartsWith("&="))
{
if (rowIndex != insertRowIndex)
{
formulaItemList.Add(new ExcelTemplateFormulaItem() { Cell = iCell, FormulaText = cellText });
}
}
}
}
}
}
}
if (propertyMapperDict != null && propertyMapperDict.Count > 0 && insertRowIndex != -1)
{
if (dataList != null && dataList.Count > 0)
{
dynamic propertyGetDict = null;
if (reflectionType != ReflectionTypeEnum.Original) propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict<T>(reflectionType);
CopyRow(sheet, insertRowIndex, dataList, propertyMapperDict, propertyGetDict);
if (formulaItemList != null && formulaItemList.Count > 0)
{
foreach (ExcelTemplateFormulaItem formulaItem in formulaItemList)
{
formulaItem.FormulaText = formulaItem.FormulaText.TrimStart(new char[] { '&', '=' });
formulaItem.FormulaText = formulaItem.FormulaText.Replace("_dataBegin", (insertRowIndex + 1).ToString());
formulaItem.FormulaText = formulaItem.FormulaText.Replace("_dataEnd", (insertRowIndex + dataList.Count).ToString());
formulaItem.Cell.SetCellFormula(formulaItem.FormulaText);
formulaItem.Cell.SetCellType(CellType.Formula);
}
}
}
}
sheet.ForceFormulaRecalculation = true;
if (mergeCellDataList != null && mergeCellDataList.Count > 0)
{
foreach (ExcelMergeCell cellItem in mergeCellDataList)
{
sheet.AddMergedRegion(new CellRangeAddress(insertRowIndex + cellItem.BeginRow, insertRowIndex + cellItem.EndRow, cellItem.BeginColumn, cellItem.EndColumn));
}
}
if (System.IO.File.Exists(excelPath)) System.IO.File.Delete(excelPath);
using (fileStream = new FileStream(excelPath, FileMode.Create, FileAccess.ReadWrite))
{
workbook.Write(fileStream);
}
workbook.Close();
}
catch
{
throw;
}
finally
{
if (fileStream != null) fileStream.Close();
if (workbook != null) workbook.Close();
}
}
#endregion
#region 逻辑处理私有函数
private static void CopyRow<T>(ISheet sheet, int templateRowIndex, List<T> dataList, Dictionary<int, string> propertyMapperDict, dynamic propertyGetDict) where T : class
{
if (dataList == null || dataList.Count == 0) return;
IRow templateDataRow = sheet.GetRow(templateRowIndex);
if (templateDataRow == null) return;
List<ExcelMergeCell> mergeCellList = new List<ExcelMergeCell>();
for (int i = 0; i < sheet.NumMergedRegions; i++)
{
CellRangeAddress cellRangeAddress = sheet.GetMergedRegion(i);
if (cellRangeAddress.FirstRow == templateDataRow.RowNum)
{
mergeCellList.Add(new ExcelMergeCell() { BeginColumn = cellRangeAddress.FirstColumn, EndColumn = cellRangeAddress.LastColumn });
}
}
int templateDataCellCount = templateDataRow.Cells.Count;
if (dataList.Count > 1)
{
sheet.ShiftRows(templateRowIndex, //开始行
sheet.LastRowNum, //结束行
dataList.Count - 1, //插入行总数
true, //是否复制行高
false //是否重置行高
);
}
IRow dataRow = null;
ICell templateCell = null;
ICell dataCell = null;
int dataCount = dataList.Count;
int dataLastIndex = templateRowIndex + dataCount;
for (int i = templateRowIndex, j = 0; i < dataLastIndex && j < dataCount; i++, j++)
{
dataRow = sheet.CreateRow(i);
dataRow.Height = templateDataRow.Height;//复制行高
for (int colIndex = templateDataRow.FirstCellNum; colIndex < templateDataRow.LastCellNum; colIndex++)
{
templateCell = templateDataRow.GetCell(colIndex);
if (templateCell == null) continue;
dataCell = dataRow.CreateCell(colIndex);
if (dataCell == null) dataCell = templateDataRow.CreateCell(colIndex);
dataCell.CellStyle = templateCell.CellStyle;//赋值单元格格式
string templateCellText = templateCell.ToString();
if (!templateCellText.StartsWith("&="))
{
dataCell.SetCellType(templateCell.CellType);
if (propertyMapperDict.ContainsKey(colIndex))
{
object propertyValue = null;
if (propertyGetDict != null && propertyGetDict.ContainsKey(propertyMapperDict[colIndex]))
{
propertyValue = propertyGetDict[propertyMapperDict[colIndex]](dataList[j]);
}
else
{
propertyValue = ReflectionHelper.GetPropertyValue(dataList[j], propertyMapperDict[colIndex]);
}
if (propertyValue != null)
{
Type propertyType = typeof(T).GetProperty(propertyMapperDict[colIndex]).PropertyType;
if (propertyType == typeof(int))
{
dataCell.SetCellValue(int.Parse(propertyValue.ToString()));
}
else if (propertyType == typeof(float) || propertyType == typeof(double))
{
dataCell.SetCellValue(double.Parse(propertyValue.ToString()));
}
else if (propertyType == typeof(DateTime))
{
dataCell.SetCellValue(DateTime.Parse(propertyValue.ToString()));
}
else
{
dataCell.SetCellValue(propertyValue.ToString());
}
}
}
}
else
{
templateCellText = templateCellText.TrimStart(new char[] { '&', '=' });
templateCellText = templateCellText.Replace("i", (dataRow.RowNum + 1).ToString());
dataCell.SetCellType(CellType.Formula);
dataCell.SetCellFormula(templateCellText);
}
}
if (i != dataLastIndex - 1 && mergeCellList != null && mergeCellList.Count > 0)
{
foreach (ExcelMergeCell excelMergeCell in mergeCellList)
{
CellRangeAddress cellRangeAddress = new CellRangeAddress(dataRow.RowNum, dataRow.RowNum, excelMergeCell.BeginColumn, excelMergeCell.EndColumn);
sheet.AddMergedRegion(cellRangeAddress);
}
}
}
}
#endregion
}
#region 逻辑处理辅助类
internal class ExcelTemplateFormulaItem
{
public ICell Cell { get; set; }
public string FormulaText { get; set; }
}
#endregion
}