프로젝트

일반

사용자정보

통계
| 개정판:

hytos / ID2.Manager / ID2.Manager.Common / Helpers / ID2Excel.cs @ 8eca8767

이력 | 보기 | 이력해설 | 다운로드 (20.3 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

    
10

    
11
namespace ID2.Manager.Common.Helpers
12
{
13
    public class ID2Excel :IDisposable
14
    {
15
        private List<UserInfo> UserlList = new List<UserInfo>();
16

    
17

    
18
        public void Dispose()
19
        {
20
            try
21
            {
22
            }
23
            catch (Exception)
24
            {
25
                throw;
26
            }
27
            finally
28
            {
29
                GC.Collect(2);
30
                GC.Collect(2);
31
            }
32
        }
33

    
34
        public ID2Excel()
35
        {
36
            UserlList = new UserController().GetAllUserInfo().ToList();
37
        }
38

    
39
        private UserInfo GetUser(string user)
40
        {
41
            UserInfo userInfo = UserlList.Where(x => x.ID.Equals(user)).FirstOrDefault();
42
            if (userInfo != null) return userInfo;
43

    
44
            userInfo = UserlList.Where(x => x.Name.Equals(user)).FirstOrDefault();
45
            if (userInfo != null) return userInfo;
46

    
47
            return userInfo ?? new UserInfo();
48
        }
49

    
50
        private string GetColumnName(int column)
51
        {
52
            int dividend = column;
53
            string columnName = string.Empty;
54

    
55
            while (dividend > 0)
56
            {
57
                int modulo = (dividend - 1) % 26;
58
                columnName = Convert.ToChar(65 + modulo) + columnName;
59
                dividend = (dividend - modulo) / 26;
60
            }
61

    
62
            return columnName;
63
        }
64

    
65

    
66

    
67
        public ImportResult ExcelDataImport(List<ExcelData> ExcelData)
68
        {
69
            ImportResult result = new ImportResult();
70

    
71
            StringBuilder sbErrMsg = new StringBuilder();
72

    
73
            try
74
            {
75
                int rowCount = ExcelData.Max(x=>x.ROW_INDEX);
76
                int columnCount = ExcelData.Max(x => x.COUMMN_INDEX);
77
                int exRow = 9;
78

    
79
                #region Excel 유효성검사
80

    
81
                //Excel 포멧체크
82
                if (rowCount < 10 || columnCount != 45)
83
                {
84
                    result.Error = "Please, check the excel.";
85
                    return result;
86
                }
87

    
88
                #region 엑셀 필수값 체크(도면 : 이름,담당자, 난이도, Typical)
89
                ExcelData.Where(col => col.ROW_INDEX > exRow)
90
                        .Where(col => col.COUMMN_INDEX > 5 && col.COUMMN_INDEX < 11 && col.ROW_INDEX > exRow && string.IsNullOrEmpty(col.VALUE))
91
                       .ToList()
92
                       .ForEach(p => sbErrMsg.Append(", " + p.TopLeftCell));
93

    
94
                if (sbErrMsg.Length > 0)
95
                {
96
                    string errMsg = sbErrMsg.ToString().Substring(2);
97
                    if (errMsg.Length > 100)
98
                    {
99
                        errMsg = $"{errMsg.Substring(0, 100)}...";
100
                    }
101

    
102
                    result.Error = $"Please, check null value in excel.\n{errMsg}";
103
                    return result;
104
                }
105
                #endregion
106

    
107
                #region 엑셀 도명명 중복 값 체크
108
                ExcelData.Where(col => col.COUMMN_INDEX == 7 && col.ROW_INDEX > exRow)
109
                        .GroupBy(g => g.ROW_INDEX)
110
                        .Select(p => new
111
                        {
112
                            rowIndex = p.Key,
113
                            docNo = p.Select(x => x.VALUE.ToString()).FirstOrDefault()
114
                        })
115
                        .GroupBy(g => g.docNo)
116
                        .Where(p => p.Count() > 1)
117
                        .Select(p => p.Select(x => (x.rowIndex + 1).ToString())
118
                                                                .Aggregate((x, y) => x.ToString() + "," + y.ToString())
119
                                                                .ToString())
120
                        .ToList().ForEach(p => sbErrMsg.Append("\n" + p.ToString()));
121

    
122
                if (sbErrMsg.Length > 0)
123
                {
124
                    sbErrMsg.Insert(0, "\n중복 된 도면명 Excel row : ");
125
                    string errMsg = sbErrMsg.ToString();
126
                    if (errMsg.Length > 100)
127
                    {
128
                        errMsg = $"{errMsg.Substring(0, 100)}...";
129
                    }
130

    
131
                    result.Error = $"Please, check the duplicate value in excel.\n{errMsg}";
132
                    return result;
133
                }
134
                #endregion
135

    
136
                #endregion
137

    
138
                result.documents = new List<Documents>();
139

    
140
                foreach (var row in ExcelData.Where(row => row.ROW_INDEX > exRow).GroupBy(x => x.ROW_INDEX))
141
                {
142
                    var document = new Documents();
143

    
144
                    foreach (var cell in row)
145
                    {
146
                        var value = cell.VALUE.DefalutValue();
147

    
148
                        switch (cell.COUMMN_INDEX)
149
                        {
150
                            case 6:
151
                                document.RefProjectCode = value;
152
                                break;
153
                            case 7:
154
                                document.DocumentNo = value;
155
                                break;
156
                            case 8:
157
                                document.PersonInCharge = this.GetUser(value).ID;
158
                                break;
159
                            case 9:
160
                                document.JobLevel = value;
161
                                break;
162

    
163
                            case 10:
164
                                document.IsTypical = value;
165
                                break;
166
                            case 11:
167
                                document.RevisonNo = value;
168
                                break;
169
                            case 12:
170
                                document.ToIsDiscussion = value;
171
                                break;
172
                            case 13:
173
                                document.ToRemarks = value;
174
                                break;
175
                            case 14:
176
                                document.ToCreator = this.GetUser(value).ID;
177
                                break;
178
                            case 17:
179
                                document.FrReviewStatus = value;
180
                                break;
181
                            case 18:
182
                                document.FrRemarks = value;
183
                                break;
184
                            case 19:
185
                                document.FrCreator = this.GetUser(value).ID;
186
                                break;
187
                            case 22:
188
                                document.IsID2Work = value;
189
                                break;
190
                            case 24:
191
                                document.ID2StartDate = string.IsNullOrEmpty(value) ? (DateTime?)null : Convert.ToDateTime(value);
192
                                break;
193
                            case 25:
194
                                document.ID2EndDate = string.IsNullOrEmpty(value) ? (DateTime?)null : Convert.ToDateTime(value);
195
                                break;
196
                            case 27:
197
                                document.ID2Status = value;
198
                                break;
199
                            case 28:
200
                                document.ID2Issues = value;
201
                                break;
202
                            case 30:
203
                                document.AVEVAConvertDate = string.IsNullOrEmpty(value) ? (DateTime?)null : Convert.ToDateTime(value);
204
                                break;
205
                            case 31:
206
                                document.AVEVAReviewDate = string.IsNullOrEmpty(value) ? (DateTime?)null : Convert.ToDateTime(value);
207
                                break;
208
                            case 32:
209
                                document.AVEVAStatus = value;
210
                                break;
211
                            case 33:
212
                                document.AVEVAIssues = value;
213
                                break;
214
                            case 36:
215
                                document.ProdReviewer = this.GetUser(value).ID;
216
                                break;
217
                            case 37:
218
                                document.ProdIsResult = value;
219
                                break;
220
                            case 38:
221
                                document.ProdRemarks = value;
222
                                break;
223
                            case 39:
224
                                document.ClientReviewer = this.GetUser(value).ID;
225
                                break;
226
                            case 40:
227
                                document.ClientIsResult = value;
228
                                break;
229
                            case 41:
230
                                document.ClientRemarks = value;
231
                                break;
232
                            case 42:
233
                                document.DTIsGateWay = value;
234
                                break;
235
                            case 43:
236
                                document.DTIsImport = value;
237
                                break;
238
                            case 44:
239
                                document.DTIsRegSystem = value;
240
                                break;
241
                            case 45:
242
                                document.DTRemarks = value;
243
                                break;
244
                        }
245

    
246
                    }
247

    
248
                    result.documents.Add(document);
249
                }
250
            }
251
            catch (Exception)
252
            {
253
                throw;
254
            }
255

    
256
            return result;
257
        }
258

    
259

    
260
        public ImportResult GemboxImport(string fileName)
261
        {
262
            ImportResult result = new ImportResult();
263

    
264
            StringBuilder sbErrMsg = new StringBuilder();
265

    
266
            try
267
            {
268
                var exFile = ExcelFile.Load(fileName);
269
                var ws = exFile.Worksheets[0];
270

    
271
                int rowCount = ws.Rows.Count;
272
                int columnCount = ws.CalculateMaxUsedColumns();
273
                int exRow = 8;
274

    
275
                #region Excel 유효성검사
276

    
277
                //Excel 포멧체크
278
                if (rowCount < 10 || columnCount != 45)
279
                {
280
                    result.Error = "Please, check the excel.";
281
                    return result;
282
                }
283

    
284
                #region 엑셀 필수값 체크(도면 : 이름,담당자, 난이도, Typical)
285
                ws.Rows.SelectMany(row => row.AllocatedCells)
286
                       .Where(col => col.Column.Index > 5 && col.Column.Index < 10 && col.Row.Index > exRow && col.Value == null)
287
                       .ToList()
288
                       .ForEach(p => sbErrMsg.Append(", " + p.Column.Name + p.Row.Name));
289

    
290
                if (sbErrMsg.Length > 0)
291
                {
292
                    string errMsg = sbErrMsg.ToString().Substring(2);
293
                    if (errMsg.Length > 100)
294
                    {
295
                        errMsg = $"{errMsg.Substring(0, 100)}...";
296
                    }
297

    
298
                    result.Error = $"Please, check null value in excel.\n{errMsg}";
299
                    return result;
300
                }
301
                #endregion
302

    
303
                #region 엑셀 도명명 중복 값 체크
304
                ws.Rows.SelectMany(row => row.AllocatedCells)
305
                                             .Where(col => col.Column.Index == 6 && col.Row.Index > exRow)
306
                                             .GroupBy(g => g.Row.Index)
307
                                             .Select(p => new
308
                                             {
309
                                                 rowIndex = p.Key,
310
                                                 docNo = p.Select(x => x.Value.ToString()).FirstOrDefault()
311
                                             })
312
                                             .GroupBy(g => g.docNo)
313
                                             .Where(p => p.Count() > 1)
314
                                             .Select(p => p.Select(x => (x.rowIndex + 1).ToString())
315
                                                                                        .Aggregate((x, y) => x.ToString() + "," + y.ToString())
316
                                                                                        .ToString())
317
                                             .ToList().ForEach(p => sbErrMsg.Append("\n" + p.ToString()));
318
                if (sbErrMsg.Length > 0)
319
                {
320
                    sbErrMsg.Insert(0, "\n중복 된 도면명 Excel row : ");
321
                    string errMsg = sbErrMsg.ToString();
322
                    if (errMsg.Length > 100)
323
                    {
324
                        errMsg = $"{errMsg.Substring(0, 100)}...";
325
                    }
326

    
327
                    result.Error = $"Please, check the duplicate value in excel.\n{errMsg}";
328
                    return result;
329
                }
330
                #endregion
331

    
332
                #endregion
333

    
334
                result.documents = new List<Documents>();
335

    
336
                ws.Rows.Where(row => row.Index > exRow)
337
                       .ToList()
338
                       .ForEach(p =>
339
                       {
340
                           try
341
                           {
342
                               result.documents.Add(new Documents()
343
                               {
344
                                   //UID = string.Empty,
345
                                   //Type = this.radTextBoxInsulationType.Text,
346
                                   //TempFrom = ws.Rows[exRow].Cells[p.Column.Index].IsNullOrEmpty() ? 0 : Convert.ToSingle(ws.Rows[exRow].Cells[p.Column.Index].Value),
347
                                   //TempTo = ws.Rows[exRow + 2].Cells[p.Column.Index].IsNullOrEmpty() ? 0 : Convert.ToSingle(ws.Rows[exRow + 2].Cells[p.Column.Index].Value),
348
                                   //NPS = ws.Rows[p.Row.Index].Cells[0].IsNullOrEmpty() ? 0 : Convert.ToSingle(ws.Rows[p.Row.Index].Cells[0].Value),
349
                                   //Thickness = p.IsNullOrEmpty() ? 0 : Convert.ToSingle(p.Value)
350

    
351
                                   RefProjectCode = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(),
352
                                   DocumentNo = ws.Rows[p.Index].Cells[6].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[6].Value.ToString(),
353
                                   PersonInCharge = ws.Rows[p.Index].Cells[7].IsNullOrEmpty() ? string.Empty : this.GetUser(ws.Rows[p.Index].Cells[7].Value.ToString()).ID,
354
                                   JobLevel = ws.Rows[p.Index].Cells[8].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[8].Value.ToString(),
355
                                   IsTypical = ws.Rows[p.Index].Cells[9].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[9].Value.ToString(),
356
                                   RevisonNo = ws.Rows[p.Index].Cells[10].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[10].Value.ToString(),
357
                                   ToIsDiscussion = ws.Rows[p.Index].Cells[11].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[11].Value.ToString(),
358
                                   ToRemarks = ws.Rows[p.Index].Cells[12].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[12].Value.ToString(),
359
                                   ToCreator = ws.Rows[p.Index].Cells[13].IsNullOrEmpty() ? string.Empty : this.GetUser(ws.Rows[p.Index].Cells[13].Value.ToString()).ID,
360
                                   //ToCapture = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(),
361
                                   //ToIsMarkup = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(),
362
                                   FrReviewStatus = ws.Rows[p.Index].Cells[16].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[16].Value.ToString(),
363
                                   FrRemarks = ws.Rows[p.Index].Cells[17].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[17].Value.ToString(),
364
                                   FrCreator = ws.Rows[p.Index].Cells[18].IsNullOrEmpty() ? string.Empty : this.GetUser(ws.Rows[p.Index].Cells[18].Value.ToString()).ID,
365
                                   //FrCapture = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(),
366
                                   //FrIsMarkup = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(),
367
                                   IsID2Work = ws.Rows[p.Index].Cells[21].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[21].Value.ToString(),
368
                                   //ID2Connection = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(),
369
                                   ID2StartDate = ws.Rows[p.Index].Cells[23].IsNullOrEmpty() ? (DateTime?)null : Convert.ToDateTime(ws.Rows[p.Index].Cells[23].Value?.ToString()),
370
                                   ID2EndDate = ws.Rows[p.Index].Cells[24].IsNullOrEmpty() ? (DateTime?)null : Convert.ToDateTime(ws.Rows[p.Index].Cells[24].Value?.ToString()),
371
                                   //ID2JobTime = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(),
372
                                   ID2Status = ws.Rows[p.Index].Cells[26].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[26].Value.ToString(),
373
                                   ID2Issues = ws.Rows[p.Index].Cells[27].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[27].Value.ToString(),
374
                                   //AVEVAConnection = ws.Rows[p.Index].Cells[5].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[5].Value.ToString(),
375
                                   AVEVAConvertDate = ws.Rows[p.Index].Cells[29].IsNullOrEmpty() ? (DateTime?)null : Convert.ToDateTime(ws.Rows[p.Index].Cells[29].Value.ToString()),
376
                                   AVEVAReviewDate = ws.Rows[p.Index].Cells[30].IsNullOrEmpty() ? (DateTime?)null : Convert.ToDateTime(ws.Rows[p.Index].Cells[30].Value.ToString()),
377
                                   AVEVAStatus = ws.Rows[p.Index].Cells[31].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[31].Value.ToString(),
378
                                   AVEVAIssues = ws.Rows[p.Index].Cells[32].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[32].Value.ToString(),
379
                                   ProdReviewer = ws.Rows[p.Index].Cells[35].IsNullOrEmpty() ? string.Empty : this.GetUser(ws.Rows[p.Index].Cells[35].Value.ToString()).ID,
380
                                   ProdIsResult = ws.Rows[p.Index].Cells[36].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[36].Value.ToString(),
381
                                   ProdRemarks = ws.Rows[p.Index].Cells[37].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[37].Value.ToString(),
382
                                   ClientReviewer = ws.Rows[p.Index].Cells[38].IsNullOrEmpty() ? string.Empty : this.GetUser(ws.Rows[p.Index].Cells[38].Value.ToString()).ID,
383
                                   ClientIsResult = ws.Rows[p.Index].Cells[39].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[39].Value.ToString(),
384
                                   ClientRemarks = ws.Rows[p.Index].Cells[40].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[40].Value.ToString(),
385
                                   DTIsGateWay = ws.Rows[p.Index].Cells[41].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[41].Value.ToString(),
386
                                   DTIsImport = ws.Rows[p.Index].Cells[42].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[42].Value.ToString(),
387
                                   DTIsRegSystem = ws.Rows[p.Index].Cells[43].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[43].Value.ToString(),
388
                                   DTRemarks = ws.Rows[p.Index].Cells[44].IsNullOrEmpty() ? string.Empty : ws.Rows[p.Index].Cells[44].Value.ToString()
389
                               });
390
                            }
391
                           catch (Exception ex)
392
                           {
393
                               throw new Exception($"Excel Import Row :{p.Index} Error.",ex);
394
                           }
395
                       });
396

    
397
            }
398
            catch (Exception)
399
            {
400
                throw;
401
            }
402

    
403
            return result;
404
        }
405
    }
406
}
클립보드 이미지 추가 (최대 크기: 500 MB)