简体   繁体   English

支持Office Automation的不同Office版本

[英]support different Office versions with Office Automation

We created an application that uses Office 2007 (Excel 2007) to read in data from an Excel worksheet. 我们创建了一个使用Office 2007(Excel 2007)从Excel工作表读取数据的应用程序。 However. 然而。 I noticed that when I want to deploy the application on a system with Office 2003 installed, it crashes because other PIA's (and other dll's) need to be referenced for this version of office. 我注意到,当我想在安装了Office 2003的系统上部署应用程序时,它会崩溃,因为此版本的办公室需要引用其他PIA(和其他dll)。

Do I need to compile different versions of my application to be able to support different versions of Office or is there a more elegant solution for this problem? 我是否需要编译不同版本的应用程序才能支持不同版本的Office或者是否有更优雅的解决方案来解决此问题?

I use Visual Studio 2010 (C#) and the .Net 4.0 platform. 我使用Visual Studio 2010(C#)和.Net 4.0平台。

As you're using .NET 4, you can use "embedded" PIAs (aka "No PIA"). 当您使用.NET 4时,您可以使用“嵌入式”PIA(又名“No PIA”)。 Change the option on the Office reference so that "Embed Interop Types" is True. 更改Office引用上的选项,以便“嵌入互操作类型”为True。

So long as you only use features of Office which are supported on the machine you deploy on, you should be fine. 只要您只使用部署的计算机上支持的Office功能,您应该没问题。

It also means that: - You don't need to worry about the PIA itself not being present on the target machine - Any methods or properties which are of the VARIANT type in the original COM interface are now represented using dynamic in your code, which can make your life simpler 它还意味着: - 您不必担心PIA本身不存在于目标机器上 - 原始COM接口中任何VARIANT类型的方法或属性现在都使用dynamic代码表示,可以让你的生活更简单

Depending on whether you have any other requirements (you say reading data from excel), you can use the OLEDB driver to connect to the excel file and query it in a SQL manner. 根据您是否有任何其他要求(您说从Excel读取数据),您可以使用OLEDB驱动程序连接到Excel文件并以SQL方式查询它。

Example: http://codehill.com/2009/01/reading-excel-2003-and-2007-files-using-oledb/ 示例: http//codehill.com/2009/01/reading-excel-2003-and-2007-files-using-oledb/

您可以参考PIA的多个版本的办公室,并在运行时找出从哪个路由您的电话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM