执行Excel的宏
internal class ExcelHelper { /// <summary> /// 执行Excel中的宏 /// </summary> /// <param name="excelFilePath">Excel文件路径</param> /// <param name="macroName">宏名称</param> /// <param name="parameters">宏参数组</param> /// <param name="rtnValue">宏返回值</param> /// <param name="isShowExcel">执行时是否显示Excel</param> public void RunExcelMacro( string excelFilePath, string macroName, object[] parameters, out object rtnValue, bool isShowExcel ) { #region 检查入参 // 检查文件是否存在 if (!File.Exists(excelFilePath)) { throw new System.Exception(excelFilePath + " 文件不存在"); } // 检查是否输入宏名称 if (string.IsNullOrEmpty(macroName)) { throw new System.Exception("请输入宏的名称"); } #endregion #region 宏参数对象 // 准备打开Excel文件时的缺省参数对象 object oMissing = System.Reflection.Missing.Value; // 根据参数组是否为空,准备参数组对象 object[] paraObjects; if (parameters == null) { paraObjects = new object[] { macroName }; } else { // 宏参数组长度 int paraLength = parameters.Length; paraObjects = new object[paraLength + 1]; paraObjects[0] = macroName; for (int i = 0; i < paraLength; i++) { paraObjects[i + 1] = parameters[i]; } } #endregion // 创建Excel对象示例 Microsoft.Office.Interop.Excel.Application oExcel = new Microsoft.Office.Interop.Excel.Application(); try { // 判断是否要求执行时Excel可见 if (isShowExcel) { // 使创建的对象可见 oExcel.Visible = true; } // 创建Workbooks对象 Microsoft.Office.Interop.Excel.Workbooks oBooks = oExcel.Workbooks; // 创建Workbook对象 Microsoft.Office.Interop.Excel._Workbook oBook = null; // 打开指定的Excel文件 oBook = oBooks.Open( excelFilePath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing ); // 执行Excel中的宏 rtnValue = this.RunMacro(oExcel, paraObjects); // 保存更改 oBook.Save(); // 退出Workbook oBook.Close(false, oMissing, oMissing); #region 释放对象 // 释放Workbook对象 System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook); oBook = null; // 释放Workbooks对象 System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks); oBooks = null; #endregion } catch (Exception ex) { throw ex; } finally { if (oExcel != null) { // 关闭Excel oExcel.Quit(); // 释放Excel对象 System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel); oExcel = null; // 调用垃圾回收 GC.Collect(); } } } /// <summary> /// 执行宏 /// </summary> /// <param name="oApp">Excel对象</param> /// <param name="oRunArgs">参数(第一个参数为指定宏名称,后面为指定宏的参数值)</param> /// <returns>宏返回值</returns> private object RunMacro(object oApp, object[] oRunArgs) { try { // 声明一个返回对象 object objRtn; // 反射方式执行宏 objRtn = oApp.GetType().InvokeMember( "Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs ); // 返回值 return objRtn; } catch (Exception ex) { // 如果有底层异常,抛出底层异常 if (ex.InnerException.Message.ToString().Length > 0) { throw ex.InnerException; } else { throw ex; } } } }
执行Excel文件的宏
try { // 返回对象 object objRtn = new object(); // 获得一个ExcelMacroHelper对象 ExcelHelper excelMacroHelper = new ExcelHelper(); // 执行指定Excel中的宏,执行时显示Excel excelMacroHelper.RunExcelMacro( @"C:\Users\Desktop\test2.xlsm", "getTime2", new Object[] { "现在时刻" }, out objRtn, true ); // 执行指定Excel中的宏,执行时不显示Excel excelMacroHelper.RunExcelMacro( @"C:\Users\Desktop\test2.xlsm", "getTime2", new Object[] { "现在时刻" }, out objRtn, false ); // 执行指定Excel中的宏,执行时显示Excel,有返回值 excelMacroHelper.RunExcelMacro( @"C:\Users\Desktop\test2.xlsm", "getTime3", new Object[] { "现在时刻" }, out objRtn, true ); MessageBox.Show((string)objRtn); } catch (System.Exception ex) { MessageBox.Show(ex.Message); }
Excel文件:
,Best Wish 不负年华