Shuxia Ning
2024-08-12 d2cee56db11f0ee475e9f9dbdc8bfd03ad982e18
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
 
namespace AStation.WinFrmUI.Chart
{
    /// <summary>
    /// ExcelFileHelper
    /// </summary>
    public class CurveExpressFileHelper
    {
        public static List<AStation.Curve.GroupPoint> ImportByExcel(string fileName, out string msg)
        {
            msg = string.Empty;
            if (!File.Exists(fileName))
            {
                msg = "文件不存在!";
                return default;
            }
 
            var list = new List<AStation.Curve.GroupPoint>();
            int line = 0;
            try
            {
                //初始化文件 
                HSSFWorkbook theBook = null;
                using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    theBook = new HSSFWorkbook(file);
                }
 
                //检查表格是否符合
                NPOI.SS.UserModel.ISheet sheet1 = theBook.GetSheet("Sheet1");
                if (sheet1 == null)
                {
                    sheet1 = theBook.GetSheetAt(0);
                    if (sheet1 == null)
                    {
                        msg = "请将数据放在Sheet1中!";
                        return default;
                    }
                }
 
                //标题行
                int titleLineIndex = 0;
                //流量列
                int colIndexQ = 1;
                //扬程列
                int colIndexH = 2;
                //效率列
                int colIndexE = 3;
                //功率列
                int colIndexP = 4;
 
                var rowTitle = sheet1.GetRow(titleLineIndex);
                if (rowTitle == null)
                {
                    msg = "第一行第一列不能空!";
                    return default;
                }
 
                //开始读取的行  
                int startLine = titleLineIndex + 1;
                var cell_0 = rowTitle.GetCell(0);
                if (cell_0 == null)
                {
                    msg = "无法读取表头文件";
                    return default;
                }
                else if (cell_0.StringCellValue.Contains("序号"))
                {
                    colIndexQ = 1;
                    colIndexH = 2;
                    colIndexE = 3;
                    colIndexP = 4;
                }
                else if (cell_0.StringCellValue.Contains("流量"))
                {
                    colIndexQ = 0;
                    colIndexH = 1;
                    colIndexE = 2;
                    colIndexP = 3;
                }
 
                double q, h, eta, power;
                NPOI.SS.UserModel.IRow rowTemp = null;
                NPOI.SS.UserModel.ICell cell;
 
                var index = 1;
                for (line = startLine; line < 1000; line++)
                {
                    q = h = eta = power = -1;
                    rowTemp = sheet1.GetRow(line);
                    if (rowTemp == null)
                        break;
 
                    cell = rowTemp.GetCell(colIndexQ);
                    if (cell == null)
                        break;
                    if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
                        q = cell.NumericCellValue;
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
                        q = Convert.ToDouble(cell.StringCellValue);
                    else
                        continue;
 
                    if (q < 0)
                        break;
 
                    cell = rowTemp.GetCell(colIndexH);
                    if (cell == null)
                        break;
                    if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
                        h = cell.NumericCellValue;
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
                        h = Convert.ToDouble(cell.StringCellValue);
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.Blank)
                        break;
                    if (h < 0)
                        break;
 
 
                    cell = rowTemp.GetCell(colIndexE);
                    if (cell == null)
                        break;
                    if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
                        eta = cell.NumericCellValue;
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
                        eta = Convert.ToDouble(cell.StringCellValue);
                    else
                        eta = -1;
 
                    cell = rowTemp.GetCell(colIndexP);
                    if (cell == null)
                        break;
                    if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
                        power = cell.NumericCellValue;
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
                        power = Convert.ToDouble(cell.StringCellValue);
                    else
                        power = -1;
 
                    if (eta > 0)
                    {
                        power = AStation.Curve.PumpCalculateHelper.CalculateP(q, h, eta);
                    }
                    else
                    {
                        eta = AStation.Curve.PumpCalculateHelper.CalculateE(q, h, power);
                    }
 
                    if (eta > 99)
                    {
                        throw new Exception("效率大于100%");
                    }
 
                    list.Add(new AStation.Curve.GroupPoint(index++, q, h, eta, power));
                }
 
