-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcelAssit.cs
382 lines (311 loc) · 11.8 KB
/
ExcelAssit.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
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
using NPOI;
using NPOI.HSSF.UserModel;
using NPOI.OpenXml4Net.OPC;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ExcelAssit
{
public static class ExcelAssit
{
public static bool WriteExcel(string path, string sheetName,List<Object> datas)
{
if (datas==null || datas.Count <= 0)
{
return false;
}
HSSFWorkbook workbook = new HSSFWorkbook();
ISheet sheet1 = workbook.CreateSheet(sheetName);
ExcelAssit.CreateTitle(sheet1, datas[0]);
int rowIndex = 1;
datas.ForEach(a => {
ExcelAssit.WriteRow(a, workbook, sheet1, rowIndex);
rowIndex++;
});
return WriteToDisk(path,workbook);
}
private static void WriteRow(Object item, IWorkbook book,ISheet sheet,int rowIndex)
{
Type type = item.GetType();
IRow row = sheet.CreateRow(rowIndex);
int cellIndex = 0;
type.GetProperties().ToList().ForEach(a => {
AssitCellAttribute attr = ExcelAssit.GetCellAttribute(a);
if (attr == null)
{
return;
}
ICell cell= ExcelAssit.CreateCell(attr, row, cellIndex);
cellIndex++;
if (attr.CellType == CellType.Image)
{
if (a.GetValue(item) == null)
{
return;
}
byte[] bytes = null;
if (a.PropertyType == typeof(string))
{
bytes = System.IO.File.ReadAllBytes(a.GetValue(item).ToString());
}
if (a.PropertyType == typeof(byte[]))
{
bytes = (byte[])a.GetValue(item);
}
int pictureIdx = book.AddPicture(bytes, PictureType.JPEG);
HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(70, 10, 0, 0, cellIndex-1, rowIndex, cellIndex, rowIndex + 1);
HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
return;
}
if (attr.CellType == CellType.Int)
{
if (a.GetValue(item) == null)
{
return;
}
cell.SetCellValue(int.Parse(a.GetValue(item).ToString()));
return;
}
if (attr.CellType == CellType.Float)
{
if (a.GetValue(item) == null)
{
return;
}
cell.SetCellValue(float.Parse(a.GetValue(item).ToString()));
return;
}
if (attr.CellType == CellType.DateTime)
{
if (a.GetValue(item) == null)
{
return;
}
if(a.PropertyType==typeof(string))
{
cell.SetCellValue(int.Parse(a.GetValue(item).ToString()));
}
if (a.PropertyType == typeof(DateTime))
{
cell.SetCellValue(DateTime.Parse(a.GetValue(item).ToString()).ToString("yyyy-MM-dd hh:mm:ss"));
}
}
if (attr.CellType == CellType.String)
{
if (a.GetValue(item) == null)
{
return;
}
cell.SetCellValue(a.GetValue(item).ToString());
return;
}
});
}
private static bool WriteToDisk(string path, IWorkbook book)
{
if (File.Exists(path))
{
File.Delete(path);
}
using (FileStream fs = new FileStream(path, FileMode.Create))
{
book.Write(fs);
return true;
}
}
private static IWorkbook NPOIOpenExcel(string filename)
{
IWorkbook myworkBook;
Stream excelStream = OpenResource(filename);
if (POIFSFileSystem.HasPOIFSHeader(excelStream))
return new HSSFWorkbook(excelStream);
if (POIXMLDocument.HasOOXMLHeader(excelStream))
{
return new XSSFWorkbook(OPCPackage.Open(excelStream));
}
if (filename.EndsWith(".xlsx"))
{
return new XSSFWorkbook(excelStream);
}
if (filename.EndsWith(".xls"))
{
new HSSFWorkbook(excelStream);
}
throw new Exception("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
}
private static Stream OpenResource(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
return fs;
}
public static bool AppendExcel(string path, string sheetName, List<Object> datas)
{
if (datas == null || datas.Count <= 0)
{
return false;
}
IWorkbook workbook = NPOIOpenExcel(path);
ISheet sheet= workbook.GetSheet(sheetName);
int rowIndex = sheet.LastRowNum + 1;
datas.ForEach(a => {
ExcelAssit.WriteRow(a, workbook, sheet, rowIndex);
rowIndex++;
});
return WriteToDisk(path, workbook);
}
public static List<T> ReadExcel<T>(string path, string sheetName)
{
Type type = typeof(T);
List<T> list = new List<T>();
IWorkbook workbook = NPOIOpenExcel(path);
ISheet sheet= workbook.GetSheet(sheetName);
IRow row = sheet.GetRow(0);
Dictionary<string, PropertyInfo> propertys = new Dictionary<string, PropertyInfo>();
var propertiess = type.GetProperties().ToList();
int cellIndex = 0;
row.Cells.ForEach(a => {
propertiess.ForEach(a1 => {
AssitCellAttribute attr = a1.GetCustomAttribute<AssitCellAttribute>();
if (attr == null) { return; }
if (attr.CellTitle == a.StringCellValue.Trim())
{
propertys.Add(cellIndex.ToString(), a1);
}
cellIndex++;
});
});
for (int i = 1; i < sheet.LastRowNum; i++)
{
IRow irow = sheet.GetRow(i);
var createfn = typeof(T).GetConstructors()[0];
Object t = createfn.Invoke(null);
for (int j = 0; j < irow.Cells.Count; j++)
{
PropertyInfo pi = propertys[j.ToString()];
if (pi == null)
{
continue;
}
AssitCellAttribute attr = GetCellAttribute(pi);
if (attr == null)
{
continue;
}
if (attr.CellType == CellType.Image)
{
continue;
}
ICell a = irow.GetCell(j);
if (attr.CellType == CellType.String)
{
if (a != null && a.StringCellValue != null)
{
pi.SetValue(t, a.StringCellValue);
}
}
if (attr.CellType == CellType.Int)
{
if (a != null && a.StringCellValue != null)
{
pi.SetValue(t, int.Parse(a.StringCellValue));
}
}
if (attr.CellType == CellType.Float)
{
if (a != null && a.StringCellValue != null)
{
pi.SetValue(t, float.Parse(a.StringCellValue));
}
}
if (attr.CellType == CellType.DateTime)
{
if (a != null && a.StringCellValue != null)
{
if (pi.PropertyType == typeof(string))
{
pi.SetValue(t, a.StringCellValue);
}
if (pi.PropertyType == typeof(DateTime))
{
pi.SetValue(t, DateTime.Parse(a.StringCellValue));
}
}
}
}
list.Add((T)t);
}
return null;
}
private static void CreateTitle(ISheet sheet,Object item)
{
Type type= item.GetType();
List<AssitCellAttribute> titles = ExcelAssit.GetCellAttributes(type);
IRow row = sheet.CreateRow(0);
int cellIndex = 0;
titles.ForEach(a => {
ExcelAssit.CreateCellTitle(a, row, cellIndex);
cellIndex++;
});
}
private static List<AssitCellAttribute> GetCellAttributes(Type type)
{
List<AssitCellAttribute> list = new List<AssitCellAttribute>();
type.GetProperties().ToList().ForEach(a => {
AssitCellAttribute[] attr = (AssitCellAttribute[])a.GetCustomAttributes(typeof(AssitCellAttribute), false);
if (attr != null && attr.Length == 1)
{
list.Add(attr[0]);
}
});
return list;
}
private static AssitCellAttribute GetCellAttribute(PropertyInfo p)
{
AssitCellAttribute[] attr = (AssitCellAttribute[])p.GetCustomAttributes(typeof(AssitCellAttribute), false);
if (attr != null && attr.Length == 1)
{
return attr[0];
}
return null;
}
private static ICell CreateCellTitle(AssitCellAttribute attr,IRow row,int index)
{
ICell cell = ExcelAssit.CreateCell(attr, row, index);
cell.SetCellValue(attr.CellTitle);
return cell;
}
private static ICell CreateCell(AssitCellAttribute attr, IRow row, int index)
{
ICell cell = row.CreateCell(index);
if (attr.CellType == CellType.String)
{
cell.SetCellType(NPOI.SS.UserModel.CellType.String);
}
if (attr.CellType == CellType.Int)
{
cell.SetCellType(NPOI.SS.UserModel.CellType.Numeric);
}
if (attr.CellType == CellType.Float)
{
cell.SetCellType(NPOI.SS.UserModel.CellType.Numeric);
}
if (attr.CellType == CellType.DateTime)
{
cell.SetCellType(NPOI.SS.UserModel.CellType.String);
}
if (attr.CellType == CellType.Image)
{
cell.SetCellType(NPOI.SS.UserModel.CellType.String);
}
return cell;
}
}
}