2017-08-14 1 views
0

J'ai un classeur Excel avec plusieurs graphiques à l'intérieur. J'ai créé une boucle for qui obtient les cartes dans chaque feuille. Cela fonctionne sans erreur. Les problèmes que j'ai sont je ne peux pas importer plusieurs diagrammes, un seul et il importe comme une image pas un diagramme.Excel/Powerpoint Office Interop - Copier le graphique de la feuille Excel vers la diapositive Powerpoint

Lorsque je tente d'importer plusieurs tableaux, je reçois un COMException sur cette ligne:

objChart.Copy(); 

erreur complète:

An exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll but was not handled in user code 

Additional information: Exception from HRESULT: 0x800A03EC 

Voici ma pleine méthode:

public int ImportExcelChartsFromWorkbookToSlides(int startingSlideIndex, string workbookPath, string[] slideTitles) 
{ 
    int slideIndex = startingSlideIndex; 
    int titleIndex = 0; 
    EXCL.Application objExclApp = new EXCL.Application(); 
    EXCL.Workbook objWorkbook = objExclApp.Workbooks.Open(workbookPath, Editable: false); 
    foreach (EXCL.Worksheet objSheet in objWorkbook.Worksheets) 
    { 
     foreach (EXCL.ChartObject objChart in objSheet.ChartObjects()) 
     { 
      AddTitleOnlySlide(slideIndex); 
      SetTitleOnlySlideTitle(slideTitles[titleIndex]); 

      // Copy Chart from Sheet to Slide 
      objChart.Copy(); 

      PPT.ShapeRange objShapeRange = objSlide.Shapes.Paste(); 

      // TODO PARAMETER 
      objShapeRange.Left = 10; 
      objShapeRange.Top = 100; 

      slideIndex++; 
      titleIndex++; 
     } 
    } 
    return slideIndex; 
} 

Quelqu'un voit-il des problèmes avec ce code?

MISE À JOUR

Si je change cette ligne:

objChart.Copy(); 

à ceci:

objChart.CopyPicture(); 

Je n'ai pas des problèmes .. Ce qui pourrait être la cause?

Répondre

0

J'ai réussi à résoudre ce problème en utilisant objChart.CopyPicture(); mais c'était causing crashes pour certains utilisateurs, donc je fini par faire ceci:

string chartPath = [email protected]"{workbookPath.Substring(0, workbookPath.LastIndexOf("\\") + 1)}{DateTime.Now.Ticks}.png"; 
objChart.Chart.Export(chartPath, "PNG", false); 
objSlide.Shapes.AddPicture2(chartPath, MsoTriState.msoFalse, MsoTriState.msoTrue, chartPosLeft, chartPosTop, compress: MsoPictureCompress.msoPictureCompressFalse);