最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】虽然已经安装了Office的Excel但是C#的exe还是运行出错:Could not load file or assembly ‘Microsoft.Office.Interop.Excel, Version=14.0.0.0

C# crifan 19123浏览 0评论

【背景】

给别人写了个C#的exe,其中内部用到excel,然后自己C#项目中引用了:

Microsoft.Office.Interop.Excel

对应的version=14的:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll

Office14 Microsoft.Office.Interop.Excel.dll

然后别人在其电脑上用exe,结果出错:

Could not load file or assembly  Microsoft.Office.Interop.Excel Version 14.0.0.0

错误详细信息:

See the end of this message for details on invoking 

just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************

System.IO.FileLoadException: Could not load file or assembly ‘Microsoft.Office.Interop.Excel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ or one of its dependencies. Invalid pointer (Exception from HRESULT: 0x80004003 (E_POINTER))

File name: ‘Microsoft.Office.Interop.Excel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ —> System.ArgumentNullException: Array cannot be null.

   at System.Reflection.Assembly.nLoadImage(Byte[] rawAssembly, Byte[] rawSymbolStore, Evidence evidence, StackCrawlMark& stackMark, Boolean fIntrospection)

   at System.Reflection.Assembly.Load(Byte[] rawAssembly)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.CurrentDomain_AssemblyResolve(Object sender, ResolveEventArgs args)

   at System.AppDomain.OnAssemblyResolveEvent(String assemblyFullName)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.createOutputFile(String excelFullFilename)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.saveProductInfo(AmazonProductInfo productInfo)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.processAmazonItem(String itemAsin)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.processAwsSearchItem(awsSearchResultItem singleAwsSearchItem)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.awsMainCategorySearch()

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.btnSearch_Click(Object sender, EventArgs e)

   at System.Windows.Forms.Control.OnClick(EventArgs e)

   at System.Windows.Forms.Button.OnClick(EventArgs e)

   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)

   at System.Windows.Forms.Control.WndProc(Message& m)

   at System.Windows.Forms.ButtonBase.WndProc(Message& m)

   at System.Windows.Forms.Button.WndProc(Message& m)

   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)

   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

************** Loaded Assemblies **************

mscorlib

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5466 (Win7SP1GDR.050727-5400)

    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll

—————————————-

ScrapeAmazonProduct

    Assembly Version: 1.0.0.0

    Win32 Version: 3.1.0.0

    CodeBase: file:///C:/Users/Eli%20Shimony/Desktop/ScrapeAmazonProduct_2013-06-17.exe

—————————————-

