using NPOI.HSSF.UserModel; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace TProduct.DataFile.PumpReport { /// /// 力夫 /// public class TestReportFileLIFU1 : TestReportFileExcelBase { public TestReportFileLIFU1() { } protected string _strExcelFile = null; double max_flow_real_ls = 0;//最大流量(实际)0扬程时,单位L/S double max_head_real = 0;//最大扬程(实际) double rated_max_flow_ls = 0;//0扬程对应流量(额定),单位L/S double rated_max_head = 0;//最大扬程(额定) double max_currentI_real = 0;//最大电流 double max_power_real = 0;//最大功率 public override string Create(string strFilePath, TProduct.Model.DispStautInfo infoDelegete) { //初始化文件 var message = InitialFile(strFilePath); if (!string.IsNullOrEmpty(message)) { infoDelegete("泵数据表V1 生成失败,Error:" + message, 1); return message; } this._strExcelFile = strFilePath; try { using (FileStream fileStream = new FileStream(_strExcelFile, FileMode.Open, FileAccess.ReadWrite)) { _theBookHander = new HSSFWorkbook(fileStream); } if (_theBookHander == null) { infoDelegete("泵数据表V1 生成失败,Error:未能打开模版Excel文件", 1); return "未能打开模版Excel文件"; } WritePage封面(infoDelegete); InitialMaxDataInfo(); WritePage试验报告(infoDelegete);//放在检测报告汇总前面 WritePage试验报告判定(infoDelegete); WritePage试验性能曲线(infoDelegete); WritePage检测报告汇总(infoDelegete);//放在试验报告之后 using (var fileStream = new FileStream(_strExcelFile, FileMode.Create)) { _theBookHander.Write(fileStream); } } catch (Exception ex) { infoDelegete("泵数据表V1 生成失败,Error:" + ex.Message, 1); return ex.Message; } finally { //workSheet.Save(); //workSheet.Close(); //workSheet = null; } return null; } public override string GetTemplateFilePath() { if (this._testItems.Count > 1) { return System.IO.Path.Combine( TProduct.DataFolderParas.FullPath, "PumpFeatReportTemplate", "P_CN_LIFU_NPSH_V1.xls"); } return System.IO.Path.Combine( TProduct.DataFolderParas.FullPath, "PumpFeatReportTemplate", "P_CN_LIFU_V1.xls"); } private bool _isJudgeResultQ_OK = false; private bool _isJudgeResultH_OK = false; private bool _isJudgeResultQH_OK = false; private void InitialMaxDataInfo() { if (_ratedParas == null) _ratedParas = TProduct.Model.RatedParas4Pump.ToModel(this._currentPump.RatedParas); if (this._featTestItem.ItemParas != null && this._featTestItem.ItemParas.ExtendPointH != null) { max_flow_real_ls = Math.Round(Eventech.Common.UnitQHelper.fromM3H( Eventech.Model.UnitQ.LMIN, this._featTestItem.ItemParas.ExtendPointH.Value), 2); } if (_ratedParas.MaxQ != null && _ratedParas.MaxQ > 0) { rated_max_flow_ls = Math.Round(Eventech.Common.UnitQHelper.fromM3H( Eventech.Model.UnitQ.LMIN, _ratedParas.MaxQ.Value), 2); } if (_ratedParas.MaxH != null && _ratedParas.MaxH > 0) { rated_max_head = _ratedParas.MaxH.Value; } var mp_电流 = this._allMonitorPoints.Find(x => x.MonitorType == Model.eMonitorType.相电流); var mp_功率 = this._allMonitorPoints.Find(x => x.MonitorType == Model.eMonitorType.功率); foreach (var record in this._allRecords) { if (record.MonitorRecordList == null) continue; if (mp_电流 != null) { var v_电流 = record.MonitorRecordList.Find(x => x.ID == mp_电流.ID); if (v_电流 != null && !string.IsNullOrEmpty(v_电流.Value)) { max_currentI_real = Math.Max(max_currentI_real, v_电流.GetDoubleValue()); } } if (mp_功率 != null) { var v_功率 = record.MonitorRecordList.Find(x => x.ID == mp_功率.ID); if (v_功率 != null && !string.IsNullOrEmpty(v_功率.Value)) { max_power_real = Math.Max(max_power_real, v_功率.GetDoubleValue()); } } max_head_real = Math.Max(max_head_real, record.CorrectPtH); } if (this._testJudgeItems != null) { _isJudgeResultQ_OK = false; var flow_min = _testJudgeItems.Find(x => x.TargeType == Model.eTestJudgeTargetType.流量 && x.ThresholdType == Model.TestGradeJudgeItem.eThresholdType.Min); if (max_flow_real_ls >= rated_max_flow_ls * flow_min.ThresholdValue) { _isJudgeResultQ_OK = true; } var head_min = _testJudgeItems.Find(x => x.TargeType == Model.eTestJudgeTargetType.扬程 && x.ThresholdType == Model.TestGradeJudgeItem.eThresholdType.Min); if (max_head_real >= rated_max_head * head_min.ThresholdValue) { _isJudgeResultH_OK = true; } if (_isJudgeResultQ_OK && _isJudgeResultH_OK) _isJudgeResultQH_OK = true; else _isJudgeResultQH_OK = false; } else { if (max_flow_real_ls >= rated_max_flow_ls) _isJudgeResultQ_OK = true; else _isJudgeResultQ_OK = false; if (max_head_real >= rated_max_head) _isJudgeResultH_OK = true; else _isJudgeResultH_OK = false; if (_isJudgeResultQ_OK && _isJudgeResultH_OK) _isJudgeResultQH_OK = true; else _isJudgeResultQH_OK = false; } //this._testUserName = new BLL.LoginUser().GetRealNameByID(this._featTestItem.LastTestUserID); } //初始化文件 private string InitialFile(string strFilePath) { string strTemplateFile = GetTemplateFilePath(); if (!File.Exists(strTemplateFile)) { return strTemplateFile + ": 模板文件被非法删除"; } if (File.Exists(strFilePath)) { File.Delete(strFilePath); } File.Copy(strTemplateFile, strFilePath, true); return null; } private bool WritePage封面(TProduct.Model.DispStautInfo infoDelegete) { NPOI.SS.UserModel.ISheet sheet封面 = _theBookHander.GetSheetAt(0); if (sheet封面 == null) { infoDelegete(" 生成失败,Error:未发现模版封面页", 1); return false; } SetCellValue(sheet封面, 11, "F", this._currentPumpCatalog.Name); //型号 SetCellValue(sheet封面, 12, "F", this._currentPump.Name); var testParas = this._featTestItem.ProjectParas; if (testParas != null) { if (testParas.TestNature == Model.eTestProjectNature.例行测试) { SetCellValue(sheet封面, 13, "F", "样件□ 小批量□ 年度例行■"); } if (testParas.TestNature == Model.eTestProjectNature.小批量) { SetCellValue(sheet封面, 13, "F", "样件□ 小批量■ 年度例行□"); } if (testParas.TestNature == Model.eTestProjectNature.样件) { SetCellValue(sheet封面, 13, "F", "样件■ 小批量□ 年度例行□"); } } //检测时间 SetCellValue(sheet封面, 14, "F", this._featTestItem.ItemCreateTime.ToString("yyyy-MM-dd")); //检 测 人 SetCellValue(sheet封面, 15, "F", new BLL.LoginUser().GetRealNameByID(this._featTestItem.LastTestUserID)); //SetCellValue(sheet封面, 11, "B", this._currentPart.Name); ////产品编号 //SetCellValue(sheet封面, 13, "B", this._currentPart.Code); ////测试编号 //SetCellValue(sheet封面, 14, "B", this._testProject.Code); ////生产单位/受检单位 //var manufacture_name = new BLL.ManufacturerBase().GetNameByID(this._currentPart.ManufacturerID); //SetCellValue(sheet封面, 16, "B", manufacture_name); ////送检单位/委托单位 //var sender_name = new BLL.Senderbase().GetNameByID(this._currentPart.SenderID); //SetCellValue(sheet封面, 17, "B", sender_name); sheet封面.ForceFormulaRecalculation = true; return true; } private bool WritePage试验报告(TProduct.Model.DispStautInfo infoDelegete) { NPOI.SS.UserModel.ISheet sheet试验报告 = _theBookHander.GetSheetAt(2); if (sheet试验报告 == null) { infoDelegete(" 生成失败,Error:未发现模版基本信息页", 1); return false; } WriteRecordData(sheet试验报告);//放在WriteRecordTitle 前 要计算最大电流 WriteRecordTitle(sheet试验报告); //检 测 人 SetCellValue(sheet试验报告, 29, "D", this._testUserName); return true; } private bool WritePage试验报告判定(TProduct.Model.DispStautInfo infoDelegete) { NPOI.SS.UserModel.ISheet sheet试验报告判定 = _theBookHander.GetSheetAt(3); if (sheet试验报告判定 == null) { infoDelegete(" 生成失败,Error:未发现模版基本信息页", 1); return false; } WriteRecordData(sheet试验报告判定); WriteRecordTitle(sheet试验报告判定); SetCellValue(sheet试验报告判定, 26, "D", BuildJudgeInfo()); //检 测 人 SetCellValue(sheet试验报告判定, 29, "D", this._testUserName); return true; } private bool WritePage试验性能曲线(TProduct.Model.DispStautInfo infoDelegete) { NPOI.SS.UserModel.ISheet sheet试验性能曲线 = _theBookHander.GetSheetAt(4); if (sheet试验性能曲线 == null) { infoDelegete(" 生成失败,Error:未发现模版基本信息页", 1); return false; } WriteRecordTitle(sheet试验性能曲线); SetCellValue(sheet试验性能曲线, 26, "D", BuildJudgeInfo()); if (_featChartImage != null) { _featChartImage.SetDockType(PumpGraph.Picture.FeatChart.eDockType.测试报告, 769, 850); var image1 = _featChartImage.CreateImage(); //System.Drawing.Image img = SPump.DataFile.BASE.NPIOExcelHelper.CreateNewLargeImage(image1, width, height); AddImage1(sheet试验性能曲线, 7, "D", 24, "P", image1);//添加图片 } //检 测 人 SetCellValue(sheet试验性能曲线, 29, "D", this._testUserName); return true; } private bool WriteRecordTitle(NPOI.SS.UserModel.ISheet sheet) { //型号 SetCellValue(sheet, 3, "C", this._currentPump.Name); //产品编号 SetCellValue(sheet, 3, "I", this._currentPart.Code); //报告编号 SetCellValue(sheet, 3, "N", this._testProject.ReportFileNO); if (_ratedParas.MaxQ != null && _ratedParas.MaxQ > 0) { SetCellValue(sheet, 4, "C", rated_max_flow_ls); } //SetCellValue(sheet, 4, "C", max_flow_real_ls); if (_ratedParas.MaxH != null && _ratedParas.MaxH > 0) { SetCellValue(sheet, 5, "C", _ratedParas.MaxH.Value); } //SetCellValue(sheet, 5, "C", max_head_real);//最高扬程 if (_ratedParas.Voltage > 0) { SetCellValue(sheet, 4, "I", _ratedParas.Voltage); } //最大电流 //SetCellValue(sheet, 6, "C", max_a_real); if (_ratedParas.CurrentI > 0) { SetCellValue(sheet, 6, "C", _ratedParas.CurrentI); } //检测时间 SetCellValue(sheet, 4, "S", this._featTestItem.ItemCreateTime.ToString("yyyy-MM-dd")); //出口表位高 var mp_outlet_press = this._allMonitorPoints.Find(x => x.Property == TProduct.Model.MonitorTypeProperty.出口 && x.MonitorType == Model.eMonitorType.压力); //if (mp_outlet_press != null && mp_outlet_press.Elevation != null) //{ // SetCellValue(sheet, 4, "N", mp_outlet_press.Elevation.Value); //} ////出口口径 //if (mp_outlet_press != null && mp_outlet_press.PipeDia != null && // mp_outlet_press.PipeDia.Value > 0) //{ // SetCellValue(sheet, 5, "N", mp_outlet_press.PipeDia.Value); //} //else //{ // var pipeParas = new Model.PipeParas4Pump(this._workBench.PipeParas); // if (pipeParas != null && pipeParas.OutletDia != null) // { // SetCellValue(sheet, 5, "N", pipeParas.OutletDia.Value); // } //} //进口口径 var mp_inlet_press = this._allMonitorPoints.Find(x => x.Property == TProduct.Model.MonitorTypeProperty.进口 && x.MonitorType == Model.eMonitorType.压力); //if (mp_inlet_press != null && mp_inlet_press.PipeDia != null && // mp_inlet_press.PipeDia.Value > 0) //{ // SetCellValue(sheet, 5, "I", mp_inlet_press.PipeDia.Value); //} //else //{ // var pipeParas = new Model.PipeParas4Pump(this._workBench.PipeParas); // if (pipeParas != null && pipeParas.InletDia != null) // { // SetCellValue(sheet, 5, "I", pipeParas.InletDia.Value); // } //} //试样来源 if (this._testProject.ProjectParas.TestNature == Model.eTestProjectNature.例行测试) { SetCellValue(sheet, 5, "S", "例行测试"); } if (this._testProject.ProjectParas.TestNature == Model.eTestProjectNature.小批量) { SetCellValue(sheet, 5, "S", "小批量"); } if (this._testProject.ProjectParas.TestNature == Model.eTestProjectNature.样件) { SetCellValue(sheet, 5, "S", "样件"); } //试验水温 SetCellValue(sheet, 6, "I", _testProject.ProjectParas.JzWenDu); return true; } private bool WriteRecordData(NPOI.SS.UserModel.ISheet sheet) { if (this._allRecords == null || this._allRecords.Count == 0) return false; var mp_电压 = this._allMonitorPoints.Find(x => x.MonitorType == Model.eMonitorType.相电压); var mp_电流 = this._allMonitorPoints.Find(x => x.MonitorType == Model.eMonitorType.相电流); var mp_流量 = this._allMonitorPoints.Find(x => x.MonitorType == Model.eMonitorType.流量); var mp_转速 = this._allMonitorPoints.Find(x => x.MonitorType == Model.eMonitorType.转速); var mp_outlet_press = this._allMonitorPoints.Find(x => x.Property == TProduct.Model.MonitorTypeProperty.出口 && x.MonitorType == Model.eMonitorType.压力); var mp_inlet_press = this._allMonitorPoints.Find(x => x.Property == TProduct.Model.MonitorTypeProperty.进口 && x.MonitorType == Model.eMonitorType.压力); int record_line_no = 10; List sort_records = null; if (RecordDataSortType == 0) { sort_records = (from x in this._allRecords orderby x.CorrectPtQ select x).ToList(); } else if (RecordDataSortType == 1) { sort_records = (from x in this._allRecords orderby x.Time select x).ToList(); } else { sort_records = this._allRecords; } int index = 1; foreach (var record in sort_records) { if (record.MonitorRecordList == null) continue; SetCellValue(sheet, record_line_no, "A", index.ToString()); index++; //进口压力 KPa if (mp_inlet_press != null) { var v_inlet_press = record.MonitorRecordList.Find(x => x.ID == mp_inlet_press.ID); if (v_inlet_press != null && !string.IsNullOrEmpty(v_inlet_press.Value)) { SetCellValue(sheet, record_line_no, "B", v_inlet_press.GetDoubleValue() * 1000); } } //出口压力 KPa if (mp_outlet_press != null) { var v_outlet_press = record.MonitorRecordList.Find(x => x.ID == mp_outlet_press.ID); if (v_outlet_press != null && !string.IsNullOrEmpty(v_outlet_press.Value)) { SetCellValue(sheet, record_line_no, "D", v_outlet_press.GetDoubleValue() * 1000); } } if (mp_电压 != null) { var v_电压 = record.MonitorRecordList.Find(x => x.ID == mp_电压.ID); if (v_电压 != null && !string.IsNullOrEmpty(v_电压.Value)) { SetCellValue(sheet, record_line_no, "F", v_电压.Value); } } if (mp_电流 != null) { var v_电流 = record.MonitorRecordList.Find(x => x.ID == mp_电流.ID); if (v_电流 != null && !string.IsNullOrEmpty(v_电流.Value)) { max_currentI_real = Math.Max(max_currentI_real, v_电流.GetDoubleValue()); SetCellValue(sheet, record_line_no, "H", v_电流.Value); } } if (mp_流量 != null) {//L/min var v_流量 = record.MonitorRecordList.Find(x => x.ID == mp_流量.ID); if (v_流量 != null && !string.IsNullOrEmpty(v_流量.Value)) { SetCellValue(sheet, record_line_no, "J", Math.Round(Eventech.Common.UnitQHelper.fromM3H(Eventech.Model.UnitQ.LMIN, v_流量.GetDoubleValue()), 2)); } } if (mp_转速 != null) { var v_转速 = record.MonitorRecordList.Find(x => x.ID == mp_转速.ID); if (v_转速 != null && !string.IsNullOrEmpty(v_转速.Value)) { SetCellValue(sheet, record_line_no, "L", v_转速.Value); } } SetCellValue(sheet, record_line_no, "N", record.CorrectPtH); //水功率 var water_poewr = record.CorrectPtP.Value * record.CorrectPtE.Value / 100; //record.CorrectPtQ * record.CorrectPtH * TProduct.ConstantParas.g / 3600;//流量×扬程×g*介质密度÷3600 SetCellValue(sheet, record_line_no, "P", Math.Round(water_poewr * 1000, 2)); SetCellValue(sheet, record_line_no, "R", record.CorrectPtP.Value * 1000); SetCellValue(sheet, record_line_no, "T", record.CorrectPtE.Value); record_line_no++; } //最大电流 SetCellValue(sheet, 6, "C", max_currentI_real); sheet.ForceFormulaRecalculation = true; return true; } private bool WritePage检测报告汇总(TProduct.Model.DispStautInfo infoDelegete) { NPOI.SS.UserModel.ISheet sheet检测报告汇总 = _theBookHander.GetSheetAt(1); if (sheet检测报告汇总 == null) { infoDelegete(" 生成失败,Error:未发现模版检测报告汇总页", 1); return false; } //产品名称 SetCellValue(sheet检测报告汇总, 3, "F", this._currentPumpCatalog.Name); //送样日期 var testParas = this._featTestItem.ProjectParas; SetCellValue(sheet检测报告汇总, 3, "B", this._currentPart.SendTime); //试验编号 SetCellValue(sheet检测报告汇总, 3, "D", this._testProject.Code); //生产日期 SetCellValue(sheet检测报告汇总, 4, "B", this._currentPart.ManufactureTime); //制造厂家 SetCellValue(sheet检测报告汇总, 4, "D", new BLL.ManufacturerBase().GetNameByID( this._currentPart.ManufacturerID)); //产品型号 SetCellValue(sheet检测报告汇总, 4, "F", this._currentPump.Name); //送检数量 SetCellValue(sheet检测报告汇总, 5, "D", testParas.InspectionNumber); //检测时间 SetCellValue(sheet检测报告汇总, 6, "D", this._featTestItem.ItemCreateTime.ToString("yyyy-MM-dd")); //检 测 人 SetCellValue(sheet检测报告汇总, 6, "B", this._testUserName); ////产品编号 //SetCellValue(sheet检测报告汇总, 13, "B", this._currentPart.Code); if (rated_max_flow_ls > 0) { SetCellValue(sheet检测报告汇总, 11, "D", rated_max_flow_ls); } if (max_flow_real_ls > 0) { SetCellValue(sheet检测报告汇总, 11, "F", max_flow_real_ls); } if (rated_max_head > 0) { SetCellValue(sheet检测报告汇总, 12, "D", rated_max_head); } if (max_head_real > 0) { SetCellValue(sheet检测报告汇总, 12, "F", max_head_real); } if (_isJudgeResultQH_OK) { SetCellValue(sheet检测报告汇总, 13, "B", "产品符合性能标准,型式试验合格。"); SetCellValue(sheet检测报告汇总, 14, "B", " ■合格 □不合格 "); } else { SetCellValue(sheet检测报告汇总, 14, "B", " □合格 ■不合格 "); SetCellValue(sheet检测报告汇总, 13, "B", "产品符合性能标准,型式试验不合格。"); } sheet检测报告汇总.ForceFormulaRecalculation = true; return true; } private string BuildJudgeInfo() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendFormat("1.保证最大流量 {0}(L / min),实测最大流量 {1}(L / min),判定:{2};", rated_max_flow_ls, max_flow_real_ls, _isJudgeResultQ_OK ? "合格" : "不合格"); stringBuilder.AppendLine(); stringBuilder.AppendFormat("2.保证最高扬程 {0}(m),实测最高扬程 {1}(m),判定:{2}; ", rated_max_head, max_head_real, _isJudgeResultH_OK ? "合格" : "不合格"); stringBuilder.AppendLine(); if (this._ratedParas.CurrentI > 0 && this._ratedParas.Voltage > 0) { var rated_max_power = this._ratedParas.CurrentI * this._ratedParas.Voltage;//算出来单位就是W stringBuilder.AppendFormat("3.保证最大功率 {0}(W),实测最大功率 {1}(W),判定:{2}; ", rated_max_power, this.max_power_real * 1000, rated_max_power >= max_power_real * 1000 ? "合格" : "不合格"); } else { stringBuilder.Append("3.保证最大功率 ***(W),实测最大功率 ***(W),判定:***; "); } stringBuilder.AppendLine(); if (_maxEtaPt == null) { stringBuilder.Append("4.最高效率 ***( %),最高效率点流量 ***( L / min),最高效率点扬程 ***. "); } else { var max_ls = Math.Round(Eventech.Common.UnitQHelper.fromM3H( Eventech.Model.UnitQ.LMIN, _maxEtaPt.Q), 2); stringBuilder.AppendFormat("4.最高效率 {0} % ,最高效率点流量 {1} L / min),最高效率点扬程 {2} ", _maxEtaPt.E, max_ls, _maxEtaPt.H); } return stringBuilder.ToString(); } } }