检索图书馆的Excel编程的版本 [英] Retrieving the Version of the Excel Library Programmatically

查看:93
本文介绍了检索图书馆的Excel编程的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法以编程方式检索的Excel互操作库的版本正在使用的C#?



我知道我大概可以在注册表中查找要弄清楚的Microsoft Office的安装实例,但我很好奇,如果Excel库包含此信息。



我知道这个信息包含在Visual Studio中,当您引用库,但我看不到在运行时的信息。



我问这个,因为当你保存工作簿决定了文件的扩展名,如果你保存2007工作簿的.xls,2007年抱怨的延伸部分。不正确


解决方案

简短的回答是这样的:你不需要知道哪个版本PIA装载解决您的问题。你想知道的是哪个版本的Excel实际运行。虽然运行Excel版本和PIA版本是<青霉>通常的是相同的,它们不必是,并且,在这种情况下,它是你所关心的,而不是PIA版本的Excel版本。



确定Excel版本运行这并不难,但它是不是很容易,因为它应该是因为 Excel.Application.Version 属性可以返回一个字符串,如11.0或9.0A或类似的,因此可以不必直接解析成一个整数值。你可以依靠的事实,一切小数点(。)的左边是版本号,但是,所以下面的代码适用于所有版本:

  Excel.Application excelApp =新Excel.Application(); 

字符串=的versionName excelApp.Version;
INT长度= versionName.IndexOf('。');
=的versionName versionName.Substring(0,长度);

// int.parse需要使用美国文化来完成。
INT的versionNumber = int.Parse(的versionName,CultureInfo.GetCultureInfo(EN-US));

如果(的versionNumber> = 12)
{
// Excel 2007或以上。
}
,否则
{
// Excel 2003或以下。
}



对于PIA与Excel对象模型的问题,这主Interop大会( PIA)开发与该PIA实际被加载到目标机器上运行时,当你集的引用可以是不同的。例如,如果您引用的Excel 2002 PIA的和目标机器使用Excel 2007,然后(通常情况下)在Excel 2007中的PIA将被装载。规则是在目标机器上可用的版本化最高处PIA都在运行时加载。



它变得更加复杂,因为它是可能的(虽然肯定不会可能)为目标机器有,比如说,Excel 2007中加载的目标机器上,但最高可PIA的是Excel 2003中。在这种情况下,Excel 2007中将加载,但您的代码将工作对Excel 2003中的PIA 。相反也可能发生:在Excel 2007中的PIA可用,但安装在机器上的最高实际版本是Excel 2003中 - 在这种情况下,Excel 2003中会加载,但您的代码将工作对Excel 2007中的PIA

这些情景是在一个标准的设置不太可能。它是最有可能的显影剂的机器上发生,其中任一(1)一个以上的Excel版本存在于机器上的同时,或(2)Excel版本已经被添加和删除,但PIA的不与它去除(其中,本身也是不可能的,因为我相信PIA的自动卸载,但我可能是错误的这一点)。



有关更多相关信息,请参阅安德鲁白教堂的文章的为什么VS开发与多个版本的Office支持?



尽管所有这些场景听起来有点吓人,在接口Excel的对象模型是非常一致的,因此,近100%的向后兼容。一般来说,如果你绑定到特定的Excel PIA版本,那么你的代码将成功地针对该版本的Excel或更高版本上运行,几乎不论所涉及的scenio的。有一些怪癖不过,因为你已经发现,但关键是要知道哪个版本的Excel实际运行。这PIA运行会心一般并不重要 - 只要PIA版本对(或更高版本),你开发的是可用的,那么PIA本身应该不会造成任何问题。



编辑:跟进到phsr的评论:




当我保存工作簿我
生成,我需要知道什么PIA我
对我的工作,因为这将被保存在该格式
。如果我得到PIA的
版本,我可以给
文件名以正确的扩展名(XLS
与XLSX)。即使2007年的工作簿保存为XLS
,它会提供有关正在以不同的格式
不是由文件扩展名指定的
警告。
我想隐藏使用正确的扩展名由
的警告。您的
点是正确的,因为它确实
到底不要紧(文件
将仍然打开),但在一些
实例(像这样),它




我知道,它可能会感觉这是PIA是这样的问题,但它是Excel对象模型,该模型实际加载的事项在这里,而不是PIA。另外,有99.9%的机会,Excel应用程序的版本号是相同的PIA的版本。这是极为罕见的,他们会有所不同。而且,他们也有所不同,这将是Excel版本的事项,而不是PIA(只要PIA是相同的版本PIA您免受开发,或更高)。



要试图澄清这一点,只PIA的指定接口参与,而不是功能。该 Excel 2003中另存为方法具有完全相同的参数签名的 Excel 2007中SaveAs方法,所以PIA的这里不相同。如何执行另存为方法,但是,取决于该Excel对象模型版本的实际运行



要解决您的问题,我可以建议采取的行动的两种可能的课程之一:



(1)请我上面给以确定代码正在运行的Excel版本。如果你这样做,并调整相应的代码,它会工作,我答应。如果失败,显示你的代码,我敢肯定,我们可以得到它的工作。



(2)不要指定您给工作簿的名称扩展它。让Excel应用程序添加默认的扩展程序。你是在正确的,如果您在Excel 2007中保存时指定一个的.xls扩展,将尊重该扩展,但不是工作簿的版本。这里最容易不过,保存工作簿时,和Excel应用程序将是.xls或的.xlsx自动追加到工作簿名称,具体取决于Excel版本正在运行,以简单的省略扩展名:

  Excel.Application excelApp =新Excel.Application(); 
Excel.Workbook工作簿= excelApp.Workbooks.Add(Type.Missing);

