Question

J'ai un scénario où un client pourrait avoir soit Excel 2007 installé ou Excel 2010 est installé, je dois être en mesure d'exporter et d'importer fichiers Excel qu'ils créent, OLEDB est pas une option donc je vais utiliser les services Interop .

La question est quand je Codé pour 2010, il n'a pas fonctionné pour 2007 et vice versa comment puis-je y parvenir?

Puis-je importer les deux références en quelque sorte et définir Excel en fonction de la version installée?

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;
Était-ce utile?

La 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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top