using DevExpress.XtraBars.Ribbon; using DevExpress.XtraEditors; using NPOI.HSSF.UserModel; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.IO; using System.Windows.Forms; using Yw; using Yw.Untity; namespace IStation.WinTest { public partial class frmMain : XtraForm { public frmMain() { InitializeComponent(); this.ribbonControl1.SetNormalView(); this.bandedGridView1.SetNormalView(); this.bandedGridView1.RegistCustomDrawRowIndicator(); this.barEditSelStation.EditValue = _sel_index; } private List _schedule_valid_vm_list = null; private int _sel_index = 2; //导入文件 private void barBtnImport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var dlg = new OpenFileDialog(); dlg.Filter = "EXCEL 文件(*.xls)|*.xls"; if (dlg.ShowDialog() != DialogResult.OK) return; var fileName = dlg.FileName; int line = 0; try { //初始化文件 HSSFWorkbook theBook = null; using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { theBook = new HSSFWorkbook(file); } //检查表格是否符合 NPOI.SS.UserModel.ISheet sheet1 = theBook.GetSheet("Sheet1"); if (sheet1 == null) { sheet1 = theBook.GetSheetAt(0); if (sheet1 == null) { MessageBox.Show("请将数据放在Sheet1中"); return; } } //标题行 int title_line_index = 0; //时间 int col_index_time = 0; //总流量 int col_index_flow = 1; //总扬程 int col_index_head = 2; //总功率 int col_index_power = 3; //运行标志 int col_index_flag_rpm_dict = 4; //运行标志 int col_index_flag_flow_dict = 5; //运行标志 int col_index_flag_head_dict = 6; //运行标志 int col_index_flag_power_dict = 7; var row_title = sheet1.GetRow(title_line_index); if (row_title == null) { MessageBox.Show("第一行第一列不能空,"); return; } //开始读取的行 int start_line = title_line_index + 1; var cell_0 = row_title.GetCell(0); if (cell_0 == null) { MessageBox.Show("无法读取表头文件"); return; } _schedule_valid_vm_list = new List(); DateTime time; Dictionary flag_rpm_dict, flag_flow_dict, flag_head_dict, flag_power_dict; double flow, head, power; NPOI.SS.UserModel.IRow rowtemp = null; NPOI.SS.UserModel.ICell cell; for (line = start_line; line < 100; line++) { flow = head = power = -1; flag_rpm_dict = new Dictionary(); flag_flow_dict = new Dictionary(); flag_head_dict = new Dictionary(); flag_power_dict = new Dictionary(); rowtemp = sheet1.GetRow(line); if (rowtemp == null) break; cell = rowtemp.GetCell(col_index_time); if (cell == null) break; if (!DateTime.TryParse(cell.StringCellValue, out time)) { continue; } cell = rowtemp.GetCell(col_index_flow); if (cell == null) break; if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric) flow = cell.NumericCellValue; else if (cell.CellType == NPOI.SS.UserModel.CellType.String) flow = Convert.ToDouble(cell.StringCellValue); else continue; if (flow < 0) break; cell = rowtemp.GetCell(col_index_head); if (cell == null) break; if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric) head = cell.NumericCellValue; else if (cell.CellType == NPOI.SS.UserModel.CellType.String) head = Convert.ToDouble(cell.StringCellValue); else if (cell.CellType == NPOI.SS.UserModel.CellType.Blank) break; if (head < 0) break; cell = rowtemp.GetCell(col_index_power); if (cell == null) break; if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric) power = cell.NumericCellValue; else if (cell.CellType == NPOI.SS.UserModel.CellType.String) power = Convert.ToDouble(cell.StringCellValue); else power = -1; cell = rowtemp.GetCell(col_index_flag_rpm_dict); if (cell == null) break; else if (cell.CellType == NPOI.SS.UserModel.CellType.String) flag_rpm_dict = JsonHelper.Json2Object>(cell.StringCellValue); cell = rowtemp.GetCell(col_index_flag_flow_dict); if (cell == null) break; else if (cell.CellType == NPOI.SS.UserModel.CellType.String) flag_flow_dict = JsonHelper.Json2Object>(cell.StringCellValue); cell = rowtemp.GetCell(col_index_flag_head_dict); if (cell == null) break; else if (cell.CellType == NPOI.SS.UserModel.CellType.String) flag_head_dict = JsonHelper.Json2Object>(cell.StringCellValue); cell = rowtemp.GetCell(col_index_flag_power_dict); if (cell == null) break; else if (cell.CellType == NPOI.SS.UserModel.CellType.String) flag_power_dict = JsonHelper.Json2Object>(cell.StringCellValue); var vm = new ScheduleValidViewModel(); vm.Time = time; vm.TotalFlowSrc = flow; vm.TotalHeadSrc = head; vm.TotalPowerSrc = power; vm.FlagRpmDict = flag_rpm_dict; if (flag_rpm_dict != null && flag_rpm_dict.Any()) { var flags = flag_rpm_dict.Select(x => x.Key).ToList(); vm.RunFlagsSrc = Yw.Untity.IntListHelper.ToString(flags); } vm.FlagFlowDict = flag_flow_dict; vm.FlagHeadDict = flag_head_dict; vm.FlagPowerDict = flag_power_dict; _schedule_valid_vm_list.Add(vm); } this.gridControl1.DataSource = _schedule_valid_vm_list; this.gridControl1.RefreshDataSource(); this.bandedGridView1.BestFitColumns(); } catch (System.Exception err) { MessageBox.Show(string.Format("读取Excel出错!错误原因:{0},行号: {1}", err.Message, line), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } } //调度 private void barBtnSchedule_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (_schedule_valid_vm_list == null || !_schedule_valid_vm_list.Any()) { XtraMessageBox.Show("无数据!"); return; } try { WaitHelper.ShowWaitForm(); var station = new Service.Station().Get(); List pumps; List flags_part1, flags_part2; if (_sel_index == 1) { pumps = station.S1; flags_part1 = station.S1FlagsPart1; flags_part2 = station.S1FlagsPart2; } else { pumps = station.S2; flags_part1 = station.S2FlagsPart1; flags_part2 = station.S2FlagsPart2; } var current_open_pump_list = new List(); var must_open_pump_list = new List(); var must_not_open_pump_list = new List(); var schedule_helper = new Algorithm.ScheduleHelper(); foreach (var vm in _schedule_valid_vm_list) { if (vm.FlagRpmDict == null || !vm.FlagRpmDict.Any()) continue; current_open_pump_list = vm.FlagRpmDict.Select(x => x.Key).ToList(); if (current_open_pump_list == null || current_open_pump_list.Count < 1) continue; var target_flow = vm.TotalFlowSrc; var target_head = vm.TotalHeadSrc; var opt_schedule = schedule_helper.Schedule(pumps, flags_part1, flags_part2, target_flow, target_head, current_open_pump_list, must_open_pump_list, must_not_open_pump_list); if (opt_schedule != null) { vm.TotalFlowSchedule = opt_schedule.Flow; vm.TotalHeadSchedule = opt_schedule.Head; vm.TotalPowerSchedule = opt_schedule.Power; vm.RunFlagsSchedule = opt_schedule.Remark; vm.TotalFlowScheduleSrcDiff = opt_schedule.Flow - target_flow; vm.TotalPowerScheduleSrcDiff = opt_schedule.Power - vm.TotalPowerSrc; } var opt_src = schedule_helper.Calc(pumps, vm.FlagRpmDict, vm.FlagHeadDict, target_flow, target_head); if (opt_src != null) { vm.TotalFlowCalc = opt_src.Flow; vm.TotalPowerCalc = opt_src.Power; vm.TotalFlowCalcSrcDiff = opt_src.Flow - target_flow; vm.TotalPowerCalcSrcDiff = opt_src.Power - vm.TotalPowerSrc; } vm.Round(); } this.gridControl1.RefreshDataSource(); this.bandedGridView1.BestFitColumns(); } //catch (Exception ex) //{ // XtraMessageBox.Show(ex.Message); //} finally { WaitHelper.HideWaitForm(); } } //选中泵站变换 private void barEditSelStation_EditValueChanged(object sender, EventArgs e) { _sel_index = Convert.ToInt32(this.barEditSelStation.EditValue); } } }