using DevExpress.Utils; using DevExpress.XtraEditors; using DevExpress.XtraGrid.Views.Grid; using NPOI.HSSF.UserModel; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Windows.Forms; //编辑原始的性能曲线 namespace IStation.WinFrmUI.Curve { public partial class ImportCurveByExcelCtrl : DevExpress.XtraEditors.XtraUserControl { public ImportCurveByExcelCtrl() { InitializeComponent(); this.gridView1.OptionsFind.FindNullPrompt = "检索"; this.gridView1.OptionsSelection.MultiSelect = false; this.gridView1.OptionsMenu.EnableColumnMenu = true; this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false; this.gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; this.gridView1.Appearance.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; this.gridView1.FocusRectStyle = DrawFocusRectStyle.RowFullFocus; this.gridView1.OptionsCustomization.AllowFilter = true; this.gridView1.OptionsCustomization.AllowSort = true; this.gridView1.OptionsCustomization.AllowQuickHideColumns = false; this.gridView1.OptionsView.ShowAutoFilterRow = false; this.gridView1.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never; this.gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None; this.gridView1.OptionsView.ShowGroupPanel = false; this.gridView1.OptionsClipboard.AllowCopy = DefaultBoolean.True; this.gridView1.OptionsBehavior.Editable = true; this.gridView1.OptionsBehavior.ReadOnly = false; this.gridView1.OptionsView.EnableAppearanceOddRow = true; this.gridView1.OptionsView.EnableAppearanceEvenRow = true; this.gridView1.Appearance.OddRow.BackColor = Color.White; this.gridView1.Appearance.EvenRow.BackColor = Color.FromArgb(244, 248, 251); int height = 30; this.gridView1.RowHeight = height; this.gridView1.ColumnPanelRowHeight = height; this.gridView1.GroupRowHeight = height; this.gridView1.BestFitColumns(); this.gridView1.OptionsView.ShowIndicator = true; this.gridView1.IndicatorWidth = height; this.gridView1.CustomDrawRowIndicator += (sender, e) => { if (e.Info.IsRowIndicator && e.RowHandle >= 0) { e.Info.DisplayText = (e.RowHandle + 1).ToString().Trim(); e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far; } }; this.gridView1.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.True; this.gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom; this.gridView1.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder; this.imgCmbCurveSourceFrom.Properties.AddEnum(typeof(Model.eCurveSourceFrom), false); this.imgCmbCurveSourceFrom.EditValue = Model.eCurveSourceFrom.现场测试; this.imgCmbQHFitType.Properties.AddEnum(typeof(Model.eCurveFitType), false); this.imgCmbQHFitType.EditValue = _qhFitType; this.imgCmbQEFitType.Properties.AddEnum(typeof(Model.eCurveFitType), false); this.imgCmbQEFitType.EditValue = _qeFitType; this.imgCmbQPFitType.Properties.AddEnum(typeof(Model.eCurveFitType), false); this.imgCmbQPFitType.EditValue = _qpFitType; } #region 页面类 public class CurrentViewModel { public enum eSortType { ID, Q, H, E, P } public CurrentViewModel() { } public CurrentViewModel(int id, double q, double h, double e, double p) { ID = id; Q = q; H = h; E = e; P = p; } public CurrentViewModel(CurrentViewModel rhs) : this(rhs.ID, rhs.Q, rhs.H, rhs.E, rhs.P) { } public int ID { get; set; } public double Q { get; set; } public double H { get; set; } public double E { get; set; } public double P { get; set; } public CurrentViewModel RoundAll(int decimals) { Q = Math.Round(Q, decimals); H = Math.Round(H, decimals); E = Math.Round(E, decimals); P = Math.Round(P, decimals); return this; } public CurrentViewModel RoundAll_Small(int decimal_places, double small = 2.0) { Q = Round_Small(Q, decimal_places, small); H = Round_Small(H, decimal_places, small); E = Round_Small(E, decimal_places, small); P = Round_Small(P, 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 Model.eCurveFitType _qhFitType = Model.eCurveFitType.CubicCurve; Model.eCurveFitType _qeFitType = Model.eCurveFitType.CubicCurve; Model.eCurveFitType _qpFitType = Model.eCurveFitType.CubicCurve; private List _allBindList; private Model.CurveExpress QhCurve = null, QeCurve = null, QpCurve = null; List QhPoints = null, QePoints = null, QpPoints = null; /// /// 初始化数据 /// public void SetBindingData() { _allBindList = new List(); this.bindingSource1.DataSource = _allBindList; this.chartFeatCurveViewCtrl1.Enabled = false; } //选择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() != DialogResult.OK) return; this.btnExcelFilePath.Text = dlg.FileName; AnaSelectFile(dlg.FileName); } //分析文件 private void AnaSelectFile(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) { MessageBox.Show("请将数据放在Sheet1中"); return; } } //标题行 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) { MessageBox.Show("第一行第一列不能空,"); return; } //开始读取的行 int start_line = title_line_index + 1; var cell_0 = row_title.GetCell(0); if (cell_0 == null) { MessageBox.Show("无法读取表头文件"); return; } 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; } _allBindList.Clear(); double q, h, eta, power; NPOI.SS.UserModel.IRow rowtemp = null; NPOI.SS.UserModel.ICell cell; var index = 1; for (line = start_line; line < 1000; line++) { q = h = eta = power = -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_h); 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(col_index_e); 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(col_index_p); 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 = Model.CurveCalcuHelper.CalculateP(q, h, eta); } else { eta = Model.CurveCalcuHelper.CalculateE(q, h, power); } if (eta > 99) { throw new Exception("效率大于100%"); } _allBindList.Add(new CurrentViewModel(index++, q, h, eta, power)); } if (_allBindList.Count < 4) { this.chartFeatCurveViewCtrl1.ClearData(); MessageBox.Show("导入点过少,请手动添加点,点数至少4个点"); return; } loadCurve(); this.bindingSource1.ResetBindings(false); } catch (Exception err) { MessageBox.Show(string.Format("读取Excel出错!错误原因:{0},行号: {1}", err.Message, line), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } } //加载曲线 private bool loadCurve() { if (_allBindList == null || _allBindList.Count < 4) { this.chartFeatCurveViewCtrl1.Enabled = false; this.chartFeatCurveViewCtrl1.ClearData(); return false; } this.chartFeatCurveViewCtrl1.Enabled = true; _qhFitType = (Model.eCurveFitType)this.imgCmbQHFitType.EditValue; _qeFitType = (Model.eCurveFitType)this.imgCmbQEFitType.EditValue; _qpFitType = (Model.eCurveFitType)this.imgCmbQPFitType.EditValue; QhPoints = new List(); QePoints = new List(); QpPoints = new List(); for (int i = 0; i < _allBindList.Count; i++) { var model = _allBindList[i]; if (model.H > 0) QhPoints.Add(new Model.CurvePoint() { X = model.Q, Y = model.H }); if (model.E >= 0) QePoints.Add(new Model.CurvePoint() { X = model.Q, Y = model.E }); if (model.P > 0) QpPoints.Add(new Model.CurvePoint() { X = model.Q, Y = model.P }); } QhCurve = new Model.CurveExpress(QhPoints, _qhFitType, true); QeCurve = new Model.CurveExpress(QePoints, _qeFitType, true); QpCurve = new Model.CurveExpress(QpPoints, _qpFitType, true); this.chartFeatCurveViewCtrl1.SetCurveInfo(QhCurve, QeCurve, QpCurve); this.chartFeatCurveViewCtrl1.UpdateChart(true); this.chartFeatCurveViewCtrl1.SetWorkPointQ_Auto(); return true; } public bool GetGroup(out string curveCode, out Model.eCurveSourceFrom eCurveSourceFrom, out Model.FeatCurvePointGroup featCurvePointGroup, out Model.FeatCurveExpressGroup featCurveExpressGroup) { curveCode = ""; eCurveSourceFrom = Model.eCurveSourceFrom.现场测试; featCurvePointGroup = null; featCurveExpressGroup = null; if (!loadCurve()) return false; curveCode = this.txtCurveCode.Text.Trim(); if (string.IsNullOrEmpty(curveCode)) { XtraMessageBox.Show("请输入曲线名称!"); return false; } eCurveSourceFrom = (Model.eCurveSourceFrom)this.imgCmbCurveSourceFrom.EditValue; featCurvePointGroup = new Model.FeatCurvePointGroup(QhPoints, QePoints, QpPoints); featCurveExpressGroup = new Model.FeatCurveExpressGroup(QhCurve, QeCurve, QpCurve); return true; } private void imgCmbQHFitType_SelectedIndexChanged(object sender, EventArgs e) { loadCurve(); } private void imgCmbQEFitType_SelectedIndexChanged(object sender, EventArgs e) { loadCurve(); } private void imgCmbQPFitType_SelectedIndexChanged(object sender, EventArgs e) { loadCurve(); } } }