C#和Excel互操作 [英] C# and Excel interop

查看:218
本文介绍了C#和Excel互操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个网友是有试图打开一个Excel通过我的C#应用​​程序文件时的问题。

一切工作正常,当我从我的机器上运行它,它适用于其他用户。我不是Excel的互操作专家,所以希望你们能帮助我。

下面是如何设置:

我添加了一个参考,以我的应用程序Microsoft.Office.Interop.Excel.dll,版本10.0.4504.0(我认为这是Excel 2002中)。在我的机器我已经安装了Excel 2007中。
在我的code我尝试打开一个工作表,像这样:

 使用Microsoft.Office.Interop
...
Microsoft.Office.Interop.Excel.ApplicationClass _excelApp =新Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = _excelApp.Workbooks.Open(workbookPath,0,真实,5,,,真实,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,假的,假0,真的,假的,假的);
Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet excelWorksheet =(Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(1);

我登录的用户版本为Excel 9.0(即2000年)。用户得到的错误是:

 异常='System.AccessViolationException:尝试读取或写入受保护的内存。这通常是指示其他内存已损坏。
   在Microsoft.Office.Interop.Excel.Workbooks.Open(字符串文件名,对象UpdateLinks,只读对象,对象格式,对象口令,对象WriteResPassword,对象IgnoreReadOnlyRecommended,原产地对象,对象分隔符,可编辑的对象,对象通知,目标转换器,对象AddToMru,本地对象,对象CorruptLoad)

它看起来像簿无法打开。该文件被证实存在,是用户的位于C PC上只是松散的:。我想通过使用PIA我也不会担心Excel版本问题。我知道有其他用户使用Excel 2000,和它的作品我的机器上。

任何想法?也许我的呼叫打开Excel文件应该改变?不幸的是,我无法重现它。


解决方案

  

我想通过使用PIA我不会
  不用担心Excel版本
  的问题。我知道有其他用户
  使用Excel 2000,和它的作品在我的
  开发机。


几乎真实的,但并不完全。

通过对PIA的Excel 2002的开发,你的程序将是(10.0)及以上的Excel 2002的所有版本兼容。发生故障的机器,但是,与Excel 2000(9.0),低于的版本的运行,您已经对开发的版本。

有低于Excel 2002中没有正式支持的PIA,所以如果你需要你的程序上的Excel 2000(9.0)上运行,那么你必须创建自己的自定义互操作程序集。您可以使用TLBIMP.EXE为Excel创建9.0互操作程序集,如在文章解释实现向后兼容.NET互操作:Excel作为案例

如果你的程序集是强命名,那么你需要创建一个强命名的互操作程序集。这可以通过通过/密钥文件交换机,提供您的.pfx或.snk文件的名称来完成,如在文章演示了如何创建主Interop大会(PIA)。为了创造一个主互操作程序集这篇文章也正在使用/主开关。使得互操作程序集小学是不是真的需要你的情况,但没有受伤。文章省略了什么,但是,是采用了/ sysarray开关,你应该使用的。

制作一个强命名的互操作程序集确实有一定的复杂性来考虑,但是。对于一些详细信息,有<一读href=\"http://www.darinhiggins.com/2009/04/14/UsingTLBIMPexeToCreateStrongNamedInteropAssemblies.aspx\">Using TLBIMP.EXE创建强名称Interop程序集。 (除其他事项外,这篇文章解释了/ sysarray交换机的需求。)

在短,使得自定义Interop程序很简单,如果你的程序集不强命名。这是比较复杂的,如果你的程序集是强命名,因此,你需要创建一个强命名的互操作程序集。但希望这些文章应该让你去。

迈克 -

One of my users is having an issue when trying to open an Excel file through my C# app.

Everything works ok when I run it from my machine and it works for other users. I am no Excel interop expert so hopefully you guys can help me out.

Here is how it is set up:

I added a reference to my app to Microsoft.Office.Interop.Excel.dll, version 10.0.4504.0 (which I believe is Excel 2002). On my machine I have installed Excel 2007. In my code I try to open a worksheet like so:

using Microsoft.Office.Interop
...
Microsoft.Office.Interop.Excel.ApplicationClass _excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = _excelApp.Workbooks.Open(workbookPath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", false, false, 0, true, false, false);
Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(1);

I logged the users version as Excel 9.0 (which is 2000). The error that the user gets is:

Exception='System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)

It looks like the workbook cannot be opened. The file was confirmed to exist and was just loose on the user's PC at C:. I thought by using the PIA I wouldn't have to worry about Excel version issues. I know there are other users using Excel 2000, and it works on my development machine.

Any ideas? Maybe my calls to open the Excel file should be changed? The unfortunate thing is that I am unable to reproduce it.

解决方案

"I thought by using the PIA I wouldn't have to worry about Excel version issues. I know there are other users using Excel 2000, and it works on my dev machine."

Almost true, but not quite.

By developing against the PIA for Excel 2002, your program will be compatible for all versions of Excel 2002 (10.0) and above. The failing machine, however, is running with Excel 2000 (9.0), which is a version below the version that you have developed against.

There is no officially supported PIA below Excel 2002, so if you need your program to run on Excel 2000 (9.0) then you will have to create your own custom interop assembly. You can use TlbImp.exe to create an interop assembly for Excel 9.0, as explained in the article Achieving Backward Compatibility with .NET Interop: Excel as Case Study.

If your assembly is strong-named, then you need to create a strong-named interop assembly. This can be done by providing the name of your .pfx or .snk file via the /keyfile switch, as demonstrated in the article How to create a Primary Interop Assembly (PIA). That article is also making use of the /primary switch in order to create a "primary interop assembly". Making the interop assembly primary is not really needed in your case, but does not hurt. What the article omits, however, is the use of the /sysarray switch, which you should use.

Making a strong-named interop assembly does have some complexities to consider, however. For some more details, have a read of Using TLBIMP.exe to create Strong Named Interop Assemblies. (Among other things, this article explains the need for the /sysarray switch.)

In short, making a custom interop assembly is very simple if your assembly is not strong named. It is more complicated if your assembly is strong-named and, therefore, you need to create a strong-named interop assembly. But hopefully these articles should get you going.

-- Mike

这篇关于C#和Excel互操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