lixiaojun
2025-02-17 772e6760f2d25a771fe314135d53d1e72373aab7
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
using System.IO;
using Yw.WinFrmUI.Phart;
 
namespace HStation.WinFrmUI.Assets
{
    public class ExcelConvertHelper
    {
        public static Yw.Vmo.PhartDiagramExGraphListVmo ParsePumpExcel(string fileName)
        {
            var qh = new List<Yw.Geometry.Point2d>();
            var qe = new List<Yw.Geometry.Point2d>();
            var qp = new List<Yw.Geometry.Point2d>();
            try
            {
                if (!File.Exists(fileName))
                    throw new Exception("文件不存在");
                int line = 0;
 
                //初始化文件
                NPOI.HSSF.UserModel.HSSFWorkbook theBook = null;
                using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
                {
                    theBook = new NPOI.HSSF.UserModel.HSSFWorkbook(file);
                }
 
                //检查表格是否符合
                NPOI.SS.UserModel.ISheet sheet1 = theBook.GetSheet("Sheet1");
                if (sheet1 == null)
                {
                    sheet1 = theBook.GetSheetAt(0);
                    if (sheet1 == null)
                        throw new Exception("无Sheet数据");
                }
 
                //标题行
                int title_line_index = 0;
                //流量列
                int col_index_q = 1;
                //扬程列
                int col_index_h = 2;
                //效率列
                int col_index_e = 3;
                //功率列
                int col_index_p = 4;
 
                var row_title = sheet1.GetRow(title_line_index);
                if (row_title == null)
                {
                    throw new Exception("第一行第一列不能空");
                }
 
                //开始读取的行
                int start_line = title_line_index + 1;
                var cell_0 = row_title.GetCell(0);
                if (cell_0 == null)
                {
                    throw new Exception("无法读取表头文件");
                }
                else if (cell_0.StringCellValue.Contains("序号"))
                {
                    col_index_q = 1;
                    col_index_h = 2;
                    col_index_e = 3;
                    col_index_p = 4;
                }
                else if (cell_0.StringCellValue.Contains("流量"))
                {
                    col_index_q = 0;
                    col_index_h = 1;
                    col_index_e = 2;
                    col_index_p = 3;
                }
 
                NPOI.SS.UserModel.IRow row_temp = null;
                NPOI.SS.UserModel.ICell cell;
 
                var list = new List<(double Q, double H, double P, double E)>();
                for (line = start_line; line < 13; line++)
                {
                    row_temp = sheet1.GetRow(line);
                    if (row_temp == null)
                        break;
 
                    cell = row_temp.GetCell(col_index_q);
                    if (cell == null)
                        break;
                    if (!ParseCellValue(cell, out double flow))
                        break;
                    if (flow < 0)
                        break;
 
                    cell = row_temp.GetCell(col_index_h);
                    if (cell == null)
                        break;
                    if (!ParseCellValue(cell, out double head))
                        break;
                    if (head < 0)
                        break;
 
                    cell = row_temp.GetCell(col_index_e);
                    if (cell == null)
                        break;
                    if (!ParseCellValue(cell, out double eff))
                    {
                        eff = -1;
                    }
 
                    cell = row_temp.GetCell(col_index_p);
                    if (cell == null)
                        break;
                    if (!ParseCellValue(cell, out double power))
                    {
                        power = -1;
                    }
 
                    if (eff > 0)
                    {
                        power = Yw.Pump.CalculationHelper.CalcuP(flow, head, eff);
                    }
                    else
                    {
                        eff = Yw.Pump.CalculationHelper.CalcuE(flow, head, power);
                    }
 
                    if (eff > 99)
                    {
                        throw new Exception("效率大于100%");
                    }
 
                    qh.Add(new Yw.Geometry.Point2d(flow, head));
                    qe.Add(new Yw.Geometry.Point2d(flow, eff));
                    qp.Add(new Yw.Geometry.Point2d(flow, power));
                }
 
                if (qh == null || qh.Count < 4)
                {
                    return null;
                }
 
                var feat_type = Yw.Ahart.eFeatType.Cubic;
 
                var vmo = new Yw.Vmo.PhartDiagramExGraphListVmo();
                vmo.DiagramType = (int)HStation.PhartRelation.eDiagramType.Feat;
                vmo.GraphList = new List<Yw.Vmo.PhartGraphVmo>();
 
                var graph_qh = new Yw.Vmo.PhartGraphVmo()
                {
                    Name = "扬程线",
                    GraphType = (int)Yw.Ahart.eCurveType.QH,
                    GraphParas = new QHGraphParasViewModel()
                    {
                        Hz = 50,
                    }.ToJson(),
                    GeometryParas = new Yw.WinFrmUI.Phart.CurveGeometryParasViewModel
                    {
                        DefinePoints = qh
                    }.ToJson(),
                    GeometryStyle = (int)HStation.PhartRelation.eGeometryStyle.FeatCurve,
                    GeometryInfo = qh.ToDbString(Yw.Ahart.eCurveType.QH, feat_type)
                };
                vmo.GraphList.Add(graph_qh);
 
                if (qe != null && qe.Count > 3)
                {
                    var graph_qe = new Yw.Vmo.PhartGraphVmo()
                    {
                        Name = "效率线",
                        GraphType = (int)Yw.Ahart.eCurveType.QE,
                        GraphParas = new QEGraphParasViewModel()
                        {
                            Hz = 50,
                        }.ToJson(),
                        GeometryParas = new Yw.WinFrmUI.Phart.CurveGeometryParasViewModel
                        {
                            DefinePoints = qe
                        }.ToJson(),
                        GeometryStyle = (int)HStation.PhartRelation.eGeometryStyle.FeatCurve,
                        GeometryInfo = qe.ToDbString(Yw.Ahart.eCurveType.QE, feat_type)
                    };
 
                    vmo.GraphList.Add(graph_qe);
                }
 
                if (qp != null && qp.Count > 3)
                {
                    var graph_qp = new Yw.Vmo.PhartGraphVmo()
                    {
                        Name = "功率线",
                        GraphType = (int)Yw.Ahart.eCurveType.QP,
                        GraphParas = new QPGraphParasViewModel()
                        {
                            Hz = 50,
                        }.ToJson(),
                        GeometryParas = new Yw.WinFrmUI.Phart.CurveGeometryParasViewModel
                        {
                            DefinePoints = qp
                        }.ToJson(),
                        GeometryStyle = (int)HStation.PhartRelation.eGeometryStyle.FeatCurve,
                        GeometryInfo = qp.ToDbString(Yw.Ahart.eCurveType.QP, feat_type)
                    };
 
                    vmo.GraphList.Add(graph_qp);
                }
 
                return vmo;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
 
        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;
        }
    }
}