using DevExpress.ClipboardSource.SpreadsheetML; using DevExpress.XtraEditors; using ICSharpCode.Core; using Mesnac.Action.Base; using Microsoft.Office.Interop.Excel; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Mesnac.Action.ChemicalWeighing.Report.SolventReport { /// /// 生产报表导出Action /// class ExportAction : ChemicalWeighingAction, Base.IAction { private RuntimeParameter _runtime; private Control _clientDGV = null; //称量明细DGV private DbMCControl _dgvLRPlan = null; //生产计划DGV public static event EventHandler OnRefresh; public void Run(RuntimeParameter runtime) { base.RunIni(runtime); //必须要调用的 this._runtime = runtime; ICSharpCode.Core.LoggingService.Debug("生产报表-导出..."); this._dgvLRPlan = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "RT_plan").FirstOrDefault(); DataGridView lR_planGridView = this._dgvLRPlan.BaseControl as DataGridView; SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "xls files(*.xls)|*.xls"; sfd.FileName = String.Format("生产报表_{0:yyyyMMdd}", DateTime.Now); sfd.AddExtension = true; DialogResult result = sfd.ShowDialog(); if (result == DialogResult.OK) { DbMCControl weighGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Hw_weigh").FirstOrDefault(); // 创建一个新的DataTable对象 System.Data.DataTable dt = new System.Data.DataTable(); DataGridView dataGridView1 = weighGridControl.BaseControl as DataGridView; // 添加列 foreach (DataGridViewColumn column in dataGridView1.Columns) { dt.Columns.Add(column.HeaderText, column.ValueType); } string fileName = sfd.FileName; if (!String.IsNullOrEmpty(fileName)) { try { DataTabletoExcel(_dgvLRPlan, fileName); string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Report_ProductionReport_ExportAction_msg1")); //导出生产报表数据至Excel成功! ICSharpCode.Core.LoggingService.Info(msg1); #region 记录操作日志 base.DBLog(msg1); #endregion MessageBox.Show(msg1, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); runtime.IsReturn = true; if (OnRefresh != null) { OnRefresh(this._runtime.BaseControl.MCRoot, System.EventArgs.Empty); } } catch (Exception ex) { string msg2 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Report_ProductionReport_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; } } } } public void DataTabletoExcel(DbMCControl myDGV, string strFileName) { System.Data.DataTable dts = myDGV.BaseControl.BindDataSource as System.Data.DataTable; dts.Columns["Plan_Id"].ColumnName= "计划号"; dts.Columns["Recipe_ID"].ColumnName= "物料ID"; dts.Columns["Recipe_Name"].ColumnName= "物料名称"; dts.Columns["Version"].ColumnName= "版本"; dts.Columns["Plan_Num"].ColumnName= "计划批次"; dts.Columns["Real_Num"].ColumnName= "完成批次"; dts.Columns["Start_Date"].ColumnName= "开始时间"; dts.Columns["End_Date"].ColumnName= "结束时间"; dts.Columns["Shift_name"].ColumnName= "班组"; dts.Columns["Weight_Man"].ColumnName = "执行人"; dts.Columns["Total_Weight"].ColumnName = "总重量"; dts.Columns["Total_Error"].ColumnName = "总误差"; dts.Columns["IsRetransmission"].ColumnName = "是否重传"; System.IO.Stream fs = new System.IO.FileStream(strFileName, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite); Mesnac.Basic.DataToFileHandler.Instance.ToExcel(dts, ref fs); fs.Close(); } } }