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 cubic_spline_qh, Yw.Geometry.CubicSpline2d cubic_spline_qe, Yw.Geometry.CubicSpline2d cubic_spline_qp, int fitPointNum = 15)
{
try
{
if (cubic_spline_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 = cubic_spline_qh.MinX;
double maxQ = cubic_spline_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 = cubic_spline_qh.GetPointY(Q);
row.CreateCell(col).SetCellValue(Trans(H));
col++;
if (cubic_spline_qe != null && Q <= cubic_spline_qe.MaxX && Q >= cubic_spline_qe.MinX)
{
if (Q < 0.1)
E = 0;
else
E = cubic_spline_qe.GetPointY(Q);
row.CreateCell(col).SetCellValue(Trans(E));
col++;
}
if (cubic_spline_qp != null && Q <= cubic_spline_qp.MaxX && Q >= cubic_spline_qp.MinX)
{
P = cubic_spline_qp.GetPointY(Q);
row.CreateCell(col).SetCellValue(Trans(P));
col++;
}
Q = Q + space;
if (Q > cubic_spline_qh.MaxX * 0.9999)
Q = cubic_spline_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 cubic_spline_qh, List cubic_spline_qe, List cubic_spline_qp, int fitPointNum = 15)
{
try
{
if (cubic_spline_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 pt_qh_list = null, pt_qe_list = null, pt_qp_list = null;
pt_qh_list = cubic_spline_qh;
if (pt_qh_list == null || pt_qh_list.Count < 4)
return;
pt_qe_list = cubic_spline_qe;
pt_qp_list = cubic_spline_qp;
//均匀插值点,计算点坐标
double minQ = cubic_spline_qh.Min()?.X ?? 0;
double maxQ = cubic_spline_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 pt_qh_list)
{
Q = pointQH.X;
H = pointQH.Y;
if (pt_qe_list != null && pt_qe_list.Count > 0)
{
var pointQE = pt_qe_list.Find(x => x.X == Q);
if (pointQE != null)
{
E = pointQE.Y;
}
else
{
E = pt_qe_list.GetPointIndexByX(Q);
}
}
if (pt_qp_list != null && pt_qp_list.Count > 0)
{
var pointQP = pt_qp_list.Find(x => x.X == Q);
if (pointQP != null)
{
P = pointQP.Y;
}
else
{
P = cubic_spline_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);
}
}
}