프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / ID2.Manager / ID2.Manager.Common / Helpers / ID2Excel.cs @ ab3c1c74

이력 | 보기 | 이력해설 | 다운로드 (23.9 KB)

1 057ca09a taeseongkim
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 b6abb7b8 yoush97
8 43ceb5b3 taeseongkim
using System.IO;
9
using System.IO.Compression;
10 8eca8767 taeseongkim
11 b6abb7b8 yoush97
using GemBox.Spreadsheet;
12
13 057ca09a taeseongkim
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 8eca8767 taeseongkim
            try
23
            {
24
            }
25
            catch (Exception)
26
            {
27
                throw;
28
            }
29
            finally
30
            {
31
                GC.Collect(2);
32
                GC.Collect(2);
33
            }
34 057ca09a taeseongkim
        }
35
36 b6abb7b8 yoush97
        public ID2Excel(List<UserInfo> users)
37 057ca09a taeseongkim
        {
38 b6abb7b8 yoush97
            UserlList = users;
39 057ca09a taeseongkim
        }
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 8eca8767 taeseongkim
        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 56f7f16e taeseongkim
        private System.Drawing.Image GetImage(string base64String)
68 43ceb5b3 taeseongkim
        {
69 56f7f16e taeseongkim
            System.Drawing.Image result = null;
70
            
71 43ceb5b3 taeseongkim
            var str =  CompressHelper.DecompressString(base64String);
72
73
            byte[] imageBytes = Convert.FromBase64String(str);
74
75
            using (MemoryStream ms = new MemoryStream(imageBytes))
76
            {
77 56f7f16e taeseongkim
                result = System.Drawing.Image.FromStream(ms);
78 43ceb5b3 taeseongkim
            }
79
80
            return result;
81
        }
82 8eca8767 taeseongkim
83 422f620d taeseongkim
        private byte[] ExcelToImageData(string base64String)
84
        {
85
            var str = CompressHelper.DecompressString(base64String);
86
87
            return Convert.FromBase64String(str);
88
        }
89
90 8eca8767 taeseongkim
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 56f7f16e taeseongkim
                result.Images = new List<System.Drawing.Image>();
164 8eca8767 taeseongkim
165
                foreach (var row in ExcelData.Where(row => row.ROW_INDEX > exRow).GroupBy(x => x.ROW_INDEX))
166
                {
167
                    var document = new Documents();
168 49ab10bd taeseongkim
                    //document.DocID = Guid.NewGuid().ToString();
169 8eca8767 taeseongkim
170 422f620d taeseongkim
                    foreach (var cell in row.OrderBy(x=>x.COUMMN_INDEX))
171 8eca8767 taeseongkim
                    {
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 d9949772 yoush97
                            //case 10:
190
                            //    document.IsTypical = value;
191
                            //    break;
192 8eca8767 taeseongkim
                            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 43ceb5b3 taeseongkim
                            case 15:
205 422f620d taeseongkim
                                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 49ab10bd taeseongkim
                                        //RefID = document.DocID,
216 422f620d taeseongkim
                                        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 43ceb5b3 taeseongkim
                                break;
228 8eca8767 taeseongkim
                            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 422f620d taeseongkim
                            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 49ab10bd taeseongkim
                                        //RefID = document.DocID,
249 422f620d taeseongkim
                                        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 2aa2a446 yoush97
                            //case 22:
262
                            //    document.IsID2Work = value;
263
                            //    break;
264 8eca8767 taeseongkim
                            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 ab3c1c74 yoush97
                            //case 31:추가필요
283
                            //    document.AVEVAWorkDate = string.IsNullOrEmpty(value) ? (DateTime?)null : Convert.ToDateTime(value);
284
                            //    break;
285 8eca8767 taeseongkim
                            case 32:
286
                                document.AVEVAStatus = value;
287
                                break;
288
                            case 33:
289
                                document.AVEVAIssues = value;
290
                                break;
291
                            case 36:
292
                                document.ProdReviewer = this.GetUser(value).ID;
293
                                break;
294
                            case 37:
295
                                document.ProdIsResult = value;
296
                                break;
297
                            case 38:
298
                                document.ProdRemarks = value;
299
                                break;
300
                            case 39:
301
                                document.ClientReviewer = this.GetUser(value).ID;
302
                                break;
303
                            case 40:
304
                                document.ClientIsResult = value;
305
                                break;
306
                            case 41:
307
                                document.ClientRemarks = value;
308
                                break;
309
                            case 42:
310
                                document.DTIsGateWay = value;
311
                                break;
312
                            case 43:
313
                                document.DTIsImport = value;
314
                                break;
315
                            case 44:
316
                                document.DTIsRegSystem = value;
317
                                break;
318
                            case 45:
319
                                document.DTRemarks = value;
320
                                break;
321
                        }
322
323
                    }
324
325
                    result.documents.Add(document);
