如何在c#中检查Excel文件的版本?

时间:2022-01-17 07:01:15

I need to differentiate that a selected file is created with Excel 2010 or Excel 2013 version of a selected Excel file and Excel application on server must match in order to continue.

我需要区分,选择的文件是使用Excel 2010或Excel 2013版本创建的,选择的Excel文件和服务器上的Excel应用程序必须匹配才能继续。

I can get the server's Excel application version:

我可以得到服务器的Excel应用程序版本:

 xApp = new Microsoft.Office.Interop.Excel.Application();
// getting version of Server's Excel Application
string versionName = xApp.Version;
int length = versionName.IndexOf('.');
versionName = versionName.Substring(0, length);
object missing = Type.Missing;
object trueObject = true;
xApp.Visible = false;
xApp.DisplayAlerts = false;
xWorkBook = 
xApp.Workbooks.Open(ExcelFilePath, missing, trueObject, 
                    missing, missing, missing,
                    missing, missing, missing,
                    missing, missing, missing, missing, missing, missing);

But how can I get the Excel version of opened Excel files? In the current case, versionName returns 14.0, which is for Office 2010.

但是如何获得已打开的Excel文件的Excel版本呢?在当前的情况下,versionName返回14.0,也就是Office 2010。

3 个解决方案

#1


1  

For .xls files you need to decode the "BIFF" value, from MSDN: How To Determine the Version of a Microsoft Excel Workbook:

对于.xls文件,您需要解码来自MSDN的“BIFF”值:如何确定Microsoft Excel工作簿的版本:

Microsoft Excel saves data using structured storage. In particular, it creates a data stream called "Workbook" (previously just "Book") where it saves the contents starting with a BOF (beginning of file) record.

Microsoft Excel使用结构化存储保存数据。特别是,它创建了一个名为“Workbook”(以前只是“Book”)的数据流,在其中以BOF(文件开头)记录开始保存内容。

There's also some code in that article but I'm unsure how much I can lift from it without breaching copyright.

这篇文章中也有一些代码,但我不确定我能从它那里得到多大的提升而不侵犯版权。

That format is used up to Excel 2002 according to a table at the bottom of that article.

根据该条底部的表格,该格式被用到了Excel 2002。

As for .xlsx files, you can use any zip file library to open it, then open the docProps\app.xml file, and inside you'll find this:

对于.xlsx文件,您可以使用任何zip文件库来打开它,然后打开docProps应用程序。xml文件,在里面你会发现:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns=...>
    ....
    <AppVersion>15.0300</AppVersion>
</Properties>

This will tell you the version of Excel that saved that particular file.

这将告诉您保存该文件的Excel版本。

Also note that Excel will determine whether to use new storage loading or old storage loading depending on the contents of the file and not only on the extension, as such you can store an old-style file with a .xlsx extension and a new-style file with a .xls extension and Excel will still open it, and so should you. Excel will give a warning though, how you handle the discrepancy is up to you.

还要注意,Excel将决定是否使用新的存储加载或旧存储加载不仅取决于文件的内容和扩展,这样你可以储存一个旧式文件.xlsx扩展和新型具有xls扩展名的文件和Excel仍然会打开它,所以你应该。Excel会给出一个警告,如何处理差异取决于你自己。

#2


1  

found a simple solution with OLE File Property Reader

找到一个简单的解决方案与OLE文件属性阅读器

var doc = new OleDocumentPropertiesClass();
doc.Open(ExcelFilePath, false, dsoFileOpenOptions.dsoOptionDefault);
string DocFileVersion = doc.SummaryProperties.Version.ToString();

this will return version of Application Version of excel file...

这将返回excel文件的应用程序版本…

#3


-1  

There is nothing wrong in code, you can have switch case to detect what version of file it is.

在代码中没有任何错误,您可以使用switch case来检测它是什么版本的文件。

Excel 95 (v7.0)
Excel 97 (v8.0)
Excel 2000 (v9.0)
Excel 2002 (v10.0)
Excel 2003 (v11.0)
Excel 2007 (v12.0)
Excel 2010 (v14.0)
Excel 2013 (v15.0)

#1


1  

For .xls files you need to decode the "BIFF" value, from MSDN: How To Determine the Version of a Microsoft Excel Workbook:

对于.xls文件,您需要解码来自MSDN的“BIFF”值:如何确定Microsoft Excel工作簿的版本:

Microsoft Excel saves data using structured storage. In particular, it creates a data stream called "Workbook" (previously just "Book") where it saves the contents starting with a BOF (beginning of file) record.

Microsoft Excel使用结构化存储保存数据。特别是,它创建了一个名为“Workbook”(以前只是“Book”)的数据流,在其中以BOF(文件开头)记录开始保存内容。

There's also some code in that article but I'm unsure how much I can lift from it without breaching copyright.

这篇文章中也有一些代码,但我不确定我能从它那里得到多大的提升而不侵犯版权。

That format is used up to Excel 2002 according to a table at the bottom of that article.

根据该条底部的表格,该格式被用到了Excel 2002。

As for .xlsx files, you can use any zip file library to open it, then open the docProps\app.xml file, and inside you'll find this:

对于.xlsx文件,您可以使用任何zip文件库来打开它,然后打开docProps应用程序。xml文件,在里面你会发现:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns=...>
    ....
    <AppVersion>15.0300</AppVersion>
</Properties>

This will tell you the version of Excel that saved that particular file.

这将告诉您保存该文件的Excel版本。

Also note that Excel will determine whether to use new storage loading or old storage loading depending on the contents of the file and not only on the extension, as such you can store an old-style file with a .xlsx extension and a new-style file with a .xls extension and Excel will still open it, and so should you. Excel will give a warning though, how you handle the discrepancy is up to you.

还要注意,Excel将决定是否使用新的存储加载或旧存储加载不仅取决于文件的内容和扩展,这样你可以储存一个旧式文件.xlsx扩展和新型具有xls扩展名的文件和Excel仍然会打开它,所以你应该。Excel会给出一个警告,如何处理差异取决于你自己。

#2


1  

found a simple solution with OLE File Property Reader

找到一个简单的解决方案与OLE文件属性阅读器

var doc = new OleDocumentPropertiesClass();
doc.Open(ExcelFilePath, false, dsoFileOpenOptions.dsoOptionDefault);
string DocFileVersion = doc.SummaryProperties.Version.ToString();

this will return version of Application Version of excel file...

这将返回excel文件的应用程序版本…

#3


-1  

There is nothing wrong in code, you can have switch case to detect what version of file it is.

在代码中没有任何错误,您可以使用switch case来检测它是什么版本的文件。

Excel 95 (v7.0)
Excel 97 (v8.0)
Excel 2000 (v9.0)
Excel 2002 (v10.0)
Excel 2003 (v11.0)
Excel 2007 (v12.0)
Excel 2010 (v14.0)
Excel 2013 (v15.0)