using DevExpress.XtraEditors;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using Yw.Geometry;
namespace Yw.WinFrmUI.Phart
{
public class Perform2dExportHelper
{
///
/// 导出
///
public static void ExportXLS(Yw.Geometry.CubicSpline2d qh, Yw.Geometry.CubicSpline2d qe, Yw.Geometry.CubicSpline2d qp, int fitPointNum = 15)
{
try
{
if (qh == 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 = qh.MinX;
double maxQ = qh.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 = qh.GetPointY(Q);
row.CreateCell(col).SetCellValue(Trans(H));
col++;
if (qe != null && Q <= qe.MaxX && Q >= qe.MinX)
{
if (Q < 0.1)
E = 0;
else
E = qe.GetPointY(Q);
row.CreateCell(col).SetCellValue(Trans(E));
col++;
}
if (qp != null && Q <= qp.MaxX && Q >= qp.MinX)
{
P = qp.GetPointY(Q);
row.CreateCell(col).SetCellValue(Trans(P));
col++;
}
Q = Q + space;
if (Q > qh.MaxX * 0.9999)
Q = qh.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 qh, List qe, List qp, int fitPointNum = 15)
{
try
{
if (qh == 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 qh_pt_list = null, qe_pt_list = null, qp_pt_list = null;
qh_pt_list = qh;
if (qh_pt_list == null || qh_pt_list.Count < 4)
return;
qe_pt_list = qe;
qp_pt_list = qp;
//均匀插值点,计算点坐标
double minQ = qh.Min()?.X ?? 0;
double maxQ = qh.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 qh_pt_list)
{
Q = pointQH.X;
H = pointQH.Y;
if (qe_pt_list != null && qe_pt_list.Count > 0)
{
var pointQE = qe_pt_list.Find(x => x.X == Q);
if (pointQE != null)
{
E = pointQE.Y;
}
else
{
E = qe_pt_list.GetPointIndexByX(Q);
}
}
if (qp_pt_list != null && qp_pt_list.Count > 0)
{
var pointQP = qp_pt_list.Find(x => x.X == Q);
if (pointQP != null)
{
P = pointQP.Y;
}
else
{
P = qp.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);
}
}
}