Question

I have a scenario where a client could have either Excel 2007 installed or Excel 2010 installed, I need to be able to export and import excel files that they create, OLEDB isn't an option so I was going to use Interop services.

Question is when I coded for 2010 it didn't work for 2007 and vice versa how can I achieve this?

Can I import both references somehow and define Excel depending on the version installed?

using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.Range xlRange;

            object misValue = System.Reflection.Missing.Value;

            pbarExcelGenerate.Visible = true;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Name = "Material Results";

            pbarExcelGenerate.Value = 5;


            xlRange = xlWorkSheet.get_Range("a1", "b1");
            xlRange.Merge(false);
            xlRange.Value = "Material Name";
            xlRange.HorizontalAlignment = 3;
            xlRange.Font.Bold = true;
            xlRange.Font.Size = 16;
Was it helpful?

Solution

no you can't... you can either use the lower version for both... or use a 3rd-party library (free or commercial) which doesn't need Excel to be installed at all or you can use raw COM to access Excel which would be much less comfortable but allows for such things.

EDIT - as per comments (thanks to @Rup):

You can download the older version of VSTO at http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=23656 which should come with the Interop library for 2007

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top