How to use a specific version of Excel in a C # program

For many reasons, I installed MSExcel 2003 and MSExcel 2007 on my laptop. I need both versions to develop specific projects for each version (document-level projects and application-level projects). Now I need to make a WinForm project that opens an Excel file, read CustomXMLParts and write a new Excel file. I use a link to Microsoft.Office.Interop.Excel, which use .. \ Visual Studio Tools for Office \ PIA \ Office12 \ Microsoft.Office.Interop.Excel.dll library (this is for Excel 2007). And for this code:

Microsoft.Office.Interop.Excel.Application excelApplication; excelApplication = new Microsoft.Office.Interop.Excel.ApplicationClass(); string version = excelApplication.Version; 

At this point, the version is "11.0", but I need to open Excel 2007, and it should be "12.0", and then when the program tries to get CustomXMLParts, it throws an exception because this method does not exist in 2003.

If I uninstall Excell 2003, it works fine, but I need to work with both (2003 and 2007). When I reinstall Excell 2003, it again fails. I check the “Specific Version” property for the Interop.Excel link to make sure it is true, and I tried changing app.config oldVersion = "12.0.0.0" so that it was not compatible with Excel 2003, but nothing happens:

  <assemblyIdentity name="Microsoft.Office.Interop.Excel" publicKeyToken="71E9BCE111E9429C" culture="neutral"/> <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="12.0.0.0"/> 

Any idea?

I am using Visual Studio 2008 and Visual Studio Tools for Office. Thanks in advance.

+4
source share
2 answers

Probably not the answer you are looking for, but look at this question: Attempting to automate Office using Excel 2007, but continues to use Excel 2003

+1
source

You can use ExcelPackage for Codeplex for your xlsx documents. Then there is no need or use interop. They have sample code for reading and writing xlsx files.

Thus, you can continue to use interop for 2003 without intervention.

If you need to use interop from both versions, you must load the assemblies using each version in a separate AppDomain to separate them.

+1
source

All Articles