尝试使用 Excel 2007 进行办公自动化,但继续使用 Excel 2003 [英] Trying to do Office Automation with Excel 2007, but keeps using Excel 2003

查看:17
本文介绍了尝试使用 Excel 2007 进行办公自动化,但继续使用 Excel 2003的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:

Windows XP 机器
Excel 2007 和 Excel 2003 均已安装(按此顺序,而不是按时间顺序).
C# 3.5

Windows XP machine
Both Excel 2007 and Excel 2003 installed (in that order, not chronologically).
C# 3.5

问题:

当我使用 PIA 进行一些 Office 自动化时,我使用以下代码行:

When I use the PIAs to do some Office automation, I use the following line of code:

var excel = new ApplicationClass();

PIA 的版本专门将其称为 Excel 12.
C:WINDOWSassemblyGACMicrosoft.Office.Interop.Excel12.0.0.0__71e9bce111e9429cMicrosoft.Office.Interop.Excel.dll
但是:

The PIA's version specifically refers to it as Excel 12.
C:WINDOWSassemblyGACMicrosoft.Office.Interop.Excel12.0.0.0__71e9bce111e9429cMicrosoft.Office.Interop.Excel.dll
But:

 excel.Version;//this is 11.0 instead of 12.0

因此,当我尝试打开扩展名为 .xlsx 的文件时,它会警告我文件转换中丢失的功能,并使用 excel 2003 打开它.我很确定它与 2007 年的安装顺序有关 -> 2003,但我无法在我的机器上卸载 2003 b/c 我们的网络服务器上有一些办公自动化,用于使用 excel 2003 的不相关项目.

Thus, when I try to open a file with extention .xlsx, it warns me about lost functionality in the file conversion, and opens it with excel 2003. I am pretty sure it has to do with the install order being 2007 -> 2003, but I can't uninstall 2003 on my machine b/c we have some office automation on our webserver for an unrelated project that uses excel 2003.

我查看了 Policy.11.0.Microsoft.Office.Interop.Excel.config 的东西,但它说

I've looked at the Policy.11.0.Microsoft.Office.Interop.Excel.config stuff, but it says

<bindingRedirect oldVersion="11.0.0.0" newVersion="12.0.0.0"></bindingRedirect>

所以,我很茫然.为什么我不能告诉 COM Interop 使用哪个版本的 Excel?

So, I am at a loss. Why can't I tell the COM Interop which version of Excel to use?

推荐答案

您无法以编程方式命令使用哪个版本的 Excel.PIA 仅指示您针对哪个接口或对象模型进行开发.但是实际运行的 Excel 版本是由注册表控制的.

You cannot programmatically command which version of Excel to use. The PIAs only dictate which interface, or object model, you are developing against. But which version of Excel is actually running is controlled by the registry.

然而,在运行 PIA 时,您实际上将针对安装在系统上的最高级别的 PIA 运行.因此,如果您针对 Excel 2003 PIA 进行开发,但客户端使用 Excel 2007 和 Excel 2007 PIA,则您的代码将针对 Excel 2007 PIA 运行——它应该可以正常运行,因为 Excel 2007 PIA 向后兼容.也就是说,每个更高编号的 PIA 版本(和 Excel 对象模型)都向后兼容针对旧 PIA 和旧 Excel 对象模型编译的命令.请注意,如果客户端在计算机上同时具有 Excel 2007 和 Excel 2003 PIA,则无论运行哪个版本的 Excel,都将加载更高版本的 PIA - 因此,如果两个 PIA 都可用,则 Excel 2007 PIA 将运行.

When it comes to running the PIAs, however, you will actually run against the highest-level PIA installed on the system. So if you develop against the Excel 2003 PIA, but the client has Excel 2007 with the Excel 2007 PIA, your code will run against the Excel 2007 PIA -- and it should run fine, because the Excel 2007 PIA is backward compatible. That is, each higher-numbered PIA version (and Excel object model) is backward compatible to commands compiled against an older PIA and older Excel object model. Note that if the client had both the Excel 2007 and Excel 2003 PIAs on the machine, then the higher versioned PIA would load, regardless of which version of Excel is running - so the Excel 2007 PIA would run, if both PIAs were available.

好的,因此您对 PIA 没有太多控制权,因为您开发的 PIA 实际上并不控制哪个 PIA 将实际在客户端计算机上运行.

Ok, so you don't have a lot of control over the PIAs because the PIA that you developed against does not actually control which PIA will actually be running on the client machine.

对于启动哪个版本的 Excel,您也没有太多控制权.例如,当您通过以下方式创建新的 Excel 实例时:

You don't have a lot of control over which version of Excel is launched either. For example, when you create a new Excel instance via:

Excel.Application excelApp = new Application();

