프로젝트

일반

사용자정보

통계
| 개정판:

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

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