CSHARP操作宏
1 /**//// <summary>
2 /// 根据传入的报表名称及相应参数调用Excel的宏生成报表
3 /// 如果成功则返回空,否则返回一包含出错原因的字符串
4 /// </summary>
5 /// <param name="reportName">要生成的报表名称</param>
6 /// <param name="rptParams">生成该报表需要的参数列表</param>
7 /// <returns></returns>
8 public static string CreateReports(string rptPath,string rptName,string rptNamefix,Hashtable rptParams)
9 {
10 Excel.Application oXL;
11
12 string sResult = null;
13 try
14 {
15 // Start a Excel application
16 oXL = new Excel.Application();
17
18 //隐藏Excel
19 oXL.Visible = false;
20 }
21 catch( Exception theException)
22 {
23 return String.Concat("在请求Excel服务时出错!",theException.Message);
24 }
25 try
26 {
27 //打开主Excel文件
28 if (SystemConfig.OfficeVerstring=="2003")
29 {
30 oXL.Workbooks.Open(rptPath+"\\oilScoreReports\\templates\\oilScoreReports.xla",Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
31 //调用VBA生成报表
32 oXL.Run("CreateReports.CreateReports",rptPath,rptName,rptParams["DWMC"],"",rptParams["rptDate"],rptParams["strCnt"],rptParams["userID"],"主管","审核","制表",rptNamefix,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
33 }
34// else
35// {
36// oXL.Workbooks.Open(rptPath+"\\oilScoreReports\\templates\\oilScoreReports.xla",Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
37// //调用VBA生成报表
38// oXL.Run("CreateReports.CreateReports",rptPath,rptName,rptParams["DWMC"],"",rptParams["rptDate"],rptParams["strCnt"],rptParams["userID"],"主管","审核","制表",rptNamefix,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
39// }
40
41 }
42 catch( Exception theException )
43 {
44 //杀掉Excel进程
45 sResult = "生成报表时出错: ";
46 sResult = String.Concat( sResult, theException.Message );
47 }
48 finally
49 {
50 oXL.Quit();
51 Marshal.ReleaseComObject(oXL);
52 GC.Collect();
53 }
54 return sResult;
55
56 }
1 /**//// <summary>
2 /// 根据传入的报表名称及相应参数调用Excel的宏生成报表
3 /// 如果成功则返回空,否则返回一包含出错原因的字符串
4 /// </summary>
5 /// <param name="reportName">要生成的报表名称</param>
6 /// <param name="rptParams">生成该报表需要的参数列表</param>
7 /// <returns></returns>
8 public static string CreateReports(string rptPath,string rptName,string rptNamefix,Hashtable rptParams)
9 {
10 Excel.Application oXL;
11
12 string sResult = null;
13 try
14 {
15 // Start a Excel application
16 oXL = new Excel.Application();
17
18 //隐藏Excel
19 oXL.Visible = false;
20 }
21 catch( Exception theException)
22 {
23 return String.Concat("在请求Excel服务时出错!",theException.Message);
24 }
25 try
26 {
27 //打开主Excel文件
28 if (SystemConfig.OfficeVerstring=="2003")
29 {
30 oXL.Workbooks.Open(rptPath+"\\oilScoreReports\\templates\\oilScoreReports.xla",Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
31 //调用VBA生成报表
32 oXL.Run("CreateReports.CreateReports",rptPath,rptName,rptParams["DWMC"],"",rptParams["rptDate"],rptParams["strCnt"],rptParams["userID"],"主管","审核","制表",rptNamefix,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
33 }
34// else
35// {
36// oXL.Workbooks.Open(rptPath+"\\oilScoreReports\\templates\\oilScoreReports.xla",Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
37// //调用VBA生成报表
38// oXL.Run("CreateReports.CreateReports",rptPath,rptName,rptParams["DWMC"],"",rptParams["rptDate"],rptParams["strCnt"],rptParams["userID"],"主管","审核","制表",rptNamefix,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
39// }
40
41 }
42 catch( Exception theException )
43 {
44 //杀掉Excel进程
45 sResult = "生成报表时出错: ";
46 sResult = String.Concat( sResult, theException.Message );
47 }
48 finally
49 {
50 oXL.Quit();
51 Marshal.ReleaseComObject(oXL);
52 GC.Collect();
53 }
54 return sResult;
55
56 }
===============
这里的内容是回下面那个朋友的.
1、在C#里面调用宏这是必须的。并且这个也很简单,就按上面我说的就可以做到。
2、在宏里面控制窗体里面的按钮的选择情况。最后调用宏即可达到目的。
3、具体贴图如下:
图1
图2
4、不知道这样能不能达到你的要求。