workbook.SaveAs(
MyWorkbook,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing);



的上述的结果是一个命名为MyWorkbook.xls或MyWorkbook.xlsx簿,取决于哪个版本的Excel正在运行。总之,让Excel为你做的工作,这样你就不用担心了。


Is there a way to programatically retrieve the version of the Excel Interop Libraries being utilized with C#?

I know I could probably look in the registry to figure out the installed instance of Microsoft Office, but I was curious if the Excel Libraries contain this information.

I know this information is contained in Visual Studio when you reference the library, but I can't see that information at runtime.

I am asking this because it dictates the file extension when you save the workbook, and if you save a 2007 workbook with '.xls', 2007 complains about the extension being incorrect.

解决方案

The short answer is this: you do not need to know which PIA version is loaded to solve your problem. What you want to know is which version of Excel is actually running. Although the running Excel version and the PIA version are usually the same, they do not have to be, and, in this case, it is the Excel version that you care about, not the PIA version.

Determining which Excel version is running is not hard, but it is not quite as easy as it should be because the Excel.Application.Version property can return strings such as "11.0" or "9.0a" or the like, so one cannot necessarily directly parse it into an integer value. You can rely on the fact that everything to the left of the decimal point (".") is the version number, however, so the following code works for all versions:

Excel.Application excelApp = new Excel.Application();

string versionName = excelApp.Version;
int length = versionName.IndexOf('.');
versionName = versionName.Substring(0, length);

// int.parse needs to be done using US Culture.
int versionNumber = int.Parse(versionName, CultureInfo.GetCultureInfo("en-US"));

if (versionNumber >= 12)
{
    // Excel 2007 or above.
}
else
{
    // Excel 2003 or below.
}

As for the PIA vs. Excel object model issue, which Primary Interop Assembly (PIA) your assembly references when developing versus which PIA is actually loaded at runtime on the target machine can be different. For example, if you reference the Excel 2002 PIAs and the target machine is using Excel 2007, then (generally) the Excel 2007 PIAs will get loaded. The rule is that the highest-versioned Office PIA available on the target machine gets loaded at runtime.

It gets even more complicated in that it is possible (although, definitely not likely) for the target machine to have, say, Excel 2007 loaded on the target machine, but the highest-available PIAs be for Excel 2003. In this case, Excel 2007 would load, but your code would be working against the Excel 2003 PIAs. The reverse can occur as well: the Excel 2007 PIAs are available, but the highest actual version installed on the machine is Excel 2003 -- in this case Excel 2003 will load, but your code will be working against the Excel 2007 PIAs.

These scenarios are very unlikely in a standard setup. It is most likely to occur on a developer's machine where either (1) more than one Excel version is present on the machine at the same time, or (2) Excel versions have been added and removed, but the PIAs not removed with it (which, in itself, is also unlikely, as I believe the PIAs are uninstalled automatically, but I could be wrong about this).

For more on this, see Andrew Whitechapel's article Why is VS development not supported with multiple versions of Office?

While all these scenarios sound a bit scary, the interfaces in Excel's object model are extremely consistent and therefore are nearly 100% backward-compatible. In general, if you bind to a given Excel PIA version, then your code will run successfully against that version of Excel or higher, pretty much regardless of the scenio involved. There are a few quirks though, as you have discovered, but the key is to know which Excel version is actually running. Knowing which PIA is running is not generally important -- as long as the PIA version you developed against (or higher) is available, then the PIA itself should not cause any issues.

Edit: Follow up to phsr's comment:

When I am saving the Workbook I generate, I do need to know what PIA I am working against, because it will be saved in that format. If I get the version of the PIA, I can give the file name to correct extension ("xls" vs "xlsx"). Even if a 2007 workbook is saved as 'xls', it will give the warning about being a different format than specified by the file extension. I would like to hide that warning by using the correct extension. Your point is correct in that it really doesn't matter in the end (the file will still open) but at in some instances (like this one) it does.

I know that it might "feel like" it is the PIA that is the issue, but it is Excel object model that is actually loaded that matters here, not the PIA. In addition, there is a 99.9% chance that the Excel application version number is the SAME as the PIA version. It is extremely rare that they would differ. And where they do differ, it would be the Excel version that matters, not the PIA (so long as the PIA is the same version as the PIA you developed against, or higher).

To try to clarify this, the PIAs only specify the interfaces involved, not the functionality. The Excel 2003 SaveAs method has exactly the same parameter signature as the Excel 2007 SaveAs method, so the PIAs do not differ here. How the SaveAs method is executed, however, depends on which version of the Excel object model is actually running.

To fix your problem, I can suggest taking one of two possible courses of action:

(1) Try the code I gave above to determine the Excel version that is running. If you do this and adjust your code accordingly it will work, I promise. If it fails, show your code and I'm sure we can get it to work.

(2) Don't specify the extension in the workbook name that you give it. Let the Excel application add the default extension for you. You are correct in that if you specify a ".xls" extension when saving in Excel 2007, it will respect that extension, but not the workbook version. Easiest here, however, is to simply omit the extension when saving your workbook and the Excel application will automatically append ".xls" or ".xlsx" to the workbook name, depending on which Excel version is currently running:

Excel.Application excelApp = new Excel.Application();    
Excel.Workbook workbook = excelApp.Workbooks.Add(Type.Missing);

workbook.SaveAs(
    "MyWorkbook", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
     Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, 
     Type.Missing, Type.Missing);

The result of the above is a workbook named either "MyWorkbook.xls" or "MyWorkbook.xlsx", depending on which version of Excel is running. In short, let Excel do the work for you so that you don't have to worry about it.

这篇关于检索图书馆的Excel编程的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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