excel vba 调用java_C#调用Excel VBA宏

本文档展示了如何使用C#代码来调用Excel VBA宏。通过创建一个名为`ExcelMacroHelper`的类,该类包含一个`RunExcelMacro`方法,该方法接收Excel文件路径、宏名、参数、返回值和是否显示Excel窗口作为参数,然后通过`ApplicationClass`对象执行宏。在执行过程中,检查了文件是否存在、宏名称是否为空,并在完成后释放了所有相关资源。
摘要由CSDN通过智能技术生成

1 using System;

2 using System.Collections.Generic;

3 using System.Text;

4 using Excel = Microsoft.Office.Interop.Excel;

5 using Microsoft.Office.Core;

6 using System.IO;

7

8 namespace DoVBAMacro

9 {

10     /// 

11     /// 执行Excel VBA宏帮助类

12     /// 

13     public class ExcelMacroHelper

14     {

15         /// 

16         /// 执行Excel中的宏

17         /// 

18         /// Excel文件路径

19         /// 宏名称

20         /// 宏参数组

21         /// 宏返回值

22         /// 执行时是否显示Excel

23         public void RunExcelMacro(

24                                             string excelFilePath,

25                                             string macroName,

26                                             object[] parameters,

27                                             out object rtnValue,

28                                             bool isShowExcel

29                                         )

30         {

31             try

32             {

33                 #region 检查入参

34

35                 // 检查文件是否存在

36                 if (!File.Exists(excelFilePath))

37                 {

38                     throw new System.Exception(excelFilePath + " 文件不存在");

39                 }

40

41                 // 检查是否输入宏名称

42                 if (string.IsNullOrEmpty(macroName))

43                 {

44                     throw new System.Exception("请输入宏的名称");

45                 }

46

47                 #endregion

48

49                 #region 调用宏处理

50

51                 // 准备打开Excel文件时的缺省参数对象

52                 object oMissing = System.Reflection.Missing.Value;

53

54                 // 根据参数组是否为空,准备参数组对象

55                 object[] paraObjects;

56

57                 if (parameters == null)

58                 {

59                     paraObjects = new object[] { macroName };

60                 }

61                 else

62                 {

63                     // 宏参数组长度

64                     int paraLength = parameters.Length;

65

66                     paraObjects = new object[paraLength + 1];

67

68                     paraObjects[0] = macroName;

69                     for (int i = 0; i 

70                     {

71                         paraObjects[i + 1] = parameters[i];

72                     }

73                 }

74

75                 // 创建Excel对象示例

76                 Excel.ApplicationClass oExcel = new Excel.ApplicationClass();

77

78                 // 判断是否要求执行时Excel可见

79                 if (isShowExcel)

80                 {

81                     // 使创建的对象可见

82                     oExcel.Visible = true;

83                 }

84

85                 // 创建Workbooks对象

86                 Excel.Workbooks oBooks = oExcel.Workbooks;

87

88                 // 创建Workbook对象

89                 Excel._Workbook oBook = null;

90

91                 // 打开指定的Excel文件

92                 oBook = oBooks.Open(

93                                         excelFilePath,

94                                         oMissing,

95                                         oMissing,

96                                         oMissing,

97                                         oMissing,

98                                         oMissing,

99                                         oMissing,

100                                         oMissing,

101                                         oMissing,

102                                         oMissing,

103                                         oMissing,

104                                         oMissing,

105                                         oMissing,

106                                         oMissing,

107                                         oMissing

108                                    );

109

110                 // 执行Excel中的宏

111                 rtnValue = this.RunMacro(oExcel, paraObjects);

112

113                 // 保存更改

114                 oBook.Save();

115

116                 // 退出Workbook

117                 oBook.Close(false, oMissing, oMissing);

118

119                 #endregion

120

121                 #region 释放对象

122

123                 // 释放Workbook对象

124                 System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook);

125                 oBook = null;

126

127                 // 释放Workbooks对象

128                 System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks);

129                 oBooks = null;

130

131                 // 关闭Excel

132                 oExcel.Quit();

133

134                 // 释放Excel对象

135                 System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel);

136                 oExcel = null;

137

138                 // 调用垃圾回收

139                 GC.Collect();

140

141                 #endregion

142             }

143             catch (Exception ex)

144             {

145                 throw ex;

146             }

147         }

148

149         /// 

150         /// 执行宏

151         /// 

152         /// Excel对象

153         /// 参数(第一个参数为指定宏名称,后面为指定宏的参数值)

154         /// 宏返回值

155         private object RunMacro(object oApp, object[] oRunArgs)

156         {

157             try

158             {

159                 // 声明一个返回对象

160                 object objRtn;

161

162                 // 反射方式执行宏

163                 objRtn = oApp.GetType().InvokeMember(

164                                                         "Run",

165                                                         System.Reflection.BindingFlags.Default |

166                                                         System.Reflection.BindingFlags.InvokeMethod,

167                                                         null,

168                                                         oApp,

169                                                         oRunArgs

170                                                      );

171

172                 // 返回值

173                 return objRtn;

174

175             }

176             catch (Exception ex)

177             {

178                 // 如果有底层异常,抛出底层异常

179                 if (ex.InnerException.Message.ToString().Length > 0)

180                 {

181                     throw ex.InnerException;

182                 }

183                 else

184                 {

185                     throw ex;

186                 }

187             }

188         }

189     }

190 }

191

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值