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