duheng
2024-12-27 1c3e5bc50d3045d51cb9a9f747d53442b82d9e2c
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
using DevExpress.XtraEditors;
using NPOI.HSSF.UserModel;
using System.IO;
 
//编辑原始的性能曲线
namespace HStation.WinFrmUI
{
    public partial class ImportValvePerform2dByExcelCtrl : DevExpress.XtraEditors.XtraUserControl
    {
        public ImportValvePerform2dByExcelCtrl()
        {
            InitializeComponent();
 
            this.gridView1.SetDefaultEditView();
            this.gridView1.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
 
            _allBindList = new List<CurrentViewModel>();
            this.bindingSource1.DataSource = _allBindList;
        }
 
        #region CurrentViewModel
 
        public class CurrentViewModel
        {
            public enum eSortType
            {
                ID,
                Q,
                L
            }
 
            public CurrentViewModel()
            { }
 
            public CurrentViewModel(int id, double q, double l)
            {
                ID = id;
                Q = q;
                L = l;
            }
 
            public CurrentViewModel(CurrentViewModel rhs) : this(rhs.ID, rhs.Q, rhs.L)
            {
            }
 
            public int ID { get; set; }
            public double Q { get; set; }
            public double L { get; set; }
 
            public CurrentViewModel RoundAll(int decimals)
            {
                Q = Math.Round(Q, decimals);
                L = Math.Round(L, decimals);
                return this;
            }
 
            public CurrentViewModel RoundAll_Small(int decimal_places, double small = 2.0)
            {
                Q = Round_Small(Q, decimal_places, small);
                L = Round_Small(L, decimal_places, small);
                return this;
            }
 
            private double Round_Small(double value, int num, double small = 2.0)
            {
                if (value < small)
                    value = Math.Round(value, 3);
                else
                    value = Math.Round(value, num);
                return value;
            }
        }
 
        #endregion CurrentViewModel
 
        public void SetBindingData(string x, string y)
        {
            xtrPerform2dChart1.SetAxisTitle(x, y);
        }
 
        private List<CurrentViewModel> _allBindList;
 
        private Yw.Geometry.CubicSpline2d
            _cubic_spline_ql = null;
 
        private List<Yw.Geometry.Point2d>
            _pt_ql_list = null;
 
        //选择Excel文件
        private void btnExcelFilePath_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dlg = new System.Windows.Forms.OpenFileDialog();
            dlg.Filter = "EXCEL 文件(*.xls)|*.xls";
            dlg.CheckFileExists = true;
            if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return;
            this.btnExcelFilePath.Text = dlg.FileName;
            LoadExcel(dlg.FileName);
        }
 
        //分析文件
        private void LoadExcel(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                XtraMessageBox.Show("路径无效!");
                return;
            }
            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)
                {
                    XtraMessageBox.Show("请将数据放在Sheet1中");
                    return;
                }
            }
 
            //标题行
            int title_line_index = 0;
            //流量列
            int col_index_q = 1;
            //水损列
            int col_index_l = 2;
 
            var row_title = sheet1.GetRow(title_line_index);
            if (row_title == null)
            {
                XtraMessageBox.Show("第一行第一列不能空,");
                return;
            }
 
            //开始读取的行
            int start_line = title_line_index + 1;
            var cell_0 = row_title.GetCell(0);
            if (cell_0 == null)
            {
                XtraMessageBox.Show("无法读取表头文件");
                return;
            }
            else if (cell_0.StringCellValue.Contains("序号"))
            {
                col_index_q = 1;
                col_index_l = 2;
            }
            else if (cell_0.StringCellValue.Contains("流量"))
            {
                col_index_q = 0;
                col_index_l = 1;
            }
 
            _allBindList.Clear();
 
            double q, l;
            NPOI.SS.UserModel.IRow rowtemp = null;
            NPOI.SS.UserModel.ICell cell;
 
            var index = 1;
            for (line = start_line; line < 1000; line++)
            {
                q = l = -1;
                rowtemp = sheet1.GetRow(line);
                if (rowtemp == null)
                    break;
 
                cell = rowtemp.GetCell(col_index_q);
                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(col_index_l);
                if (cell == null)
                    break;
                if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
                    l = cell.NumericCellValue;
                else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
                    l = Convert.ToDouble(cell.StringCellValue);
                else if (cell.CellType == NPOI.SS.UserModel.CellType.Blank)
                    break;
                if (l < 0)
                    break;
 
                _allBindList.Add(new CurrentViewModel(index++, q, l));
            }
 
            if (_allBindList.Count < 4)
            {
                this.xtrPerform2dChart1.InitialChartData();
                XtraMessageBox.Show("导入点过少,请手动添加点,点数至少4个点");
                return;
            }
            this.bindingSource1.ResetBindings(false);
            loadChart();
            //}
            //catch (Exception err)
            //{
            //    XtraMessageBox.Show(string.Format("读取Excel出错!错误原因:{0},行号: {1}", err.Message, line), "提示信息",
            //        System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
            //}
        }
 
        //加载曲线
        private bool loadChart()
        {
            if (_allBindList == null || _allBindList.Count < 4)
            {
                this.xtrPerform2dChart1.InitialChartData();
                return false;
            }
 
            _pt_ql_list = new List<Yw.Geometry.Point2d>();
            for (int i = 0; i < _allBindList.Count; i++)
            {
                var model = _allBindList[i];
                if (model.L > 0)
                    _pt_ql_list.Add(new Yw.Geometry.Point2d() { X = model.Q, Y = model.L });
            }
 
            _cubic_spline_ql = new Yw.Geometry.CubicSpline2d(_pt_ql_list);
            this.xtrPerform2dChart1.SetBindingData(_cubic_spline_ql.ToDbString(), string.Empty, true);
            return true;
        }
 
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        public bool Get(out List<Yw.Geometry.Point2d> pt_list)
        {
            pt_list = null;
 
            if (!loadChart())
                return false;
            pt_list = _pt_ql_list;
            return true;
        }
    }
}