使用VS2008开发及部署Excel AddIn 心得

使用VS2008开发Excel AddIn,在部署的时候会出现很多奇怪的问题。

如:在开发机器上安装没有问题,然而到一台普通的机器上时则可能会出现安装不上的问题。

那么遇到此种情况首先需检查安装程序是否做了如下操作:

1)为你的Excel AddIn进行策略授权(Caspol)

2)客户环境是否安装了Microsoft Office 2003 Primary Interop Assemblies

3)客户环境是否安装了Microsoft Visual Studio 2005 Tools for Office Runtime

 

如果以上三步都已经符合要求了,那么则需要你在AddIn的构造函数中是否添加了代码(这个地方如果你初始化了自己的自定义对象可能会因为你的程序集尚未装载到内存而导致无法初始化的错误:未将对象设置到实例。这样你的程序AddIn将会无法正常加载,抛出加载时错误或本Excel禁用)。

 

下面的代码可以放在安装包的Commit时执行:

 

ExpandedBlockStart.gif 代码
  1  using  System;
  2  using  System.Collections.Generic;
  3  using  System.Linq;
  4  using  System.Text;
  5  using  System.Diagnostics;
  6  using  Microsoft.Win32;
  7 
  8  namespace  CTPExcelAddInInstall
  9  {
 10       class  Program
 11      {
 12           static  System.IO.StreamWriter sw  =   null ;
 13           static   void  Main( string [] args)
 14          {
 15               string  fullpath  =  System.Reflection.Assembly.GetExecutingAssembly().Location;
 16               string  installLocation  =  fullpath.Substring( 0 , fullpath.LastIndexOf( ' \\ ' ));
 17 
 18              sw  =  System.IO.File.CreateText( " C:\\CTPExcelAddInstall.log " );
 19              sw.WriteLine( " 安装路径: "   +  installLocation);
 20 
 21              sw.WriteLine( " 开始安装时间: "   +  DateTime.Now.ToString());
 22 
 23               try
 24              {
 25                  CaspolExcelAddInFiles(installLocation);
 26 
 27                  sw.WriteLine( " 成功执行授权脚本! " );
 28 
 29              }
 30               catch  (Exception exp)
 31              {
 32                  sw.WriteLine( " 为应用程序授权时出错,错误信息: "   +  exp.Message);
 33              }
 34 
 35               try
 36              {
 37                  InstallOfficePIA(installLocation);
 38 
 39                  sw.WriteLine( " 成功执行安装 Office 2003 Primary Interop Assemblies! " );
 40              }
 41               catch  (Exception expPIA)
 42              {
 43                  sw.WriteLine( " 执行安装 Office 2003 Primary Interop Assemblies 时出错,错误信息: "   +  expPIA.Message);
 44              }
 45 
 46               try
 47              {
 48                  RegistryKey regKey  =  Registry.LocalMachine.OpenSubKey( @" SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Microsoft Visual Studio 2005 Tools for Office Runtime " );
 49                   if  (regKey  ==   null )
 50                  {
 51                      InstallVSTO2005(installLocation);                    
 52                  }
 53              }
 54               catch  (Exception expVSTO)
 55              {
 56                  sw.WriteLine( " 执行安装Microsoft Visual Studio 2005 Tools for Office Runtime 时出错,错误信息: "   +  expVSTO.Message);
 57              }
 58              
 59              
 60 
 61              sw.WriteLine( " 结束安装时间: "   +  DateTime.Now.ToString());
 62              sw.Close();
 63 
 64              System.Threading.Thread.Sleep( 2000 );
 65          }
 66 
 67           ///   <summary>
 68           ///  安装 Office 2003 Primary Interop Assemblies
 69           ///   </summary>
 70           ///   <param name="filePath"> 安装文件路径 </param>
 71           static   void  InstallOfficePIA( string  filePath)
 72          {
 73              Process pExecPIA  =   new  Process();
 74              pExecPIA.StartInfo.FileName  =   " MSIEXEC.EXE " ;
 75              pExecPIA.StartInfo.WorkingDirectory  =   @" C:\Windows\Microsoft.NET\Framework\v2.0.50727 " ;
 76              pExecPIA.StartInfo.CreateNoWindow  =   true ;
 77              pExecPIA.StartInfo.Arguments  =   "  /i \ ""  + filePath +  " \\O2003PIA .msi\ "" ;
 78              pExecPIA.StartInfo.UseShellExecute  =   false ;
 79              pExecPIA.StartInfo.RedirectStandardInput  =   true ;
 80              pExecPIA.StartInfo.RedirectStandardOutput  =   true ;
 81              pExecPIA.StartInfo.RedirectStandardError  =   true ;
 82              pExecPIA.StartInfo.WindowStyle  =  ProcessWindowStyle.Hidden;
 83              pExecPIA.OutputDataReceived  +=   new  DataReceivedEventHandler(p_OutputDataReceived);
 84              pExecPIA.ErrorDataReceived  +=   new  DataReceivedEventHandler(p_OutputDataReceived);
 85              pExecPIA.Start();
 86              pExecPIA.BeginOutputReadLine();
 87              pExecPIA.BeginErrorReadLine();
 88              pExecPIA.Close();
 89          }
 90 
 91           ///   <summary>
 92           ///  Excel插件执行策略授权
 93           ///   </summary>
 94           ///   <param name="filePath"> 授权文件夹 </param>
 95           static   void  CaspolExcelAddInFiles( string  filePath)
 96          {
 97              Process pCaspolCMD  =   new  Process();
 98              pCaspolCMD.StartInfo.FileName  =   " cmd.exe " ;
 99              pCaspolCMD.StartInfo.UseShellExecute  =   false ;
100              pCaspolCMD.StartInfo.RedirectStandardInput  =   true ;
101              pCaspolCMD.StartInfo.RedirectStandardOutput  =   true ;
102              pCaspolCMD.StartInfo.RedirectStandardError  =   true ;
103              pCaspolCMD.StartInfo.WorkingDirectory  =   @" C:\Windows\Microsoft.NET\Framework\v2.0.50727 " ;
104              pCaspolCMD.StartInfo.CreateNoWindow  =   true ;
105              pCaspolCMD.StartInfo.WindowStyle  =  ProcessWindowStyle.Hidden;
106              pCaspolCMD.StartInfo.Arguments  =   " /K \r\nCaspol -u -ag All_Code -url \ ""  + filePath +  " \\ * \ "  FullTrust -n  \ " CTPExcelAddIn\ "" ;
107              pCaspolCMD.OutputDataReceived  +=   new  DataReceivedEventHandler(p_OutputDataReceived);
108              pCaspolCMD.ErrorDataReceived  +=   new  DataReceivedEventHandler(p_OutputDataReceived);
109              pCaspolCMD.Start();
110              pCaspolCMD.StandardInput.WriteLine( " y " );
111 
112              pCaspolCMD.BeginOutputReadLine();
113              pCaspolCMD.BeginErrorReadLine();
114 
115              pCaspolCMD.Close();
116          }
117 
118           ///   <summary>
119           ///  安装 VSTO 2005 Runtime
120           ///   </summary>
121           ///   <param name="filePath"> 安装文件路径 </param>
122           static   void  InstallVSTO2005( string  filePath)
123          {
124               string  DirectoryName  =  filePath  +   " \\Microsoft Visual Studio 2005 Tools for Office Runtime " ;
125 
126               if  (System.IO.Directory.Exists(DirectoryName))
127              {
128                  Process pVSTO2005  =   new  Process();
129                  pVSTO2005.StartInfo.FileName  =  DirectoryName  +   " \\install.exe " ;
130                  pVSTO2005.StartInfo.WorkingDirectory  =   @" C:\Windows\Microsoft.NET\Framework\v2.0.50727 " ;
131                  pVSTO2005.StartInfo.CreateNoWindow  =   false ;
132                  pVSTO2005.StartInfo.UseShellExecute  =   true ;
133                  pVSTO2005.Start();
134                  pVSTO2005.Close();
135 
136                  sw.WriteLine( " 成功执行安装 Microsoft Visual Studio 2005 Tools for Office Runtime! " );
137              }
138               else
139              {
140                  sw.WriteLine( " 未找到“Microsoft Visual Studio 2005 Tools for Office Runtime”安装目录: "   +  DirectoryName);
141              }
142          }
143 
144           static   void  p_OutputDataReceived( object  sender, DataReceivedEventArgs e)
145          {            
146              Console.WriteLine(e.Data);
147          }       
148 
149      }
150  }
151 

 

 

转载于:https://www.cnblogs.com/sucsy/archive/2010/09/14/1826125.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值