加载的 Excel 应用程序根据注册表中设置的当前版本进行设置.当前版本保存在:

The Excel application loaded is set according to the current version set in the registry. The current version is saved at:

HKEY_CLASSES_ROOTExcel.ApplicationCurVer

在您的案例中,CurVer"键的默认值似乎是Excel.Application.11",而不是Excel.Application.12".单独更改它可能会解决问题,但我更愿意进行修复以确保正确更正所有注册表设置.(而且我不可能知道所有设置应该是什么.)好的,我刚刚找到了另一个:您还需要更改:

It looks like the 'CurVer' key in your case will have a default value of 'Excel.Application.11', instead of 'Excel.Application.12'. Changing this alone might do the trick, but I would prefer to do a repair instead to make sure that all the registry settings are corrected properly. (And I couldn't possibly know what all the settings should be.) Ok, I just found another one: you would also need to change:

[HKEY_CLASSES_ROOTCLSID{00024500-0000-0000-C000-000000000046}ProgID]

保存Excel.Application.12"的值.但我强烈建议改为进行修复.我不知道可能需要更改哪些其他设置,因此手动更改它们有点冒险.

to hold a value of "Excel.Application.12". But I would strongly recommend running a repair instead. I don't know what other settings might need to be changed, so changing them by hand is a bit risky.

此外,您还应该找到以下键:

In addition, you should find the following keys as well:

HKEY_CLASSES_ROOTExcel.Application.11
HKEY_CLASSES_ROOTExcel.Application.12

因为这些是您安装的 Excel 版本.

Because these are the versions of Excel that you have installed.

(有关进一步讨论,请参阅此处.)

(See here for a further discussion.)

我很确定这与安装顺序为 2007 -> 2003

I am pretty sure it has to do with the install order being 2007 -> 2003

是的,这是 100% 正确的.您可以尝试在 Excel 2007 上运行修复,这将是最简单的操作.如果这不起作用,那么我将卸载它们,然后重新安装它们.我将卸载 Excel 2003,然后卸载 2007(颠倒安装它们的顺序),然​​后安装 Excel 2003,然后安装 Excel 2007,这样您就可以按照正确的顺序安装这两个版本.

Yes, this is 100% correct. You could try running a repair on Excel 2007, this would be the easiest thing to do. If this does not work, then I would uninstall both and then re-install them both. I would uninstall Excel 2003 and then uninstall 2007 (reversing the order in which you installed them) and then install Excel 2003 and then install Excel 2007 so that you are installing both versions in the correct order.

但请记住,通过这样做,Excel 2007 将在您调用 Excel.Application excelApp = new Application() 时默认运行.

But keep in mind that by doing this, Excel 2007 will be run by default when you call Excel.Application excelApp = new Application().

实际推荐的做法是让两个版本的 Excel 都在开发人员的机器上运行.有关这方面的更多信息,请参阅:

The actual recommended practice is not to have both versions of Excel running on the developer's machine. For more on this, see:

我曾经在我的同一台开发机器上拥有多个版本的 Excel,我个人觉得缺点并不像这些文章所说的那么复杂.通常,Excel 2007 PIA 向后兼容 Excel 2003 PIA,并且一切正常.但是我曾经陷入与您类似的注册表混乱并决定做正确的事".我都卸载了,然后只重新安装了 Excel 2007.

I used to have multiple versions of Excel on my same development machine, and I personally felt that the downsides were not as complicated as these articles make it sound. In general, the Excel 2007 PIA is backward-compatible to the Excel 2003 PIA and everything works fine. But I once got into a registry mess similar to yours and decided to "do the right thing". I uninstalled both and then only re-installed Excel 2007.

从那里我安装了免费的 Virtual PC(VM ware 实际上要好一点,但它不是免费的),然后在单独的 VM 上安装了用于 2003、2002、2000 和 '97 的较低版本的 Excel.设置肯定是一些工作,但是一旦你这样做了,一切都是 100% 干净的.

From there I installed Virtual PC, which is free (VM ware is actually a little better, but it's not free) and then installed my lower versions of Excel for 2003, 2002, 2000, and '97 on separate VMs. It is definitely some work to set up, but once you do this, everything is 100% clean.

也就是说,我可能不想真正开发虚拟机上的低版本 Excel,使用虚拟机中托管的 Visual Studio 太难了.因此,这些 VM 仅适用于测试部署,以确保您的系统可以针对各种客户端配置工作.有意义吗?

That said, I probably would not want to actually develop against lower-versions of Excel on a VM, it would be too hard to use Visual Studio hosted within a VM. So these VMs are only good for testing deployment to make sure that your system can work against various client configurations. Make sense?

希望这会有所帮助!

迈克

这篇关于尝试使用 Excel 2007 进行办公自动化,但继续使用 Excel 2003的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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