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

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

问题描述

我的一个用户在尝试通过C#应用程序打开Excel文件时遇到问题。

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

当我从我的机器运行它时,一切都可以正常工作适用于其他用户。我不是Excel interop专家,希望你们可以帮助我。

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.

这是如何设置的:

我添加了对我的应用程序的引用到Microsoft.Office.Interop.Excel.dll,版本10.0.4504.0(我相信是Excel 2002)。在我的机器上我已经安装了Excel 2007.
在我的代码中,我尝试打开一个这样的工作表:

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);

我将用户版本记录为Excel 9.0(即2000)。用户获取的错误是:

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)

看起来工作簿无法打开。该文件已被确认存在,并刚刚松动在用户的PC上C:。我以为通过使用PIA我不用担心Excel版本的问题。我知道有其他用户使用Excel 2000,它可以在我的开发机器上工作。

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.

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

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

推荐答案


我以为通过使用PIA我不会
不得不担心Excel版本
问题,我知道有其他用户
使用Excel 2000,它适用于我的
dev机器。

"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.

通过针对Excel的PIA 2002年,您的程序将兼容所有版本的Excel 2002(10.0)及以上版本。然而,出现故障的机器正在运行Excel 2000(9.0),它是您开发的版本下的

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.

在Excel 2002下没有正式支持的PIA,所以如果你需要你的程序在Excel 2000(9.0)上运行,那么你将必须创建自己的定制互操作程序集。您可以使用TlbImp.exe为Excel 9.0创建互操作程序集,如文章实现与.NET Interop的向后兼容性:Excel作为案例研究

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.

如果您的程序集具有强名称,那么您需要创建一个强名互操作装配这可以通过/ keyfile开关提供.pfx或.snk文件的名称来完成,如文章如何创建主要互操作程序集(PIA)。该文章还使用/主开关来创建主互操作程序集。在你的情况下,使互操作组合不是真正需要的,但不会受到伤害。然而,文章省略的是使用/ sysarray开关,您应该使用它。

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.

制作一个强大的互操作程序集确实有一些复杂性要考虑,然而。有关详细信息,请阅读使用TLBIMP.exe创建强命名的互操作程序集。 (除此之外,这篇文章解释了对/ sysarray开关的需要。)

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天全站免登陆