using DevExpress.XtraEditors;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using Yw.Geometry;
namespace HStation.WinFrmUI.Phart
{
public class ExportHelper
{
///
/// 导出
///
public static void ExportXLS(Yw.Geometry.CubicSpline2d curveQH, Yw.Geometry.CubicSpline2d curveQE, Yw.Geometry.CubicSpline2d curveQP, int fitPointNum = 15)
{
try
{
if (curveQH == null)
return;
var dlg = new SaveFileDialog();
dlg.Filter = "EXCEL 文件(*.xls)|*.xls";
if (dlg.ShowDialog() != DialogResult.OK)
return;
HSSFWorkbook theBook = new HSSFWorkbook();
var theSheet1 = theBook.CreateSheet("Sheet1");
IRow rowTile = theSheet1.CreateRow(0);
rowTile.CreateCell(0).SetCellValue("流量");
rowTile.CreateCell(1).SetCellValue("扬程");
rowTile.CreateCell(2).SetCellValue("效率");
rowTile.CreateCell(3).SetCellValue("功率");
//均匀插值点,计算点坐标
double minQ = curveQH.MinX;
double maxQ = curveQH.MaxX;
double space = (maxQ - minQ) / (fitPointNum - 1);
double Q = minQ;
double H, E, P;
for (int rowIndex = 1; rowIndex <= fitPointNum; rowIndex++)
{
int col = 0;
IRow row = theSheet1.CreateRow(rowIndex);
row.CreateCell(col).SetCellValue(Trans(Q));
col++;
H = curveQH.GetPointY(Q);
row.CreateCell(col).SetCellValue(Trans(H));
col++;
if (curveQE != null && Q <= curveQE.MaxX && Q >= curveQE.MinX)
{
if (Q < 0.1)
E = 0;
else
E = curveQE.GetPointY(Q);
row.CreateCell(col).SetCellValue(Trans(E));
col++;
}
if (curveQP != null && Q <= curveQP.MaxX && Q >= curveQP.MinX)
{
P = curveQP.GetPointY(Q);
row.CreateCell(col).SetCellValue(Trans(P));
col++;
}
Q = Q + space;
if (Q > curveQH.MaxX * 0.9999)
Q = curveQH.MaxX * 0.9999;
}
//强制Excel在打开时重新计算所有公式
theSheet1.ForceFormulaRecalculation = true;
using (FileStream fs = File.OpenWrite(dlg.FileName))
{
theBook.Write(fs);
}
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.ToString());
}
XtraMessageBox.Show("导出成功");
}
///
/// 导出定义点
///
public static void ExportDefinePoints(List curveQH, List curveQE, List curveQP, int fitPointNum = 15)
{
try
{
if (curveQH == null)
return;
var dlg = new SaveFileDialog();
dlg.Filter = "EXCEL 文件(*.xls)|*.xls";
if (dlg.ShowDialog() != DialogResult.OK)
return;
HSSFWorkbook theBook = new HSSFWorkbook();
var theSheet1 = theBook.CreateSheet("Sheet1");
IRow rowTile = theSheet1.CreateRow(0);
rowTile.CreateCell(0).SetCellValue("流量");
rowTile.CreateCell(1).SetCellValue("扬程");
rowTile.CreateCell(2).SetCellValue("效率");
rowTile.CreateCell(3).SetCellValue("功率");
List pointsQH = null, pointsQE = null, pointsQP = null;
pointsQH = curveQH;
if (pointsQH == null || pointsQH.Count < 4)
return;
pointsQE = curveQE;
pointsQP = curveQP;
//均匀插值点,计算点坐标
double minQ = curveQH.Min()?.X??0;
double maxQ = curveQH.Max()?.X??0;
double space = (maxQ - minQ) / (fitPointNum - 1);
double Q = 0;
double H, E=0, P=0;
int rowIndex = 1;
foreach (var pointQH in pointsQH)
{
Q = pointQH.X;
H = pointQH.Y;
if (pointsQE != null && pointsQE.Count > 0)
{
var pointQE = pointsQE.Find(x => x.X == Q);
if (pointQE != null)
{
E = pointQE.Y;
}
else
{
E = pointsQE.GetPointIndexByX( Q);
}
}
if (pointsQP != null && pointsQP.Count > 0)
{
var pointQP = pointsQP.Find(x => x.X == Q);
if (pointQP != null)
{
P = pointQP.Y;
}
else
{
P = curveQP.GetPointIndexByX(Q);
}
}
var row = theSheet1.CreateRow(rowIndex);
rowIndex++;
int col = 0;
row.CreateCell(col).SetCellValue(Trans(Q));
col++;
row.CreateCell(col).SetCellValue(Trans(H));
col++;
row.CreateCell(col).SetCellValue(Trans(E));
col++;
row.CreateCell(col).SetCellValue(Trans(P));
col++;
}
//强制Excel在打开时重新计算所有公式
theSheet1.ForceFormulaRecalculation = true;
using (FileStream fs = File.OpenWrite(dlg.FileName))
{
theBook.Write(fs);
}
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.ToString());
return;
}
XtraMessageBox.Show("导出成功");
}
///
/// 转换
///
private static string DoubleTrans(double doubleValue)
{
if (doubleValue < 1)
return doubleValue.ToString("0.0000");
else if (doubleValue < 10)
return doubleValue.ToString("0.000");
else if (doubleValue < 100)
return doubleValue.ToString("0.00");
else
return doubleValue.ToString("0.0");
}
///
/// 转换
///
private static double Trans(double doubleValue)
{
if (doubleValue < 1)
return Math.Round(doubleValue, 4);
else if (doubleValue < 10)
return Math.Round(doubleValue, 3);
else if (doubleValue < 100)
return Math.Round(doubleValue, 2);
else
return Math.Round(doubleValue, 1);
}
}
}