1
lixiaojun
2025-02-08 234ed90eaa92f2e02a78dfa164703de7547bf8c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using DevExpress.CodeParser;
using HStation.Assets;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;
 
namespace HStation.WinFrmUI
{
    public class TranslationExcelHelper
    {
        public static string ParseTranslationExcel(string fileName, out List<HStation.Vmo.AssetsTranslationMainVmo> list)
        {
            list = new List<Vmo.AssetsTranslationMainVmo>();
            if (!File.Exists(fileName))
                return "文件不存在";
            int line = 0;
 
            IWorkbook workbook;
            using (FileStream file = new(fileName, FileMode.Open, FileAccess.Read))
            {
                if (Path.GetExtension(fileName).ToLower() == ".xls")
                {
                    workbook = new HSSFWorkbook(file);
                }
                else if (Path.GetExtension(fileName).ToLower() == ".xlsx")
                {
                    workbook = new XSSFWorkbook(file);
                }
                else
                {
                    return "不支持的文件格式";
                }
            }
 
            ISheet sheet_ql = workbook.GetSheet("sheet1");
            if (sheet_ql == null)
            {
                return "表格不符合格式";
            }
            //标题行
            int title_line_index = 0;
            //名称列
            int col_index_name = 0;
            //类型列
            int col_index_type = 1;
            //材料列
            int col_index_materials = 2;
            //系数列
            int col_index_coefficient = 3;
            //上游直径列
            int col_index_start_diameter = 4;
 
            //下游直径列
            int col_index_end_diameter = 5;
 
            //备注列
            int col_index_descriptions = 6;
 
            var row_title = sheet_ql.GetRow(title_line_index);
            if (row_title == null)
            {
                return ("第一行第一列不能空");
            }
 
            //开始读取的行
            int start_line = title_line_index + 1;
            var cell_0 = row_title.GetCell(0);
            if (cell_0 == null)
            {
                return ("无法读取表头文件");
            }
 
            NPOI.SS.UserModel.IRow row_temp = null;
            NPOI.SS.UserModel.ICell cell;
 
            for (line = start_line; line < 1000; line++)
            {
                row_temp = sheet_ql.GetRow(line);
                if (row_temp == null)
                    break;
                var name = row_temp.GetCell(col_index_name);
                if (name == null)
                    break;
                var type = row_temp.GetCell(col_index_type);
                eTranslationType? eTranslationType = null;
                switch (type.StringCellValue)
                {
                    case "渐缩变径":
                        eTranslationType = HStation.Assets.eTranslationType.GradualContraction;
                        break;
 
                    case "偏心渐缩":
                        eTranslationType = HStation.Assets.eTranslationType.EccentricContraction;
                        break;
 
                    case "偏心渐扩":
                        eTranslationType = HStation.Assets.eTranslationType.EccentricExpansion;
                        break;
 
                    case "渐扩变径":
                        eTranslationType = HStation.Assets.eTranslationType.GradualExpansion;
                        break;
                }
 
                var material = row_temp.GetCell(col_index_materials);
                if (material == null)
                    break;
                cell = row_temp.GetCell(col_index_coefficient);
                if (cell == null)
                    break;
                if (!ParseCellValue(cell, out double coefficient))
                    break;
                if (coefficient < 0)
                    break;
                cell = row_temp.GetCell(col_index_start_diameter);
                if (cell == null)
                    break;
                if (!ParseCellValue(cell, out double start_diameter))
                    break;
                if (start_diameter < 0)
                    break;
                cell = row_temp.GetCell(col_index_end_diameter);
                if (cell == null)
                    break;
                if (!ParseCellValue(cell, out double end_diameter))
                    break;
                if (end_diameter < 0)
                    break;
                list.Add(new Vmo.AssetsTranslationMainVmo
                {
                    Name = name.StringCellValue,
                    TranslationType = eTranslationType,
                    Material = material.StringCellValue,
                    MinorLoss = coefficient,
                    StartDiameter = start_diameter,
                    EndDiameter = end_diameter,
                    Roughness = 120,
                    Description = row_temp.GetCell(col_index_descriptions).StringCellValue,//?.StringCellValue,
                });
            }
 
            if (list == null || !list.Any())
            {
                return ("表格不符合格式");
            }
            return "";
        }
 
        private static bool ParseCellValue(NPOI.SS.UserModel.ICell cell, out double cell_value)
        {
            cell_value = 0;
            if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
            {
                cell_value = cell.NumericCellValue;
                return true;
            }
 
            if (cell.CellType == NPOI.SS.UserModel.CellType.String)
            {
                if (double.TryParse(cell.StringCellValue, out double value))
                {
                    cell_value = value;
                    return true;
                }
            }
 
            return false;
        }
 
        //转换可以为空
        private static bool ParseCellValue(NPOI.SS.UserModel.ICell cell, out double? cell_value)
        {
            cell_value = null;
            if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
            {
                cell_value = cell.NumericCellValue;
                return true;
            }
 
            if (cell.CellType == NPOI.SS.UserModel.CellType.String)
            {
                if (double.TryParse(cell.StringCellValue, out double value))
                {
                    cell_value = value;
                    return true;
                }
                else if (cell.StringCellValue == null)
                {
                    return true;
                }
            }
            if (cell.CellType == NPOI.SS.UserModel.CellType.Blank)
            {
                return true;
            }
 
            return false;
        }
 
        private static bool ParseCellValue(NPOI.SS.UserModel.ICell cell, out int cell_value)
        {
            cell_value = 0;
            if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
            {
                cell_value = (int)cell.NumericCellValue;
                return true;
            }
 
            if (cell.CellType == NPOI.SS.UserModel.CellType.String)
            {
                if (int.TryParse(cell.StringCellValue, out int value))
                {
                    cell_value = value;
                    return true;
                }
            }
 
            return false;
        }
 
        public static void ExportTranslationTemplate(string file_path)
        {
            NPOI.HSSF.UserModel.HSSFWorkbook theBook = new NPOI.HSSF.UserModel.HSSFWorkbook();
 
            var theSheet1 = theBook.CreateSheet("Sheet1");
            NPOI.SS.UserModel.IRow rowTile1 = theSheet1.CreateRow(0);
            rowTile1.CreateCell(0).SetCellValue("名称");
            rowTile1.CreateCell(1).SetCellValue("类型");
            rowTile1.CreateCell(2).SetCellValue("材料");
            rowTile1.CreateCell(3).SetCellValue("局阻系数");
            rowTile1.CreateCell(4).SetCellValue("上游直径");
            rowTile1.CreateCell(5).SetCellValue("下游直径");
            rowTile1.CreateCell(6).SetCellValue("备注");
            theSheet1.ForceFormulaRecalculation = true;
 
            using FileStream fs = File.OpenWrite(file_path);
            theBook.Write(fs);
        }
    }
}