using Mesnac.Action.Base; using Mesnac.Action.ChemicalWeighing.Entity.Report; using Mesnac.Action.ChemicalWeighing.Report.ProductionReport; using Microsoft.Office.Interop.Excel; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Mesnac.Action.ChemicalWeighing.Report.DryMixer { /// /// 干混机报表导出事件 /// public class ExportAction : ChemicalWeighingAction, Base.IAction { private Control _clientGridControl = null; //报表明细DGV private DbMCControl _dgvDryMixer = null; //报表数据DGV public void Run(RuntimeParameter runtime) { base.RunIni(runtime); //必须要调用的 ICSharpCode.Core.LoggingService.Debug("干混机报表-导出..."); this._dgvDryMixer = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Report_DryMixer").FirstOrDefault(); DataGridView _DryMixerGridView = this._dgvDryMixer.BaseControl as DataGridView; if (_DryMixerGridView != null) { DryMixerDos dryMixerDos = new DryMixerDos(); dryMixerDos.eqNo = ParseToInt(_DryMixerGridView.SelectedRows[0].Cells["eqNo"].Value.ToString()); dryMixerDos.dos = ParseToInt(_DryMixerGridView.SelectedRows[0].Cells["dos"].Value.ToString()); dryMixerDos.batch = ParseToInt(_DryMixerGridView.SelectedRows[0].Cells["batch"].Value.ToString()); dryMixerDos.matCode = ParseToInt(_DryMixerGridView.SelectedRows[0].Cells["matCode"].Value.ToString()); dryMixerDos.setValue = Convert.ToDouble(_DryMixerGridView.SelectedRows[0].Cells["setValue"].Value); dryMixerDos.setToler = Convert.ToDouble(_DryMixerGridView.SelectedRows[0].Cells["setToler"].Value); dryMixerDos.actValue = Convert.ToDouble(_DryMixerGridView.SelectedRows[0].Cells["actValue"].Value); dryMixerDos.actToLer = Convert.ToDouble(_DryMixerGridView.SelectedRows[0].Cells["actToLer"].Value); dryMixerDos.recordTime = _DryMixerGridView.SelectedRows[0].Cells["recordTime"].Value.ToString(); 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) { this._clientGridControl = GetAllControls().Where(x => x.Name.Contains("MultiColHeaderDgv")).FirstOrDefault(); if (_clientGridControl == null) { ICSharpCode.Core.LoggingService.Warn("{生产报表} 缺少缺少称量名细MultiColHeaderDgv控件..."); runtime.IsReturn = false; return; } Mesnac.Controls.Default.MultiColHeaderDgv clientGrid = (this._clientGridControl as Mesnac.Controls.Default.MultiColHeaderDgv); System.Data.DataTable dt = clientGrid.DataSource as System.Data.DataTable; string fileName = sfd.FileName; if (!String.IsNullOrEmpty(fileName)) { //写入Excle DataTabletoExcel(fileName, dt, dryMixerDos); } } } } /// /// 写入到Excel文件 /// /// /// /// public void DataTabletoExcel(string strFileName, System.Data.DataTable tmpDataTable, DryMixerDos dryMixerDos) { ///先得到datatable的行数 int rowNum = tmpDataTable.Rows.Count; ///列数 int columnNum = tmpDataTable.Columns.Count; ///声明一个应用程序类实例 Microsoft.Office.Interop.Excel.Application xlApp = new ApplicationClass(); //创建一个新工作簿 Workbook xlBook = xlApp.Workbooks.Add(); ///在工作簿中得到sheet。 _Worksheet oSheet = (_Worksheet)xlBook.Worksheets[1]; #region 绘制列 //绘制配方名和开始时间 oSheet.Cells[1, 1] = "设备"; oSheet.Cells[1, 2] = "Dos"; oSheet.Cells[1, 3] = "批次号"; oSheet.Cells[1, 4] = "物料代码"; oSheet.Cells[1, 5] = "设定重量"; oSheet.Cells[1, 6] = "设定公差"; oSheet.Cells[1, 7] = "实际重量"; oSheet.Cells[1, 8] = "实际公差"; oSheet.Cells[1, 9] = "记录时间"; oSheet.Cells[2, 1] = dryMixerDos.eqNo; oSheet.Cells[2, 2] = dryMixerDos.dos; oSheet.Cells[2, 3] = dryMixerDos.batch; oSheet.Cells[2, 4] = dryMixerDos.matCode; oSheet.Cells[2, 5] = dryMixerDos.setValue; oSheet.Cells[2, 6] = dryMixerDos.setToler; oSheet.Cells[2, 7] = dryMixerDos.actValue; oSheet.Cells[2, 8] = dryMixerDos.setToler; oSheet.Cells[2, 9] = dryMixerDos.recordTime; //自定义方法,绘制合并表头 oSheet.Cells[3, 1] = "设备"; oSheet.Cells[3, 2] = "Mix"; oSheet.Cells[3, 3] = "批次"; oSheet.Cells[3, 4] = "步号"; oSheet.Cells[3, 5] = "动作"; oSheet.Cells[3, 6] = "时间"; oSheet.Cells[3, 7] = "温度"; oSheet.Cells[3, 8] = "速度"; oSheet.Cells[3, 9] = "记录时间"; #endregion //将DataTable中的数据导入Excel中 for (int i = 0; i < rowNum; i++) { for (int j = 0; j < columnNum; j++) { ///excel中的列是从1开始的 xlApp.Cells[i + 4, j + 1] = tmpDataTable.Rows[i][j].ToString(); } } oSheet.SaveAs(strFileName); } /// /// 字符串转Int /// /// /// private int ParseToInt(string str) { int returnInt = 0; if (str == null || str.Trim().Length < 1) { return returnInt; } if (int.TryParse(str, out returnInt)) { return returnInt; } else { return 0; } } } }