System.Windows.Forms

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5468 (Win7SP1GDR.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll

—————————————-

System

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5467 (Win7SP1GDR.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll

—————————————-

System.Drawing

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5467 (Win7SP1GDR.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll

—————————————-

System.Configuration

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll

—————————————-

System.Xml

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll

—————————————-

NLog

    Assembly Version: 2.0.0.0

    Win32 Version: 3.1.0.0

    CodeBase: file:///C:/Users/Eli%20Shimony/Desktop/ScrapeAmazonProduct_2013-06-17.exe

—————————————-

System.ServiceModel

    Assembly Version: 3.0.0.0

    Win32 Version: 3.0.4506.5452 (Win7SP1GDR.030729-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.ServiceModel/3.0.0.0__b77a5c561934e089/System.ServiceModel.dll

—————————————-

System.Data

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll

—————————————-

System.Core

    Assembly Version: 3.5.0.0

    Win32 Version: 3.5.30729.5420 built by: Win7SP1

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Core/3.5.0.0__b77a5c561934e089/System.Core.dll

—————————————-

System.Runtime.Serialization

    Assembly Version: 3.0.0.0

    Win32 Version: 3.0.4506.5452 (Win7SP1GDR.030729-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Runtime.Serialization/3.0.0.0__b77a5c561934e089/System.Runtime.Serialization.dll

—————————————-

System.Web

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5456 (Win7SP1GDR.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_32/System.Web/2.0.0.0__b03f5f7f11d50a3a/System.Web.dll

—————————————-

HtmlAgilityPack

    Assembly Version: 1.4.6.0

    Win32 Version: 3.1.0.0

    CodeBase: file:///C:/Users/Eli%20Shimony/Desktop/ScrapeAmazonProduct_2013-06-17.exe

—————————————-

System.Web.Extensions

    Assembly Version: 3.5.0.0

    Win32 Version: 3.5.30729.5446

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Web.Extensions/3.5.0.0__31bf3856ad364e35/System.Web.Extensions.dll

—————————————-

System.Web.Abstractions

    Assembly Version: 3.5.0.0

    Win32 Version: 3.5.30729.5420

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Web.Abstractions/3.5.0.0__31bf3856ad364e35/System.Web.Abstractions.dll

—————————————-

************** JIT Debugging **************

To enable just-in-time (JIT) debugging, the .config file for this

application or computer (machine.config) must have the

jitDebugging value set in the system.windows.forms section.

The application must also be compiled with debugging

enabled.

For example:

<configuration>

    <system.windows.forms jitDebugging="true" />

</configuration>

When JIT debugging is enabled, any unhandled exception

will be sent to the JIT debugger registered on the computer

rather than be handled by this dialog box.

【解决过程】

1. 后来得知,别人机子上,已经安装了Microsoft Excel了。

所以,按理来说,应该可以找到Microsoft.Office.Interop.Excel.dll才对。

2.但是自己想起了之前的一个事情:

关于svn的脚本更新,以支持word等对比的,其中脚本配置中就有:

C:\Program Files\TortoiseSVN\Diff-Scripts\diff-doc.js
// Microsoft Office versions for Microsoft Windows OS
var vOffice2000 = 9;
var vOffice2002 = 10;
var vOffice2003 = 11;
var vOffice2007 = 12;
var vOffice2010 = 14;
var vOffice2013 = 15;

所以,应该是:

Office2007是Office12

Office2010是Office14

结果还出错,所以就推断出来,估计是他机子上的Office的版本低了,而我此处引用的版本高了:

即,别人电脑上的office是Office12,而我此处的是Office14

所以同样都是:

Microsoft.Office.Interop.Excel

但是却找不到此dll。

3.所以,此处去改为version=12的:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Excel.dll

引用:

Office12 Microsoft.Office.Interop.Excel.dll

indeed reference office 12

然后的确就可以了。

4. 后来,为了,此处集成的dll,是内置集成的dll,所以,还是避免从vs安装目录中添加此office12的dll库,

而从自己当前项目,额外的添加自己的dll:

reference add excel dll file

然后再设置为embedded:

set excel dll to embedded resource

【总结】

C#的exe用到了excel的话,希望拿到别人的地方也正常运行不报错的话,

其中,别的地方:

  • 可能没装excel,
  • 也可能装了excel,但是版本低

那么要:

1。最好自己在exe中集成了对应的excel的dll库。

关于,集成dll到exe中,

简述:

(1)把dll加到自己的资源resource中

(2)把dll加到自己的项目中,且属性设置为 嵌入的资源(Embedded Resource)

(3)自己的类的初始化函数中,加上对应的load dll的相关代码

详见:

【已解决】C#中集成DLL库到自己的exe程序中

或者是:

2. 即使不集成excel的dll,在引用excel的dll

(1)也要尽量引用低版本

比如别人已装的excel,即Office是Office12

那么你也就不要去引用Office14==Office2010:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll

了,而去引用Office12==Office2007:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Excel.dll

(2)或者是和对方电脑中安装的excel的版本一致

当然,最好确认一下,对方的电脑中装了哪个版本的Office(的excel)

比如是Office 2010==Office14,那么你也就去引用对应的Office14:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll

就好了。

转载请注明:在路上 » 【已解决】虽然已经安装了Office的Excel但是C#的exe还是运行出错:Could not load file or assembly ‘Microsoft.Office.Interop.Excel, Version=14.0.0.0

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.174 seconds, using 22.17MB memory