views:

128

answers:

1

I have both 2003 and 2007 Excel versions installed on my machine. The current source code uses Office11 (2003) interop assembly Microsoft.Office.Interop.Excel.dll to create the Excel template.

When I create the template and open in Excel 2007, it opens perfectly. The same template when I open in 2003 I get the message "File format is not valid".

_excel = new Excel.Application();
_workbooks = _excel.Workbooks;
_excel.Visible = false;

_excel.DisplayAlerts = false;

// create and add a workbook with 1 worksheet named "Sheet1" 
_workbook = _workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);

_sheet = (Excel.Worksheet)_workbook.ActiveSheet;
+1  A: 

If you have both versions of the interop assemblies installed, you probably have a binding redirect assembly which is redirecting Office11 calls to Office12 assemblies.

From here:

When you install and register the Office PIAs in the global assembly cache (either with Office or by installing the redistributable package for the PIAs), the binding redirect assemblies are also installed only in the global assembly cache. These assemblies help make sure that the correct version of the primary interop assemblies are loaded at run time. For example, when a solution that references a 2007 Microsoft Office primary interop assembly runs on a computer that has the Microsoft Office 2010 version of the same primary interop assembly, the binding redirect assembly instructs the .NET Framework runtime to load the Microsoft Office 2010 version of the primary interop assembly. For more information, see Assembly Binding Redirection.

This would mean that your code is invoking Excel 2007 and therefore creating an Excel 2007 format file, which Excel 2003 can't read (without a conversion plugin).

Charles