using DevExpress.XtraEditors;
|
using NPOI.HSSF.UserModel;
|
using NPOI.SS.UserModel;
|
using System.IO;
|
using Yw.Geometry;
|
|
namespace Yw.WinFrmUI.Phart
|
{
|
public class PumpChartExportHelper
|
{
|
|
/// <summary>
|
/// 导出
|
/// </summary>
|
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 min_flow = qh.MinX;
|
double max_flow = qh.MaxX;
|
double space = (max_flow - min_flow) / (fitPointNum - 1);
|
double Q = min_flow;
|
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("导出成功");
|
}
|
|
/// <summary>
|
/// 导出定义点
|
/// </summary>
|
public static void ExportDefinePoints(List<Yw.Geometry.Point2d> qh, List<Yw.Geometry.Point2d> qe, List<Yw.Geometry.Point2d> 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<Yw.Geometry.Point2d> 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 min_flow = qh.Min()?.X ?? 0;
|
double max_flow = qh.Max()?.X ?? 0;
|
double space = (max_flow - min_flow) / (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("导出成功");
|
}
|
|
/// <summary>
|
/// 转换
|
/// </summary>
|
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");
|
}
|
|
/// <summary>
|
/// 转换
|
/// </summary>
|
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);
|
}
|
|
}
|
}
|