using DevExpress.XtraEditors; using NPOI.HSSF.UserModel; using System.IO; //编辑原始的性能曲线 namespace HStation.WinFrmUI { public partial class ImportCompressorPerform2dByExcelCtrl : DevExpress.XtraEditors.XtraUserControl { public ImportCompressorPerform2dByExcelCtrl() { InitializeComponent(); this.gridView1.SetDefaultEditView(); this.gridView1.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder; _allBindList = new List(); 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 _allBindList; private Yw.Geometry.CubicSpline2d _cubic_spline_ql = null; private List _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(); 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; } /// /// 获取数据 /// /// public bool Get(out List pt_list) { pt_list = null; if (!loadChart()) return false; pt_list = _pt_ql_list; return true; } } }