hytos / DTI_PID / SPPIDConverter_AutoModeling / Utill / ExcelUtill.cs @ b8ff72eb
이력 | 보기 | 이력해설 | 다운로드 (3.07 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using GemBox.Spreadsheet; |
6 |
using System.Threading.Tasks; |
7 |
|
8 |
|
9 |
namespace SPPID.Utill |
10 |
{ |
11 |
static class ExcelUtill |
12 |
{ |
13 |
const string SPPID_MappingCell = "N"; |
14 |
|
15 |
|
16 |
readonly static string _LicenseKey = "EXK0-W4HZ-N518-IMEW"; |
17 |
|
18 |
public static void LoadMappingExcelFile(string filePath, Dictionary<string, string> mapping) |
19 |
{ |
20 |
try |
21 |
{ |
22 |
SpreadsheetInfo.SetLicense(_LicenseKey); |
23 |
ExcelFile excelFile = ExcelFile.Load(filePath); |
24 |
|
25 |
foreach (ExcelWorksheet sheet in excelFile.Worksheets) |
26 |
{ |
27 |
if (sheet.Name == "Equipment" || |
28 |
sheet.Name == "Equipment Components" || |
29 |
sheet.Name == "Instrumentation" || |
30 |
sheet.Name == "Piping") |
31 |
{ |
32 |
SetMappingSheet(sheet, mapping); |
33 |
} |
34 |
} |
35 |
|
36 |
} |
37 |
catch (Exception ex) |
38 |
{ |
39 |
Log.WriteLine(ex); |
40 |
} |
41 |
} |
42 |
|
43 |
private static void SetMappingSheet(ExcelWorksheet sheet, Dictionary<string, string> mapping) |
44 |
{ |
45 |
for (int i = 1; i < sheet.Rows.Count; i++) |
46 |
{ |
47 |
ExcelRow row = sheet.Rows[i]; |
48 |
object AValue = row.Cells[0].Value; |
49 |
if (AValue != null && !string.IsNullOrEmpty(AValue.ToString())) |
50 |
{ |
51 |
// ID2 Name |
52 |
object mappingCellValue = row.Cells[13].Value; |
53 |
if (mappingCellValue != null && !string.IsNullOrEmpty(mappingCellValue.ToString())) |
54 |
{ |
55 |
string sMappingCellValue = mappingCellValue.ToString(); |
56 |
string symbolPath = GetSymbolPath(row); |
57 |
if (!string.IsNullOrEmpty(symbolPath)) |
58 |
{ |
59 |
if (mapping.ContainsKey(sMappingCellValue)) |
60 |
mapping[sMappingCellValue] = symbolPath; |
61 |
else |
62 |
mapping.Add(sMappingCellValue, symbolPath); |
63 |
} |
64 |
} |
65 |
} |
66 |
} |
67 |
} |
68 |
|
69 |
private static string GetSymbolPath(ExcelRow row) |
70 |
{ |
71 |
StringBuilder sb = new StringBuilder(); |
72 |
|
73 |
for (int i = 1; i < 6; i++) |
74 |
{ |
75 |
object cellValue = row.Cells[i].Value; |
76 |
if (cellValue != null && !string.IsNullOrEmpty(cellValue.ToString())) |
77 |
{ |
78 |
sb.Append(@"\" + cellValue.ToString()); |
79 |
} |
80 |
else |
81 |
break; |
82 |
} |
83 |
|
84 |
object symbolName = row.Cells[8].Value; |
85 |
if (symbolName != null && !string.IsNullOrEmpty(symbolName.ToString())) |
86 |
{ |
87 |
sb.Append(@"\" + symbolName.ToString()); |
88 |
return sb.ToString(); |
89 |
} |
90 |
else |
91 |
{ |
92 |
return ""; |
93 |
} |
94 |
} |
95 |
|
96 |
} |
97 |
} |