Shuxia Ning
2025-02-13 2f1cbec203dcff25df7a5c2b51b13ec558f2c3db
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
using System;
using System.Collections.Generic;
using System.IO;
 
namespace IStation.WinFrmUI.Monitor
{
    /// <summary>
    /// 
    /// </summary>
    public class ParseExcelHelper
    {
       
        //public static string ParseExcel(string fileName, List<(DateTime Time,double Flow,double Head)> list)
        //{
        //    list = new List<(DateTime Time, double Flow, double Head)>();
        //    try
        //    {
        //        if (!File.Exists(fileName))
        //            return "文件不存在";
        //        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)
        //                return ("无Sheet数据");
        //        }
 
        //        //标题行
        //        int title_line_index = 0; 
 
        //        //流量列
        //        int col_index_t = 0;
        //        //流量列
        //        int col_index_q = 1;
        //        //扬程列
        //        int col_index_h = 2;
              
 
        //        var row_title = sheet1.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 = sheet1.GetRow(line);
        //            if (row_temp == null)
        //                break;
 
 
        //            cell = row_temp.GetCell(col_index_t);
        //            if (cell == null)
        //                break;
        //            if (!ParseCellValue(cell, out DateTime time))
        //                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;
                     
 
 
        //            qh.Add(new Model.CurvePoint(flow, head));
        //            qe.Add(new Model.CurvePoint(flow, eff));
        //            qp.Add(new Model.CurvePoint(flow, power));
        //        }
 
        //        return "";
 
        //    }
        //    catch (Exception ex)
        //    {
        //        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 DateTime cell_value)
        {
            cell_value = DateTime.MinValue;
            if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
            {
                cell_value = cell.DateCellValue;
                return true;
            }
 
            if (cell.CellType == NPOI.SS.UserModel.CellType.String)
            {
                if (DateTime.TryParse(cell.StringCellValue, out DateTime value))
                {
                    cell_value = value;
                    return true;
                }
            }
 
            return false;
        }
         
 
    }
}