Automatically retrieve Excel library version

Is there a way to programmatically get the version of Excel Interop libraries used with C #?

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

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

I ask this because it dictates the file extension when saving the book, and if you save the 2007 book with ".xls", 2007 complains about the wrong extension.

+3
source share
3 answers

: , PIA . , Excel . Excel PIA, , , , Excel, PIA.

, Excel , , , , Excel.Application.Version , "11.0" "9.0a" , . , ( "." ) , , :

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

PIA vs. Excel, (PIA) , PIA , . , PIA Excel 2002, Excel 2007, ( ) PIA Excel 2007. , PIA , , .

, ( ) , , Excel 2007, , PIA Excel 2003. , Excel 2007 , PIA Excel 2003. : PIA Excel 2007, , , - Excel 2003 - Excel 2003 , PIA Excel 2007.

. , , (1) Excel, (2) Excel , PIA ( , , PIA , ).

. Andrew Whitechapel VS Office?

, Excel , , 100% . , Excel PIA, Excel , . , , , , Excel . , PIA, - PIA, ( ), , PIA .

: phsr:

, , PIA , . PIA, ( "xls" vs "xlsx" ). 2007 "xls", . . , ( ), (, ).

, "" , PIA, Excel, , , PIA. , 99,9% , Excel PIA. , . , , Excel, PIA ( PIA , PIA, ).

, PIA , . Excel 2003 SaveAs ​​ , Excel 2007 SaveAs , PIA . , SaveAs, , Excel .

, :

(1) , Excel, . , , . , , , .

(2) , . Excel . , ".xls" Excel 2007, , . , Excel ".xls" ".xlsx" , , Excel :

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);

"MyWorkbook.xls" "MyWorkbook.xlsx", , Excel . , Excel , .

+7

, Windows . .

:

+2

Try this (source code from ExcelDna):

Put this code somewhere in the class declaration:

[DllImport("XLCALL32.DLL")]
public static extern int XLCallVer();

Call this function where you need to:

int version = XLCallVer() / 256;

Or, if you use ExcelDna, you can call directry:

double version = ExcelDnaUtil.ExcelVersion;
0
source

Source: https://habr.com/ru/post/1716484/


All Articles