Search code examples
c#exceloledb

Programmatically finding an Excel file's Excel version


I'm using an OleDbConnection to connect to a spreadsheet from a C# program. One of the parameters in the connection string is the Excel version.

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties="Excel 8.0;HDR=YES"

Given the path of an Excel file how can I find out which Excel format version it uses?

Thanks in advance,

T.


Solution

  • Download OLE File Property Reader. Register dsofile.dll with regsvr32 and add it as a reference in your application. The following code will output the type of Excel file. It will not work on xlsx/docx since those are not OLE compunds object files, but should work on all older Office formats (2007/2003/XP).

    var doc = new OleDocumentPropertiesClass();            
    doc.Open(@"c:\spreadhseet.xls", false, dsoFileOpenOptions.dsoOptionDefault);
    Console.WriteLine(doc.OleDocumentType);