tags:

views:

60

answers:

2

I was wondering about the best way to do this:

I have an app that works with the Excel DOM. Users may have either Excel 2003 or 2007 installed, I don't know which one a priori. Mine is a C# console app.

How can I make my app compatible with both? Is it possible at runtime to find what version and modify the Using statements accordingly? For example, I seem to remember that to use Excel 2003, I write Using Microsoft.Excel, or just Excel with the corresponding reference dll, but for 2007, I need Using.interop.Excel or something like that... not to the point right now, but makes the problem clearer. Besides, many of the types of Excel 2003 are, well, not clearly defined (series, for example, are Arrays of Objetc), while in 2007 some of them are more strongly "typed", etc.

I'd be happy to edit if the question is not yet clear enough.

Thanks.

+1  A: 

Consider using MS Office For Net, a wrapper library that handles many versions of Office.

If that does not work for you, instead code to the oldest version of the interop assembly you need to support. For one project, I had the current version installed on my PC but had the oldest version I supported installed on a virtual machine that I used as a build machine. In that situation I had to be careful to only use things available in the older API (else the compile would fail on the build VM).

Eric J.
A: 

We tried for a while to get some COM Interop test code to run against both Excel 2003 and Excel 2007 but eventually decided to support only Excel 2007 because of the API differences.

If you really need to support both, my suggestion is to have two versions of your application / library. The first would be written to Excel 2003 and built on a machine which only has Excel 2003 installed. The second would be written to support Excel 2007 and built on a machine which only has Excel 2007 installed.

You might want to try SpreadsheetGear for .NET which supports .xls and .xlsx files and will get rid of any dependency on Excel. You can download a free evaluation here.

Disclaimer: I own SpreadsheetGear LLC

Joe Erickson