326
                }
327
            }
328
            catch (Exception)
329
            {
330
                throw;
331
            }
332
333
            return result;
334
        }
335
336
337 057ca09a taeseongkim
        public ImportResult GemboxImport(string fileName)
338
        {
339
            ImportResult result = new ImportResult();
340
341
            StringBuilder sbErrMsg = new StringBuilder();
342
343
            try
344
            {
345
                var exFile = ExcelFile.Load(fileName);
346
                var ws = exFile.Worksheets[0];
347
348
                int rowCount = ws.Rows.Count;
349
                int columnCount = ws.CalculateMaxUsedColumns();
350
                int exRow = 8;
351
352
                #region Excel 유효성검사
353
354
                //Excel 포멧체크
355
                if (rowCount < 10 || columnCount != 45)
356
                {
357
                    result.Error = "Please, check the excel.";
358
                    return result;
359
                }
360
361
                #region 엑셀 필수값 체크(도면 : 이름,담당자, 난이도, Typical)
362
                ws.Rows.SelectMany(row => row.AllocatedCells)
363
                       .Where(col => col.Column.Index > 5 && col.Column.Index < 10 && col.Row.Index > exRow && col.Value == null)
364
                       .ToList()
365
                       .ForEach(p => sbErrMsg.Append(", " + p.Column.Name + p.Row.Name));
366
367
                if (sbErrMsg.Length > 0)
368
                {
369
                    string errMsg = sbErrMsg.ToString().Substring(2);
370
                    if (errMsg.Length > 100)
371
                    {
372
                        errMsg = $"{errMsg.Substring(0, 100)}...";
373
                    }
374
375
                    result.Error = $"Please, check null value in excel.\n{errMsg}";
376
                    return result;
377
                }
378
                #endregion
379
380
                #region 엑셀 도명명 중복 값 체크
381
                ws.Rows.SelectMany(row => row.AllocatedCells)
382
                                             .Where(col => col.Column.Index == 6 && col.Row.Index > exRow)
383
                                             .GroupBy(g => g.Row.Index)
384
                                             .Select(p => new
385
                                             {
386
                                                 rowIndex = p.Key,
387
                                                 docNo = p.Select(x => x.Value.ToString()).FirstOrDefault()
388
                                             })
389
                                             .GroupBy(g => g.docNo)
390
                                             .Where(p => p.Count() > 1)
391
                                             .Select(p => p.Select(x => (x.rowIndex + 1).ToString())
392
                                                                                        .Aggregate((x, y) => x.ToString() + "," + y.ToString())
393
                                                                                        .ToString())
394
                                             .ToList().ForEach(p => sbErrMsg.Append("\n" + p.ToString()));
395
                if (sbErrMsg.Length > 0)
396
                {
397
                    sbErrMsg.Insert(0, "\n중복 된 도면명 Excel row : ");
398
                    string errMsg = sbErrMsg.ToString();
399
                    if (errMsg.Length > 100)
400
                    {
401
                        errMsg = $"{errMsg.Substring(0, 100)}...";
402
                    }
403
404
                    result.Error = $"Please, check the duplicate value in excel.\n{errMsg}";
405
                    return result;
406
                }
407
                #endregion
408
409
                #endregion
410
411
                result.documents = new List<Documents>();
412
413
                ws.Rows.Where(row => row.Index > exRow)
414
                       .ToList()
415
                       .ForEach(p =>
416
                       {
417
                           try
418
                           {
419
                               result.documents.Add(new Documents()
420
                               {
421
                                   //UID = string.Empty,
422
                                   //Type = this.radTextBoxInsulationType.Text,
423
                                   //TempFrom = ws.Rows[exRow].Cells[p.Column.Index].IsNullOrEmpty() ? 0 : Convert.ToSingle(ws.Rows[exRow].Cells[p.Column.Index].Value),
424
                                   //TempTo = ws.Rows[exRow + 2].Cells[p.Column.Index].IsNullOrEmpty() ? 0 : Convert.ToSingle(ws.Rows[exRow + 2].Cells[p.Column.Index].Value),
425
                                   //NPS = ws.Rows[p.Row.Index].Cells[0].IsNullOrEmpty() ? 0 : Convert.ToSingle(ws.Rows[p.Row.Index].Cells[0].Value),
426
                                   //Thickness = p.IsNullOrEmpty() ? 0 : Convert.ToSingle(p.Value)
427
428
                                   RefProjectCode = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(),
429
                                   DocumentNo = ws.Rows[p.Index].Cells[6].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[6].Value.ToString(),
430
                                   PersonInCharge = ws.Rows[p.Index].Cells[7].IsNullOrEmpty() ? string.Empty : this.GetUser(ws.Rows[p.Index].Cells[7].Value.ToString()).ID,
431
                                   JobLevel = ws.Rows[p.Index].Cells[8].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[8].Value.ToString(),
432 d9949772 yoush97
                                   //IsTypical = ws.Rows[p.Index].Cells[9].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[9].Value.ToString(),
433 057ca09a taeseongkim
                                   RevisonNo = ws.Rows[p.Index].Cells[10].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[10].Value.ToString(),
434
                                   ToIsDiscussion = ws.Rows[p.Index].Cells[11].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[11].Value.ToString(),
435
                                   ToRemarks = ws.Rows[p.Index].Cells[12].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[12].Value.ToString(),
436
                                   ToCreator = ws.Rows[p.Index].Cells[13].IsNullOrEmpty() ? string.Empty : this.GetUser(ws.Rows[p.Index].Cells[13].Value.ToString()).ID,
437
                                   //ToCapture = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(),
438
                                   //ToIsMarkup = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(),
439
                                   FrReviewStatus = ws.Rows[p.Index].Cells[16].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[16].Value.ToString(),
440
                                   FrRemarks = ws.Rows[p.Index].Cells[17].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[17].Value.ToString(),
441
                                   FrCreator = ws.Rows[p.Index].Cells[18].IsNullOrEmpty() ? string.Empty : this.GetUser(ws.Rows[p.Index].Cells[18].Value.ToString()).ID,
442
                                   //FrCapture = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(),
443
                                   //FrIsMarkup = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(),
444 2aa2a446 yoush97
                                   //IsID2Work = ws.Rows[p.Index].Cells[21].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[21].Value.ToString(),
445 057ca09a taeseongkim
                                   //ID2Connection = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(),
446
                                   ID2StartDate = ws.Rows[p.Index].Cells[23].IsNullOrEmpty() ? (DateTime?)null : Convert.ToDateTime(ws.Rows[p.Index].Cells[23].Value?.ToString()),
447
                                   ID2EndDate = ws.Rows[p.Index].Cells[24].IsNullOrEmpty() ? (DateTime?)null : Convert.ToDateTime(ws.Rows[p.Index].Cells[24].Value?.ToString()),
448
                                   //ID2JobTime = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(),
449
                                   ID2Status = ws.Rows[p.Index].Cells[26].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[26].Value.ToString(),
450
                                   ID2Issues = ws.Rows[p.Index].Cells[27].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[27].Value.ToString(),
451
                                   //AVEVAConnection = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(),
452
                                   AVEVAConvertDate = ws.Rows[p.Index].Cells[29].IsNullOrEmpty() ? (DateTime?)null : Convert.ToDateTime(ws.Rows[p.Index].Cells[29].Value.ToString()),
453
                                   AVEVAReviewDate = ws.Rows[p.Index].Cells[30].IsNullOrEmpty() ? (DateTime?)null : Convert.ToDateTime(ws.Rows[p.Index].Cells[30].Value.ToString()),
454 ab3c1c74 yoush97
                                   //AVEVAWorkDate 추가필요
455 057ca09a taeseongkim
                                   AVEVAStatus = ws.Rows[p.Index].Cells[31].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[31].Value.ToString(),
456
                                   AVEVAIssues = ws.Rows[p.Index].Cells[32].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[32].Value.ToString(),
457
                                   ProdReviewer = ws.Rows[p.Index].Cells[35].IsNullOrEmpty() ? string.Empty : this.GetUser(ws.Rows[p.Index].Cells[35].Value.ToString()).ID,
458
                                   ProdIsResult = ws.Rows[p.Index].Cells[36].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[36].Value.ToString(),
459
                                   ProdRemarks = ws.Rows[p.Index].Cells[37].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[37].Value.ToString(),
460
                                   ClientReviewer = ws.Rows[p.Index].Cells[38].IsNullOrEmpty() ? string.Empty : this.GetUser(ws.Rows[p.Index].Cells[38].Value.ToString()).ID,
461
                                   ClientIsResult = ws.Rows[p.Index].Cells[39].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[39].Value.ToString(),
462
                                   ClientRemarks = ws.Rows[p.Index].Cells[40].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[40].Value.ToString(),
463
                                   DTIsGateWay = ws.Rows[p.Index].Cells[41].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[41].Value.ToString(),
464
                                   DTIsImport = ws.Rows[p.Index].Cells[42].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[42].Value.ToString(),
465
                                   DTIsRegSystem = ws.Rows[p.Index].Cells[43].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[43].Value.ToString(),
466
                                   DTRemarks = ws.Rows[p.Index].Cells[44].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[44].Value.ToString()
467
                               });
468
                            }
469
                           catch (Exception ex)
470
                           {
471
                               throw new Exception($"Excel Import Row :{p.Index} Error.",ex);
472
                           }
473
                       });
474
475
            }
476
            catch (Exception)
477
            {
478
                throw;
479
            }
480
481
            return result;
482
        }
483
    }
484
}
클립보드 이미지 추가 (최대 크기: 500 MB)