How can I check the version of Excel files in C#?

╄→尐↘猪︶ㄣ 提交于 2019-12-02 13:03:13

问题


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.

I can get the server's Excel application version:

 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.


回答1:


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

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.

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.

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:

<?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.

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.




回答2:


found a simple solution with OLE File Property Reader

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...




回答3:


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

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)


来源:https://stackoverflow.com/questions/27834386/how-can-i-check-the-version-of-excel-files-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!