using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data; using ICSharpCode.Core; using Mesnac.Action.Base; using Mesnac.Codd.Session; namespace Mesnac.Action.ChemicalWeighing.Alarm.PmtAlarm { /// /// 导出报警参数业务 /// public class ExportAction : ChemicalWeighingAction, IAction { public void Run(RuntimeParameter runtime) { base.RunIni(runtime); //必须要调用的 ICSharpCode.Core.LoggingService.Debug("报警参数-导出..."); DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { ICSharpCode.Core.LoggingService.Error("获取本地数据连接失败..."); return; } SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "xls files(*.xls)|*.xls"; sfd.FileName = String.Format("Pmt_Alarm_{0:yyyyMMdd}", DateTime.Now); sfd.AddExtension = true; DialogResult result = sfd.ShowDialog(); if (result == DialogResult.OK) { string sqlstr = @"SELECT Alarm_ID ,Alarm_PLC ,Alarm_Block ,Alarm_Word ,Alarm_Bit ,Alarm_Cn_Info ,Alarm_Other_Info FROM Pmt_Alarm ORDER BY Alarm_Block, Alarm_Word, Alarm_Bit"; DataTable dt = dbHelper.GetDataTableBySql(sqlstr); if (dt.Rows.Count == 0) { DataRow row = dt.NewRow(); row["Alarm_ID"] = String.Empty; row["Alarm_PLC"] = String.Empty; row["Alarm_Block"] = String.Empty; row["Alarm_Word"] = 0; row["Alarm_Bit"] = 0; //row["AlarmCode"] = String.Empty; row["Alarm_Cn_Info"] = String.Empty; row["Alarm_Other_Info"] = String.Empty; //row["Remark"] = String.Empty; //row["RecordTime"] = DateTime.Now; //row["RecordUser"] = Mesnac.Basic.UserInfo.Instance.UserName; //row["DataSource"] = "1"; dt.Rows.InsertAt(row, 0); } string fileName = sfd.FileName; if (!String.IsNullOrEmpty(fileName)) { try { System.IO.Stream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite); Mesnac.Basic.DataToFileHandler.Instance.ToExcel(dt, ref fs); fs.Close(); string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Alarm_PmtAlarm_ExportAction_msg1")); //导出报警参数数据至Excel成功! ICSharpCode.Core.LoggingService.Info(msg1); #region 记录操作日志 base.DBLog(msg1); #endregion MessageBox.Show(msg1, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { string msg2 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Alarm_PmtAlarm_ExportAction_msg2")); //导出报警参数数据至Excel失败:{0}! msg2 = String.Format(msg2, ex.Message); ICSharpCode.Core.LoggingService.Error(msg2); #region 记录操作日志 base.DBLog(msg2); #endregion MessageBox.Show(msg2, Mesnac.Basic.LanguageHelper.WarnCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning); runtime.IsReturn = true; return; } } } } } }