using DevExpress.XtraEditors;
|
using NPOI.HSSF.UserModel;
|
using System.IO;
|
|
namespace Yw.WinFrmUI.Phart
|
{
|
public partial class PumpImportByExcelCtrl : DevExpress.XtraEditors.XtraUserControl
|
{
|
public PumpImportByExcelCtrl()
|
{
|
InitializeComponent();
|
|
this.gridView1.SetDefaultEditView();
|
this.gridView1.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
|
|
_all_bind_list = new List<CurrentViewModel>();
|
this.bindingSource1.DataSource = _all_bind_list;
|
}
|
|
#region CurrentViewModel
|
|
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 CurrentViewModel
|
|
private List<CurrentViewModel> _all_bind_list;
|
|
private Yw.Geometry.CubicSpline2d
|
_qh = null,
|
_qe = null,
|
_qp = null;
|
|
private List<Yw.Geometry.Point2d>
|
_qh_pt_list = null,
|
_qe_pt_list = null,
|
_qp_pt_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_h = 2;
|
//效率列
|
int col_index_e = 3;
|
//功率列
|
int col_index_p = 4;
|
|
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_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;
|
}
|
|
_all_bind_list.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 = Yw.Pump.CalculationHelper.CalcuP(q, h, eta);
|
}
|
else
|
{
|
eta = Yw.Pump.CalculationHelper.CalcuE(q, h, power);
|
}
|
|
if (eta > 99)
|
{
|
throw new Exception("效率大于100%");
|
}
|
_all_bind_list.Add(new CurrentViewModel(index++, q, h, eta, power));
|
}
|
|
if (_all_bind_list.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 (_all_bind_list == null || _all_bind_list.Count < 4)
|
{
|
this.xtrPerform2dChart1.InitialChartData();
|
return false;
|
}
|
|
_qh_pt_list = new List<Yw.Geometry.Point2d>();
|
_qe_pt_list = new List<Yw.Geometry.Point2d>();
|
_qp_pt_list = new List<Yw.Geometry.Point2d>();
|
for (int i = 0; i < _all_bind_list.Count; i++)
|
{
|
var model = _all_bind_list[i];
|
if (model.H > 0)
|
_qh_pt_list.Add(new Yw.Geometry.Point2d() { X = model.Q, Y = model.H });
|
if (model.E >= 0)
|
_qe_pt_list.Add(new Yw.Geometry.Point2d() { X = model.Q, Y = model.E });
|
|
if (model.P > 0)
|
_qp_pt_list.Add(new Yw.Geometry.Point2d() { X = model.Q, Y = model.P });
|
}
|
|
_qh = new Yw.Geometry.CubicSpline2d(_qh_pt_list);
|
_qe = new Yw.Geometry.CubicSpline2d(_qe_pt_list);
|
_qp = new Yw.Geometry.CubicSpline2d(_qp_pt_list);
|
|
var test_pt_list = _qe.GetPointList();
|
this.xtrPerform2dChart1.SetBindingData(_qh, _qe, _qp, string.Empty, true);
|
return true;
|
}
|
|
/// <summary>
|
/// 获取数据
|
/// </summary>
|
/// <returns></returns>
|
public bool Get(out string other_name, out Yw.Pump.CurveQH qh, out Yw.Pump.CurveQE qe, out Yw.Pump.CurveQP qp)
|
{
|
other_name = this.txtCurveCode.Text.Trim();
|
qh = null;
|
qe = null;
|
qp = null;
|
|
if (!loadChart())
|
return false;
|
|
if (string.IsNullOrEmpty(other_name))
|
{
|
XtraMessageBox.Show("请输入曲线名称!");
|
return false;
|
}
|
qh = new Pump.CurveQH(Yw.Pump.eFeatType.Cubic, _qh_pt_list);
|
qe = new Pump.CurveQE(Yw.Pump.eFeatType.Cubic, _qe_pt_list);
|
qp = new Pump.CurveQP(Yw.Pump.eFeatType.Cubic, _qp_pt_list);
|
|
return true;
|
}
|
}
|
}
|