hytos / ID2.Manager / ID2.Manager.Common / Helpers / ID2Excel.cs @ d9949772
이력 | 보기 | 이력해설 | 다운로드 (23.6 KB)
1 |
using ID2.Manager.Data.Models; |
---|---|
2 |
using System; |
3 |
using System.Collections.Generic; |
4 |
using System.Linq; |
5 |
using System.Text; |
6 |
using System.Threading.Tasks; |
7 |
|
8 |
using System.IO; |
9 |
using System.IO.Compression; |
10 |
|
11 |
using GemBox.Spreadsheet; |
12 |
|
13 |
namespace ID2.Manager.Common.Helpers |
14 |
{ |
15 |
public class ID2Excel :IDisposable |
16 |
{ |
17 |
private List<UserInfo> UserlList = new List<UserInfo>(); |
18 |
|
19 |
|
20 |
public void Dispose() |
21 |
{ |
22 |
try |
23 |
{ |
24 |
} |
25 |
catch (Exception) |
26 |
{ |
27 |
throw; |
28 |
} |
29 |
finally |
30 |
{ |
31 |
GC.Collect(2); |
32 |
GC.Collect(2); |
33 |
} |
34 |
} |
35 |
|
36 |
public ID2Excel(List<UserInfo> users) |
37 |
{ |
38 |
UserlList = users; |
39 |
} |
40 |
|
41 |
private UserInfo GetUser(string user) |
42 |
{ |
43 |
UserInfo userInfo = UserlList.Where(x => x.ID.Equals(user)).FirstOrDefault(); |
44 |
if (userInfo != null) return userInfo; |
45 |
|
46 |
userInfo = UserlList.Where(x => x.Name.Equals(user)).FirstOrDefault(); |
47 |
if (userInfo != null) return userInfo; |
48 |
|
49 |
return userInfo ?? new UserInfo(); |
50 |
} |
51 |
|
52 |
private string GetColumnName(int column) |
53 |
{ |
54 |
int dividend = column; |
55 |
string columnName = string.Empty; |
56 |
|
57 |
while (dividend > 0) |
58 |
{ |
59 |
int modulo = (dividend - 1) % 26; |
60 |
columnName = Convert.ToChar(65 + modulo) + columnName; |
61 |
dividend = (dividend - modulo) / 26; |
62 |
} |
63 |
|
64 |
return columnName; |
65 |
} |
66 |
|
67 |
private System.Drawing.Image GetImage(string base64String) |
68 |
{ |
69 |
System.Drawing.Image result = null; |
70 |
|
71 |
var str = CompressHelper.DecompressString(base64String); |
72 |
|
73 |
byte[] imageBytes = Convert.FromBase64String(str); |
74 |
|
75 |
using (MemoryStream ms = new MemoryStream(imageBytes)) |
76 |
{ |
77 |
result = System.Drawing.Image.FromStream(ms); |
78 |
} |
79 |
|
80 |
return result; |
81 |
} |
82 |
|
83 |
private byte[] ExcelToImageData(string base64String) |
84 |
{ |
85 |
var str = CompressHelper.DecompressString(base64String); |
86 |
|
87 |
return Convert.FromBase64String(str); |
88 |
} |
89 |
|
90 |
|
91 |
public ImportResult ExcelDataImport(List<ExcelData> ExcelData) |
92 |
{ |
93 |
ImportResult result = new ImportResult(); |
94 |
|
95 |
StringBuilder sbErrMsg = new StringBuilder(); |
96 |
|
97 |
try |
98 |
{ |
99 |
int rowCount = ExcelData.Max(x=>x.ROW_INDEX); |
100 |
int columnCount = ExcelData.Max(x => x.COUMMN_INDEX); |
101 |
int exRow = 9; |
102 |
|
103 |
#region Excel 유효성검사 |
104 |
|
105 |
//Excel 포멧체크 |
106 |
if (rowCount < 10 || columnCount != 45) |
107 |
{ |
108 |
result.Error = "Please, check the excel."; |
109 |
return result; |
110 |
} |
111 |
|
112 |
#region 엑셀 필수값 체크(도면 : 이름,담당자, 난이도, Typical) |
113 |
ExcelData.Where(col => col.ROW_INDEX > exRow) |
114 |
.Where(col => col.COUMMN_INDEX > 5 && col.COUMMN_INDEX < 11 && col.ROW_INDEX > exRow && string.IsNullOrEmpty(col.VALUE)) |
115 |
.ToList() |
116 |
.ForEach(p => sbErrMsg.Append(", " + p.TopLeftCell)); |
117 |
|
118 |
if (sbErrMsg.Length > 0) |
119 |
{ |
120 |
string errMsg = sbErrMsg.ToString().Substring(2); |
121 |
if (errMsg.Length > 100) |
122 |
{ |
123 |
errMsg = $"{errMsg.Substring(0, 100)}..."; |
124 |
} |
125 |
|
126 |
result.Error = $"Please, check null value in excel.\n{errMsg}"; |
127 |
return result; |
128 |
} |
129 |
#endregion |
130 |
|
131 |
#region 엑셀 도명명 중복 값 체크 |
132 |
ExcelData.Where(col => col.COUMMN_INDEX == 7 && col.ROW_INDEX > exRow) |
133 |
.GroupBy(g => g.ROW_INDEX) |
134 |
.Select(p => new |
135 |
{ |
136 |
rowIndex = p.Key, |
137 |
docNo = p.Select(x => x.VALUE.ToString()).FirstOrDefault() |
138 |
}) |
139 |
.GroupBy(g => g.docNo) |
140 |
.Where(p => p.Count() > 1) |
141 |
.Select(p => p.Select(x => (x.rowIndex + 1).ToString()) |
142 |
.Aggregate((x, y) => x.ToString() + "," + y.ToString()) |
143 |
.ToString()) |
144 |
.ToList().ForEach(p => sbErrMsg.Append("\n" + p.ToString())); |
145 |
|
146 |
if (sbErrMsg.Length > 0) |
147 |
{ |
148 |
sbErrMsg.Insert(0, "\n중복 된 도면명 Excel row : "); |
149 |
string errMsg = sbErrMsg.ToString(); |
150 |
if (errMsg.Length > 100) |
151 |
{ |
152 |
errMsg = $"{errMsg.Substring(0, 100)}..."; |
153 |
} |
154 |
|
155 |
result.Error = $"Please, check the duplicate value in excel.\n{errMsg}"; |
156 |
return result; |
157 |
} |
158 |
#endregion |
159 |
|
160 |
#endregion |
161 |
|
162 |
result.documents = new List<Documents>(); |
163 |
result.Images = new List<System.Drawing.Image>(); |
164 |
|
165 |
foreach (var row in ExcelData.Where(row => row.ROW_INDEX > exRow).GroupBy(x => x.ROW_INDEX)) |
166 |
{ |
167 |
var document = new Documents(); |
168 |
//document.DocID = Guid.NewGuid().ToString(); |
169 |
|
170 |
foreach (var cell in row.OrderBy(x=>x.COUMMN_INDEX)) |
171 |
{ |
172 |
var value = cell.VALUE.DefalutValue(); |
173 |
|
174 |
switch (cell.COUMMN_INDEX) |
175 |
{ |
176 |
case 6: |
177 |
document.RefProjectCode = value; |
178 |
break; |
179 |
case 7: |
180 |
document.DocumentNo = value; |
181 |
break; |
182 |
case 8: |
183 |
document.PersonInCharge = this.GetUser(value).ID; |
184 |
break; |
185 |
case 9: |
186 |
document.JobLevel = value; |
187 |
break; |
188 |
|
189 |
//case 10: |
190 |
// document.IsTypical = value; |
191 |
// break; |
192 |
case 11: |
193 |
document.RevisonNo = value; |
194 |
break; |
195 |
case 12: |
196 |
document.ToIsDiscussion = value; |
197 |
break; |
198 |
case 13: |
199 |
document.ToRemarks = value; |
200 |
break; |
201 |
case 14: |
202 |
document.ToCreator = this.GetUser(value).ID; |
203 |
break; |
204 |
case 15: |
205 |
if (value != null) |
206 |
{ |
207 |
if (document.AttFiles == null) |
208 |
{ |
209 |
document.AttFiles = new List<AttFileInfo>(); |
210 |
} |
211 |
|
212 |
document.AttFiles.Add(new AttFileInfo |
213 |
{ |
214 |
FileID = Guid.NewGuid().ToString(), |
215 |
//RefID = document.DocID, |
216 |
Category = "toreview", |
217 |
FileType = "image/png", |
218 |
FileName = "ClipBoard", |
219 |
FilePath = "ClipBoard", |
220 |
FileExtension = ".png", |
221 |
CreatedDate = DateTime.Now, |
222 |
Creator = document.ToCreator, |
223 |
FileData = ExcelToImageData(value) |
224 |
|
225 |
}); |
226 |
} |
227 |
break; |
228 |
case 17: |
229 |
document.FrReviewStatus = value; |
230 |
break; |
231 |
case 18: |
232 |
document.FrRemarks = value; |
233 |
break; |
234 |
case 19: |
235 |
document.FrCreator = this.GetUser(value).ID; |
236 |
break; |
237 |
case 20: |
238 |
if (value != null) |
239 |
{ |
240 |
if (document.AttFiles == null) |
241 |
{ |
242 |
document.AttFiles = new List<AttFileInfo>(); |
243 |
} |
244 |
|
245 |
document.AttFiles.Add(new AttFileInfo |
246 |
{ |
247 |
FileID = Guid.NewGuid().ToString(), |
248 |
//RefID = document.DocID, |
249 |
Category = "frreview", |
250 |
FileType = "image/png", |
251 |
FileName = "ClipBoard", |
252 |
FilePath = "ClipBoard", |
253 |
FileExtension = ".png", |
254 |
CreatedDate = DateTime.Now, |
255 |
Creator = document.FrCreator, |
256 |
FileData = ExcelToImageData(value) |
257 |
|
258 |
}); |
259 |
} |
260 |
break; |
261 |
//case 22: |
262 |
// document.IsID2Work = value; |
263 |
// break; |
264 |
case 24: |
265 |
document.ID2StartDate = string.IsNullOrEmpty(value) ? (DateTime?)null : Convert.ToDateTime(value); |
266 |
break; |
267 |
case 25: |
268 |
document.ID2EndDate = string.IsNullOrEmpty(value) ? (DateTime?)null : Convert.ToDateTime(value); |
269 |
break; |
270 |
case 27: |
271 |
document.ID2Status = value; |
272 |
break; |
273 |
case 28: |
274 |
document.ID2Issues = value; |
275 |
break; |
276 |
case 30: |
277 |
document.AVEVAConvertDate = string.IsNullOrEmpty(value) ? (DateTime?)null : Convert.ToDateTime(value); |
278 |
break; |
279 |
case 31: |
280 |
document.AVEVAReviewDate = string.IsNullOrEmpty(value) ? (DateTime?)null : Convert.ToDateTime(value); |
281 |
break; |
282 |
case 32: |
283 |
document.AVEVAStatus = value; |
284 |
break; |
285 |
case 33: |
286 |
document.AVEVAIssues = value; |
287 |
break; |
288 |
case 36: |
289 |
document.ProdReviewer = this.GetUser(value).ID; |
290 |
break; |
291 |
case 37: |
292 |
document.ProdIsResult = value; |
293 |
break; |
294 |
case 38: |
295 |
document.ProdRemarks = value; |
296 |
break; |
297 |
case 39: |
298 |
document.ClientReviewer = this.GetUser(value).ID; |
299 |
break; |
300 |
case 40: |
301 |
document.ClientIsResult = value; |
302 |
break; |
303 |
case 41: |
304 |
document.ClientRemarks = value; |
305 |
break; |
306 |
case 42: |
307 |
document.DTIsGateWay = value; |
308 |
break; |
309 |
case 43: |
310 |
document.DTIsImport = value; |
311 |
break; |
312 |
case 44: |
313 |
document.DTIsRegSystem = value; |
314 |
break; |
315 |
case 45: |
316 |
document.DTRemarks = value; |
317 |
break; |
318 |
} |
319 |
|
320 |
} |
321 |
|
322 |
result.documents.Add(document); |
323 |
} |
324 |
} |
325 |
catch (Exception) |
326 |
{ |
327 |
throw; |
328 |
} |
329 |
|
330 |
return result; |
331 |
} |
332 |
|
333 |
|
334 |
public ImportResult GemboxImport(string fileName) |
335 |
{ |
336 |
ImportResult result = new ImportResult(); |
337 |
|
338 |
StringBuilder sbErrMsg = new StringBuilder(); |
339 |
|
340 |
try |
341 |
{ |
342 |
var exFile = ExcelFile.Load(fileName); |
343 |
var ws = exFile.Worksheets[0]; |
344 |
|
345 |
int rowCount = ws.Rows.Count; |
346 |
int columnCount = ws.CalculateMaxUsedColumns(); |
347 |
int exRow = 8; |
348 |
|
349 |
#region Excel 유효성검사 |
350 |
|
351 |
//Excel 포멧체크 |
352 |
if (rowCount < 10 || columnCount != 45) |
353 |
{ |
354 |
result.Error = "Please, check the excel."; |
355 |
return result; |
356 |
} |
357 |
|
358 |
#region 엑셀 필수값 체크(도면 : 이름,담당자, 난이도, Typical) |
359 |
ws.Rows.SelectMany(row => row.AllocatedCells) |
360 |
.Where(col => col.Column.Index > 5 && col.Column.Index < 10 && col.Row.Index > exRow && col.Value == null) |
361 |
.ToList() |
362 |
.ForEach(p => sbErrMsg.Append(", " + p.Column.Name + p.Row.Name)); |
363 |
|
364 |
if (sbErrMsg.Length > 0) |
365 |
{ |
366 |
string errMsg = sbErrMsg.ToString().Substring(2); |
367 |
if (errMsg.Length > 100) |
368 |
{ |
369 |
errMsg = $"{errMsg.Substring(0, 100)}..."; |
370 |
} |
371 |
|
372 |
result.Error = $"Please, check null value in excel.\n{errMsg}"; |
373 |
return result; |
374 |
} |
375 |
#endregion |
376 |
|
377 |
#region 엑셀 도명명 중복 값 체크 |
378 |
ws.Rows.SelectMany(row => row.AllocatedCells) |
379 |
.Where(col => col.Column.Index == 6 && col.Row.Index > exRow) |
380 |
.GroupBy(g => g.Row.Index) |
381 |
.Select(p => new |
382 |
{ |
383 |
rowIndex = p.Key, |
384 |
docNo = p.Select(x => x.Value.ToString()).FirstOrDefault() |
385 |
}) |
386 |
.GroupBy(g => g.docNo) |
387 |
.Where(p => p.Count() > 1) |
388 |
.Select(p => p.Select(x => (x.rowIndex + 1).ToString()) |
389 |
.Aggregate((x, y) => x.ToString() + "," + y.ToString()) |
390 |
.ToString()) |
391 |
.ToList().ForEach(p => sbErrMsg.Append("\n" + p.ToString())); |
392 |
if (sbErrMsg.Length > 0) |
393 |
{ |
394 |
sbErrMsg.Insert(0, "\n중복 된 도면명 Excel row : "); |
395 |
string errMsg = sbErrMsg.ToString(); |
396 |
if (errMsg.Length > 100) |
397 |
{ |
398 |
errMsg = $"{errMsg.Substring(0, 100)}..."; |
399 |
} |
400 |
|
401 |
result.Error = $"Please, check the duplicate value in excel.\n{errMsg}"; |
402 |
return result; |
403 |
} |
404 |
#endregion |
405 |
|
406 |
#endregion |
407 |
|
408 |
result.documents = new List<Documents>(); |
409 |
|
410 |
ws.Rows.Where(row => row.Index > exRow) |
411 |
.ToList() |
412 |
.ForEach(p => |
413 |
{ |
414 |
try |
415 |
{ |
416 |
result.documents.Add(new Documents() |
417 |
{ |
418 |
//UID = string.Empty, |
419 |
//Type = this.radTextBoxInsulationType.Text, |
420 |
//TempFrom = ws.Rows[exRow].Cells[p.Column.Index].IsNullOrEmpty() ? 0 : Convert.ToSingle(ws.Rows[exRow].Cells[p.Column.Index].Value), |
421 |
//TempTo = ws.Rows[exRow + 2].Cells[p.Column.Index].IsNullOrEmpty() ? 0 : Convert.ToSingle(ws.Rows[exRow + 2].Cells[p.Column.Index].Value), |
422 |
//NPS = ws.Rows[p.Row.Index].Cells[0].IsNullOrEmpty() ? 0 : Convert.ToSingle(ws.Rows[p.Row.Index].Cells[0].Value), |
423 |
//Thickness = p.IsNullOrEmpty() ? 0 : Convert.ToSingle(p.Value) |
424 |
|
425 |
RefProjectCode = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(), |
426 |
DocumentNo = ws.Rows[p.Index].Cells[6].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[6].Value.ToString(), |
427 |
PersonInCharge = ws.Rows[p.Index].Cells[7].IsNullOrEmpty() ? string.Empty : this.GetUser(ws.Rows[p.Index].Cells[7].Value.ToString()).ID, |
428 |
JobLevel = ws.Rows[p.Index].Cells[8].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[8].Value.ToString(), |
429 |
//IsTypical = ws.Rows[p.Index].Cells[9].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[9].Value.ToString(), |
430 |
RevisonNo = ws.Rows[p.Index].Cells[10].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[10].Value.ToString(), |
431 |
ToIsDiscussion = ws.Rows[p.Index].Cells[11].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[11].Value.ToString(), |
432 |
ToRemarks = ws.Rows[p.Index].Cells[12].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[12].Value.ToString(), |
433 |
ToCreator = ws.Rows[p.Index].Cells[13].IsNullOrEmpty() ? string.Empty : this.GetUser(ws.Rows[p.Index].Cells[13].Value.ToString()).ID, |
434 |
//ToCapture = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(), |
435 |
//ToIsMarkup = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(), |
436 |
FrReviewStatus = ws.Rows[p.Index].Cells[16].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[16].Value.ToString(), |
437 |
FrRemarks = ws.Rows[p.Index].Cells[17].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[17].Value.ToString(), |
438 |
FrCreator = ws.Rows[p.Index].Cells[18].IsNullOrEmpty() ? string.Empty : this.GetUser(ws.Rows[p.Index].Cells[18].Value.ToString()).ID, |
439 |
//FrCapture = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(), |
440 |
//FrIsMarkup = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(), |
441 |
//IsID2Work = ws.Rows[p.Index].Cells[21].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[21].Value.ToString(), |
442 |
//ID2Connection = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(), |
443 |
ID2StartDate = ws.Rows[p.Index].Cells[23].IsNullOrEmpty() ? (DateTime?)null : Convert.ToDateTime(ws.Rows[p.Index].Cells[23].Value?.ToString()), |
444 |
ID2EndDate = ws.Rows[p.Index].Cells[24].IsNullOrEmpty() ? (DateTime?)null : Convert.ToDateTime(ws.Rows[p.Index].Cells[24].Value?.ToString()), |
445 |
//ID2JobTime = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(), |
446 |
ID2Status = ws.Rows[p.Index].Cells[26].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[26].Value.ToString(), |
447 |
ID2Issues = ws.Rows[p.Index].Cells[27].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[27].Value.ToString(), |
448 |
//AVEVAConnection = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(), |
449 |
AVEVAConvertDate = ws.Rows[p.Index].Cells[29].IsNullOrEmpty() ? (DateTime?)null : Convert.ToDateTime(ws.Rows[p.Index].Cells[29].Value.ToString()), |
450 |
AVEVAReviewDate = ws.Rows[p.Index].Cells[30].IsNullOrEmpty() ? (DateTime?)null : Convert.ToDateTime(ws.Rows[p.Index].Cells[30].Value.ToString()), |
451 |
AVEVAStatus = ws.Rows[p.Index].Cells[31].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[31].Value.ToString(), |
452 |
AVEVAIssues = ws.Rows[p.Index].Cells[32].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[32].Value.ToString(), |
453 |
ProdReviewer = ws.Rows[p.Index].Cells[35].IsNullOrEmpty() ? string.Empty : this.GetUser(ws.Rows[p.Index].Cells[35].Value.ToString()).ID, |
454 |
ProdIsResult = ws.Rows[p.Index].Cells[36].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[36].Value.ToString(), |
455 |
ProdRemarks = ws.Rows[p.Index].Cells[37].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[37].Value.ToString(), |
456 |
ClientReviewer = ws.Rows[p.Index].Cells[38].IsNullOrEmpty() ? string.Empty : this.GetUser(ws.Rows[p.Index].Cells[38].Value.ToString()).ID, |
457 |
ClientIsResult = ws.Rows[p.Index].Cells[39].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[39].Value.ToString(), |
458 |
ClientRemarks = ws.Rows[p.Index].Cells[40].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[40].Value.ToString(), |
459 |
DTIsGateWay = ws.Rows[p.Index].Cells[41].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[41].Value.ToString(), |
460 |
DTIsImport = ws.Rows[p.Index].Cells[42].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[42].Value.ToString(), |
461 |
DTIsRegSystem = ws.Rows[p.Index].Cells[43].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[43].Value.ToString(), |
462 |
DTRemarks = ws.Rows[p.Index].Cells[44].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[44].Value.ToString() |
463 |
}); |
464 |
} |
465 |
catch (Exception ex) |
466 |
{ |
467 |
throw new Exception($"Excel Import Row :{p.Index} Error.",ex); |
468 |
} |
469 |
}); |
470 |
|
471 |
} |
472 |
catch (Exception) |
473 |
{ |
474 |
throw; |
475 |
} |
476 |
|
477 |
return result; |
478 |
} |
479 |
} |
480 |
} |