using DevExpress.XtraEditors;
using NPOI.HSSF.UserModel;
using System;
using System.IO;
namespace IStation.WinFrmUI.Curve
{
public partial class ExportToXLS
{
///
/// 导出
///
public static bool Export(string filePath, Model.FeatCurveExpressGroup curveExpressGroup, int FitPointNumQH = 15)
{
if (curveExpressGroup == null)
return false;
return Export(filePath, curveExpressGroup.CurveQH, curveExpressGroup.CurveQE, curveExpressGroup.CurveQP, FitPointNumQH);
}
///
/// 导出
///
public static bool Export(string filePath, Model.CurveExpress CurveExpressQH, Model.CurveExpress CurveExpressQE, Model.CurveExpress CurveExpressQP, int FitPointNumQH = 15)
{
try
{
int i = 0;
#region 初始化表格
HSSFWorkbook theBook = InitializeBook(filePath);
if (theBook == null)
{
return false;
}
var theSheet1 = theBook.GetSheet("Sheet1");
if (theSheet1 == null)
return false;
#endregion
NPOI.SS.UserModel.IRow rowTile = theSheet1.CreateRow(0);
rowTile.CreateCell(0).SetCellValue("序号");//
rowTile.CreateCell(1).SetCellValue("流量");
rowTile.CreateCell(2).SetCellValue("扬程");
rowTile.CreateCell(3).SetCellValue("效率");
rowTile.CreateCell(4).SetCellValue("功率");
//均匀插值点,计算点坐标
double rQmin = CurveExpressQH.Min;
double rQmax = CurveExpressQH.Max;
double space = (rQmax - rQmin) / (FitPointNumQH - 1);
double Q = rQmin;
double H, E, P;
for (i = 0; i < FitPointNumQH; i++)
{
H = Model.FitCurveHelper.GetFitPointY(CurveExpressQH, Q);
P = Model.FitCurveHelper.GetFitPointY(CurveExpressQP, Q);
var eta = Model.FitCurveHelper.GetFitPointY(CurveExpressQE, Q);
int col = 0;
NPOI.SS.UserModel.IRow rowtemp = theSheet1.CreateRow(i + 1);
rowtemp.CreateCell(col).SetCellValue(i + 1);//序号
col++;
rowtemp.CreateCell(col).SetCellValue(MyTrans(Q));
col++;
rowtemp.CreateCell(col).SetCellValue(MyTrans(H));
col++;
if (CurveExpressQE != null
&& Q <= CurveExpressQE.Max
&& Q >= CurveExpressQE.Min)
{
if (Q < 0.1)
E = 0;
else
E = Model.FitCurveHelper.GetFitPointY(CurveExpressQE, Q);
rowtemp.CreateCell(col).SetCellValue(MyTrans(E));
col++;
}
if (CurveExpressQP != null
&& Q <= CurveExpressQP.Max
&& Q >= CurveExpressQP.Min)
{
P = Model.FitCurveHelper.GetFitPointY(CurveExpressQP, Q);
rowtemp.CreateCell(col).SetCellValue(MyTrans(P));
col++;
}
Q = Q + space;
if (Q > CurveExpressQH.Max * 0.9999)
Q = CurveExpressQH.Max * 0.9999;
}
//Force excel to recalculate all the formula while open
theSheet1.ForceFormulaRecalculation = true;
//打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件!
using (FileStream fs = File.OpenWrite(filePath))
{
theBook.Write(fs); //向打开的这个xls文件中写入mySheet表并保存。
}
}
catch (TimeoutException ex)
{
XtraMessageBox.Show("通讯超时,请重试", "Error:249 " + ex.Message);
return false;
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.ToString());
return false;
}
return true;
}
/// 导出
///
public static bool ExportEasyExcel(string filePath, Model.FeatCurveExpressGroup curveExpressGroup, int FitPointNumQH = 15)
{
if (curveExpressGroup == null)
return false;
return ExportEasyExcel(filePath, curveExpressGroup.CurveQH, curveExpressGroup.CurveQE, curveExpressGroup.CurveQP, FitPointNumQH);
}
/// 导出
///
public static bool ExportEasyExcel(string filePath, Model.CurveExpress CurveExpressQH, Model.CurveExpress CurveExpressQE, Model.CurveExpress CurveExpressQP, int FitPointNumQH = 15)
{
try
{
int i = 0;
#region 初始化表格
HSSFWorkbook theBook = InitializeBook(filePath);
if (theBook == null)
{
return false;
}
var theSheet1 = theBook.GetSheet("Sheet1");
if (theSheet1 == null)
return false;
#endregion
NPOI.SS.UserModel.IRow rowTile = theSheet1.CreateRow(0);
rowTile.CreateCell(0).SetCellValue("流量");
rowTile.CreateCell(1).SetCellValue("扬程");
rowTile.CreateCell(2).SetCellValue("效率");
rowTile.CreateCell(3).SetCellValue("功率");
//均匀插值点,计算点坐标
double rQmin = CurveExpressQH.Min;
double rQmax = CurveExpressQH.Max;
double space = (rQmax - rQmin) / (FitPointNumQH - 1);
double Q = rQmin;
double H, E, P;
for (i = 0; i < FitPointNumQH; i++)
{
H = Model.FitCurveHelper.GetFitPointY(CurveExpressQH, Q);
P = Model.FitCurveHelper.GetFitPointY(CurveExpressQP, Q);
var eta = Model.FitCurveHelper.GetFitPointY(CurveExpressQE, Q);
int col = 0;
NPOI.SS.UserModel.IRow rowtemp = theSheet1.CreateRow(i + 1);
rowtemp.CreateCell(col).SetCellValue(MyTrans(Q));
col++;
rowtemp.CreateCell(col).SetCellValue(MyTrans(H));
col++;
if (CurveExpressQE != null
&& Q <= CurveExpressQE.Max
&& Q >= CurveExpressQE.Min)
{
if (Q < 0.1)
E = 0;
else
E = Model.FitCurveHelper.GetFitPointY(CurveExpressQE, Q);
rowtemp.CreateCell(col).SetCellValue(MyTrans(E));
col++;
}
if (Q <= CurveExpressQP.Max)
{
}
if (Q >= CurveExpressQP.Min)
{
}
if (CurveExpressQP != null
&& Q <= CurveExpressQP.Max
&& Q >= CurveExpressQP.Min)
{
P = Model.FitCurveHelper.GetFitPointY(CurveExpressQP, Q);
rowtemp.CreateCell(col).SetCellValue(MyTrans(P));
col++;
}
//if (P==0)
//{
// var a = 1;
//}
Q = Q + space;
if (Q > CurveExpressQH.Max * 0.9999)
Q = CurveExpressQH.Max * 0.9999;
}
//Force excel to recalculate all the formula while open
theSheet1.ForceFormulaRecalculation = true;
//打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件!
using (FileStream fs = File.OpenWrite(filePath))
{
theBook.Write(fs); //向打开的这个xls文件中写入mySheet表并保存。
}
}
catch (TimeoutException ex)
{
XtraMessageBox.Show("通讯超时,请重试", "Error:249 " + ex.Message);
return false;
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.ToString());
return false;
}
return true;
}
///
/// 导出
///
public static bool ExportDefinePoints(string filePath, Model.FeatCurveExpressGroup curveExpressGroup, int FitPointNumQH = 15)
{
if (curveExpressGroup == null)
return false;
return ExportDefinePoints(filePath, curveExpressGroup.CurveQH, curveExpressGroup.CurveQE, curveExpressGroup.CurveQP, FitPointNumQH);
}
///
/// 导出
///
public static bool ExportDefinePoints(string filePath, Model.CurveExpress CurveExpressQH, Model.CurveExpress CurveExpressQE, Model.CurveExpress CurveExpressQP, int FitPointNumQH = 15)
{
try
{
int i = 0;
#region 初始化表格
HSSFWorkbook theBook = InitializeBook(filePath);
if (theBook == null)
{
return false;
}
var theSheet1 = theBook.GetSheet("Sheet1");
if (theSheet1 == null)
return false;
#endregion
NPOI.SS.UserModel.IRow rowTile = theSheet1.CreateRow(0);
rowTile.CreateCell(0).SetCellValue("流量");
rowTile.CreateCell(1).SetCellValue("扬程");
rowTile.CreateCell(2).SetCellValue("功率");
rowTile.CreateCell(3).SetCellValue("效率");
var qhPoints = CurveExpressQH.DefinePoints;
if (qhPoints == null || qhPoints.Count < 1)
{
qhPoints = CurveExpressQH.GetFitPoints(FitPointNumQH);
}
if (qhPoints == null || qhPoints.Count < 1)
return false;
var qePoints = CurveExpressQE.DefinePoints;
var qpPoints = CurveExpressQP.DefinePoints;
//均匀插值点,计算点坐标
double rQmin = CurveExpressQH.Min;
double rQmax = CurveExpressQH.Max;
double space = (rQmax - rQmin) / (FitPointNumQH - 1);
double Q = 0;
double H, E, P;
foreach (var qhPoint in qhPoints)
{
Q = qhPoint.X;
H = qhPoint.Y;
if (qpPoints != null && qpPoints.Count > 0)
{
var qp = qpPoints.Find(x => x.X == Q);
if (qp != null)
{
P = qp.Y;
}
else
{
P = Model.FitCurveHelper.GetFitPointY(CurveExpressQP, Q);
}
}
else
{
P = Model.FitCurveHelper.GetFitPointY(CurveExpressQP, Q);
}
if (qePoints != null && qePoints.Count > 0)
{
var qe = qePoints.Find(x => x.X == Q);
if (qe != null)
{
E = qe.Y;
}
else
{
E = Model.FitCurveHelper.GetFitPointY(CurveExpressQE, Q);
}
}
else
{
E = Model.FitCurveHelper.GetFitPointY(CurveExpressQE, Q);
}
int col = 0;
i = ++i;
NPOI.SS.UserModel.IRow rowtemp = theSheet1.CreateRow(i);
rowtemp.CreateCell(col).SetCellValue(MyTrans(Q));
col++;
rowtemp.CreateCell(col).SetCellValue(MyTrans(H));
col++;
if (P == 0)
{
if (CurveExpressQP != null && Q <= CurveExpressQP.Max && Q >= CurveExpressQP.Min)
{
P = Model.FitCurveHelper.GetFitPointY(CurveExpressQP, Q);
rowtemp.CreateCell(col).SetCellValue(MyTrans(P));
col++;
}
}
else
{
rowtemp.CreateCell(col).SetCellValue(MyTrans(P));
col++;
}
if (E == 0)
{
if (CurveExpressQE != null && Q <= CurveExpressQE.Max && Q >= CurveExpressQE.Min)
{
if (Q < 0.1)
E = 0;
else
E = Model.FitCurveHelper.GetFitPointY(CurveExpressQE, Q);
rowtemp.CreateCell(col).SetCellValue(MyTrans(E));
col++;
}
}
else
{
rowtemp.CreateCell(col).SetCellValue(MyTrans(E));
col++;
}
}
//Force excel to recalculate all the formula while open
theSheet1.ForceFormulaRecalculation = true;
//打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件!
using (FileStream fs = File.OpenWrite(filePath))
{
theBook.Write(fs); //向打开的这个xls文件中写入mySheet表并保存。
}
}
catch (TimeoutException ex)
{
XtraMessageBox.Show("通讯超时,请重试", "Error:249 " + ex.Message);
return false;
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.ToString());
return false;
}
return true;
}
}
}