2016-05-24 6 views
0

Wie ein Microsoft Excel (.xlsx) zu einer PPTProgrammatically (C#) konvertieren Excel in eine Liste von Bild

Mein bestimmt

Algorithmus

Datei konvertieren Get Charts in Excel programmatisch (C#) Datei . und speichern Sie in Liste der Bilder

Derzeit bin ich mit Microsoft Interop Bibliotheken, auch durch ich nicht, wie ich habe keine freie Alternative für diese Aufgabe

Also meine aktuelle Arbeit-around ist wie folgt:

Öffnen Sie Excel-Datei mit Microsoft Interop;

Alle Charts in Excel

Verwenden Copypicture() auf diesem Diagramm, das die Daten in die Zwischenablage kopiert.

, wenn wir Bilder in der Liste haben, können wir, dass in neue Powerpoint-Datei Schaffung neuer PPT-Dokument

Bitte lassen Sie mich hinzufügen, wissen, wie man Bilderliste

public List<Image> Chartimages; 
public List<Metafile> ChartimagesMetafile; 

public List<BitmapSource> ChartimagesBitmapSource; 

    public void InsertChartIntoChartlist() 
      { 
       try 
       { 
        // Create an instance of PowerPoint. 
        powerpointApplication = new pptNS.Application(); 

        // Create an instance Excel. 
        excelApplication = new xlNS.Application(); 

        // Open the Excel workbook containing the worksheet with the chart 

        // data. 
        excelWorkBook = excelApplication.Workbooks.Open(paramWorkbookPath, 
                 paramMissing, paramMissing, paramMissing, 
                 paramMissing, paramMissing, paramMissing, 
                 paramMissing, paramMissing, paramMissing, 
                 paramMissing, paramMissing, paramMissing, 
                 paramMissing, paramMissing); 

        // Get the worksheet that contains the chart. 
        targetSheet = (xlNS.Worksheet)(excelWorkBook.Worksheets[2]); 

        // Get the ChartObjects collection for the sheet. 

        chartObjects = (xlNS.ChartObjects)(targetSheet.ChartObjects(paramMissing)); 

        foreach (xlNS.ChartObject item in chartObjects) 
        { 
         // // Get the chart to copy. 
         existingChartObject = (xlNS.ChartObject)(item); 

         // Copy the chart from the Excel worksheet to the clipboard. 

         existingChartObject.CopyPicture(xlNS.XlPictureAppearance.xlPrinter, xlNS.XlCopyPictureFormat.xlPicture); 

         if (Clipboard.ContainsData(System.Windows.DataFormats.EnhancedMetafile)) 
         { 
          Metafile metafile = Clipboard.GetData(System.Windows.DataFormats.EnhancedMetafile) as Metafile; 
          // metafile.Save(fileName); 
          ChartimagesMetafile.Add(metafile); 
         } 
         else if (Clipboard.ContainsData(System.Windows.DataFormats.Bitmap)) 
         { 
          BitmapSource bitmapSource = Clipboard.GetData(System.Windows.DataFormats.Bitmap) as BitmapSource; 

          ChartimagesBitmapSource.Add(bitmapSource); 
         } 
        } 
       } 
       catch (Exception ex) 
       { 
        Console.WriteLine(ex.Message); 
       } 
      } 

Dank

to Clipboard hinzufügen Schließlich, wer auch immer das abgelehnt hat: Wir lernten alle eines Tages, wie man etwas macht, da ein Anfänger definitiv programmierbezogen ist, unabhängig davon, wie neu jemand sein könnte.

Antwort

0

Ich konnte den Code erhalten, um das Diagramm zu kopieren; getestet durch Einfügen in ein Word-Dokument. Der Powerpoint-Teil sollte sich einfügen.

public void InsertChartIntoChartlist() 
{ 
    try 
    { 
     // Create an instance of PowerPoint. 
     var powerpointApplication = new Microsoft.Office.Interop.PowerPoint.Application(); 

     // Create an instance Excel. 
     var excelApplication = new Microsoft.Office.Interop.Excel.Application(); 

     // Open the Excel workbook containing the worksheet with the chart data. 
     var excelWorkBook = excelApplication.Workbooks.Open(@"C:\Book1.xlsx"); 

     // Get the worksheet that contains the chart. 
     var targetSheet = excelWorkBook.Worksheets[2]; 

     // Get the ChartObjects collection for the sheet. 
     var chartObjects = targetSheet.ChartObjects(Type.Missing); 

     foreach (Microsoft.Office.Interop.Excel.ChartObject item in chartObjects) 
     { 
      item.Copy(); 
     } 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex.Message); 
    } 
} 
+0

Ryan löste ich es in besseren Weg –

1

Hier Lösung:

Get-Charts in Excel programmatisch (C#) Datei.und speichern Sie in Liste der Bilder

erstellen Klasse ImageWithImageName genannt Bilder zu halten zusammen mit Namen

class ImageWithImageName 
    { 
     public Image ChartimagesBitmapSource; 
     public string Filename; 
     public ImageWithImageName(Image pramChartimagesBitmapSource, string pramFilename) 
     { 
      ChartimagesBitmapSource = pramChartimagesBitmapSource; 
      Filename = pramFilename; 
     } 
    } 

Nutzungs

public List<ImageWithImageName> ChartImages; 

Methode Erstellen Sie alle Diagramme ChartImages Liste Array hinzufügen

public void InsertChartIntoExcel() { versuchen {

  // Create an instance Excel. 
      excelApplication = new xlNS.Application(); 

      // Open the Excel workbook containing the worksheet with the chart 
      // data. 
      excelWorkBook = excelApplication.Workbooks.Open(paramWorkbookPath, 
          paramMissing, paramMissing, paramMissing, 
          paramMissing, paramMissing, paramMissing, 
          paramMissing, paramMissing, paramMissing, 
          paramMissing, paramMissing, paramMissing, 
          paramMissing, paramMissing); 

      // Get the worksheet that contains the chart. 
      targetSheet = (xlNS.Worksheet)(excelWorkBook.Worksheets[2]); 

      // Get the ChartObjects collection for the sheet. 
      chartObjects = (xlNS.ChartObjects)(targetSheet.ChartObjects(paramMissing)); 

      // Create a PowerPoint presentation. 
      //pptPresentation = powerpointApplication.Presentations.Add(
      //     Microsoft.Office.Core.MsoTriState.msoTrue); 

      int i = 1; 
      foreach (xlNS.ChartObject item in chartObjects) 
      { 
       // // Get the chart to copy. 
       existingChartObject = (xlNS.ChartObject)(item); 

       string chartname = item.Name; 

       existingChartObject.CopyPicture(xlNS.XlPictureAppearance.xlScreen, xlNS.XlCopyPictureFormat.xlBitmap); 

// contains in Clipboard so extract from clipboard 

       if (Clipboard.ContainsImage()) 
       { 


        var image = Clipboard.GetData(System.Windows.DataFormats.Bitmap) as Image; 
        if (image != null) 
        { 
         ChartImages.Add(new ImageWithImageName(image, chartname + ".png")); 
        } 
       } 



       i++; 
      } 

     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
     finally 
     { 


      // Release the Excel objects. 
      targetSheet = null; 
      chartObjects = null; 
      existingChartObject = null; 

      // Close and release the Excel Workbook object. 
      if (excelWorkBook != null) 
      { 
       excelWorkBook.Close(false, paramMissing, paramMissing); 
       excelWorkBook = null; 
      } 

      // Quit Excel and release the ApplicationClass object. 
      if (excelApplication != null) 
      { 
       excelApplication.Quit(); 
       excelApplication = null; 
      } 

      GC.Collect(); 
      GC.WaitForPendingFinalizers(); 
      GC.Collect(); 
      GC.WaitForPendingFinalizers(); 
     } 
    } 

Jetzt habe ich Bilder in der Liste der Bilder so, jetzt können wir eine neue Powerpoint-Datei für diese Bilder erstellen

FYI: wir Bilder auf PPT über Open XML hinzufügen warum COM-Interop, um eine PPT zu erstellen

Glücklich Codierung Ranjith

Verwandte Themen