本文介绍了尝试使用Excel 2007执行Office Automation,但仍保持使用Excel 2003的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:



Windows XP计算机

Excel 2007和Excel 2003都安装

C#3.5



问题:



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

  var excel = new ApplicationClass ); 

PIA的版本特指它为Excel 12.

C: \\ WINDOWS\assembly\GAC\Microsoft.Office.Interop.Excel\12.0.0.071e9bce111e9429c\Microsoft.Office.Interop.Excel.dll

但是:

  excel.Version; //这是11.0而不是12.0 

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



我查看了Policy.11.0.Microsoft.Office.Interop.Excel.config的内容,但它表示

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

所以,我有点失落。为什么我不能告诉COM Interop哪个版本的Excel使用?

解决方案

您不能以编程方式命令使用哪个版本的Excel。 PIA只指示正在开发的接口或对象模型。但是哪个版本的Excel实际上是由注册表控制的。



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



好,所以你没有很多控制PIA,因为您开发的PIA实际上不控制哪个PIA实际上将在客户端机器上运行。



您没有很多控制也将启动其版本的Excel。例如,通过以下方式创建新的Excel实例时:

  Excel.Application excelApp = new Application(); 

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

  HKEY_CLASSES_ROOT\Excel.Application\CurVer 

看起来您的案例中的CurVer键的默认值为Excel.Application.11,而不是Excel.Application .12'。单独更改这可能会做的伎俩,但我宁愿做一个修复,以确保所有的注册表设置正确修正。 (我不知道所有的设置应该是什么。)好的,我只是发现了另一个:你还需要改变:

  [HKEY_CLASSES_ROOT\CLSID\ {00024500-0000-0000-C000-000000000046} \ProgID] 

保存值Excel.Application.12。但我强烈建议运行修复。我不知道什么其他设置可能需要更改,所以手动更改有点冒险。



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

  HKEY_CLASSES_ROOT\Excel.Application.11 
HKEY_CLASSES_ROOT\Excel.Application.12

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



(请参阅以进一步讨论。)

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.

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

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

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.

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.

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?

Hope this helps!

Mike

这篇关于尝试使用Excel 2007执行Office Automation,但仍保持使用Excel 2003的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:04