                if (list.Count < 4)
                {
                    msg = "导入点过少,请手动添加点,点数至少4个点";
                    return default;
                }
            }
            catch (Exception err)
            {
                msg = string.Format("读取Excel出错!错误原因:{0},行号: {1}", err.Message, line);
            }
            return list;
        }
 
        public static Model.PumpCurveInfo ImportByCurveExpress(string fileName, out string msg)
        {
            msg = string.Empty;
            if (!File.Exists(fileName))
            {
                msg = "文件不存在!";
                return default;
            }
            var strInfo = File.ReadAllText(fileName);
            if (string.IsNullOrEmpty(strInfo))
            {
                msg = "无数据!";
                return default;
            }
 
            var express = Model.PumpCurveInfo.ToModel(strInfo);
            if (express == null)
            {
                msg = "解析失败!";
                return default;
            }
            return express;
        }
 
 
        public static bool ExportCurveExpress(string fileName, Model.PumpCurveInfo featCurveExpressGroup, out string msg)
        {
            msg = string.Empty;
            if (featCurveExpressGroup == null)
            {
                msg = "表达式为空!";
                return default;
            }
            try
            {
                var str = featCurveExpressGroup.ToJson();
                File.WriteAllText(fileName, str);
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return true;
        }
 
 
 
        public static bool Export2Excel(string fileName, Model.PumpCurveInfo featCurveExpressGroup, out string msg, int fitPointNumQH = 12)
        {
            msg = string.Empty;
            if (featCurveExpressGroup == null)
            {
                msg = "曲线表达式为空!";
                return default;
            }
            return Export2Excel(fileName, featCurveExpressGroup.CurveQH, featCurveExpressGroup.CurveQE, featCurveExpressGroup.CurveQP, out msg, fitPointNumQH);
        }
        public static bool Export2Excel(string fileName, AStation.Curve.CurveExpress curveExpressQH, AStation.Curve.CurveExpress curveExpressQE, AStation.Curve.CurveExpress curveExpressQP, out string msg, int fitPointNumQH = 12)
        {
            msg = string.Empty;
            if (curveExpressQH == null)
            {
                msg = "流量扬程曲线为空!";
                return default;
            }
            if (!File.Exists(fileName))
            {
                File.Create(fileName);
            }
 
            try
            {
                HSSFWorkbook theBook = null;
                using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    theBook = new HSSFWorkbook(file);
                    if (theBook == null)
                    {
                        msg = "Excel初始化失败!";
                        return default;
                    }
                    var dsi = NPOI.HPSF.PropertySetFactory.CreateDocumentSummaryInformation();
                    dsi.Company = "EvenTech";
                    theBook.DocumentSummaryInformation = dsi;
                    var si = NPOI.HPSF.PropertySetFactory.CreateSummaryInformation();
                    si.Subject = "yiwei";
                    theBook.SummaryInformation = si;
                }
 
                ISheet theSheet1 = theBook.CreateSheet("Sheet1");
                theSheet1.ForceFormulaRecalculation = true;
 
                IRow rowTile = theSheet1.CreateRow(0);
                rowTile.CreateCell(0).SetCellValue("序号");
                rowTile.CreateCell(1).SetCellValue("流量");
                rowTile.CreateCell(2).SetCellValue("扬程");
                rowTile.CreateCell(3).SetCellValue("效率");
                rowTile.CreateCell(4).SetCellValue("功率");
 
 
                //均匀插值点,计算点坐标
                double rQmin = curveExpressQH.Min;
                double rQmax = curveExpressQH.Max;
                double space = (rQmax - rQmin) / (fitPointNumQH - 1);
                double Q = rQmin;
                double H, E, P;
                for (int i = 0; i < fitPointNumQH; i++)
                {
                    H = AStation.Curve.FitHelper.GetFitPointY(curveExpressQH, Q);
                    P = AStation.Curve.FitHelper.GetFitPointY(curveExpressQP, Q);
                    var eta = AStation.Curve.FitHelper.GetFitPointY(curveExpressQE, Q);
 
                    int col = 0;
                    IRow rowtemp = theSheet1.CreateRow(i + 1);
                    rowtemp.CreateCell(col).SetCellValue(i + 1);//序号
                    col++;
                    rowtemp.CreateCell(col).SetCellValue(DoubleTrans(Q));
                    col++;
                    rowtemp.CreateCell(col).SetCellValue(DoubleTrans(H));
                    col++;
 
                    if (curveExpressQE != null && Q <= curveExpressQE.Max && Q >= curveExpressQE.Min)
                    {
                        if (Q < 0.1)
                            E = 0;
                        else
                            E = AStation.Curve.FitHelper.GetFitPointY(curveExpressQE, Q);
                        rowtemp.CreateCell(col).SetCellValue(DoubleTrans(E));
                        col++;
                    }
 
                    if (curveExpressQP != null && Q <= curveExpressQP.Max && Q >= curveExpressQP.Min)
                    {
                        P = AStation.Curve.FitHelper.GetFitPointY(curveExpressQP, Q);
                        rowtemp.CreateCell(col).SetCellValue(DoubleTrans(P));
                        col++;
                    }
 
                    Q = Q + space;
                    if (Q > curveExpressQH.Max * 0.9999)
                        Q = curveExpressQH.Max * 0.9999;
                }
                using (FileStream fs = File.OpenWrite(fileName))
                {
                    theBook.Write(fs);
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return true;
        }
 
 
        private static string DoubleTrans(double doubleValue)
        {
            if (doubleValue < 1)
                return doubleValue.ToString("0.0000");
            else if (doubleValue < 10)
                return doubleValue.ToString("0.000");
            else if (doubleValue < 100)
                return doubleValue.ToString("0.00");
            else
                return doubleValue.ToString("0.0");
        }
 
    }
}