Hi

I have an excel file which contains some data and a chart on Sheet1.


I also have a Powerpoint presentation which contains 3 slides. I would like to update the chart on Slide #2 with the chart from the excel file. Please note that there are no chart title on the excel and the powerpoint slide


The following console application code (C#) that I copy has the following problems

1) After executing the application when I try to open the Powerpoint (.pptx) file I get a error "Powerpoint found a problem with content in <filename>

2) I also think that the correct Chart is not getting replaced.

Any help will be greatly appreciated

Ps. I have copied my script below because when I clicked on Insert script button and clicked on the insert link nothing happend.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using OpenXmlPkg = DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

namespace ExportChart
{
    class Program
    {
        static void Main(string[] args)
        {
            string SourceFile = "Projected Sales.xlsx";
            string TargetFile = "Projected Sales.pptx";
            string targetppt = "Generatedppt.pptx";

            ChartPart chartPart;
            ChartPart newChartPart;
            SlidePart slidepartbkMark = null;
            string chartPartIdBookMark = null;

            File.Copy(TargetFile, targetppt, true);

            //Powerpoint document
            using (OpenXmlPkg.PresentationDocument pptPackage = OpenXmlPkg.PresentationDocument.Open(targetppt, true))
            {

                OpenXmlPkg.PresentationPart presentationPart = pptPackage.PresentationPart;

                var secondSlidePart = pptPackage.PresentationPart.SlideParts.Skip(1).Take(1);
                foreach (var slidepart in pptPackage.PresentationPart.SlideParts)
                {
               
                slidepartbkMark = slidepart;

                    if (slidepart.GetPartsCountOfType<ChartPart>() != 0)
                    {
                        chartPart = slidepart.ChartParts.First();
                        chartPartIdBookMark = slidepart.GetIdOfPart(chartPart);
                        slidepart.DeletePart(chartPart);
                        slidepart.Slide.Save();
                    }

                    //return;
                }

                newChartPart = slidepartbkMark.AddNewPart<ChartPart>(chartPartIdBookMark);

                ChartPart saveXlsChart = null;
                using (SpreadsheetDocument xlsDocument = SpreadsheetDocument.Open(SourceFile.ToString(), true))
                {
                    WorkbookPart xlsbookpart = xlsDocument.WorkbookPart;

                    foreach (var worksheetPart in xlsDocument.WorkbookPart.WorksheetParts)
                    {
                        if (worksheetPart.DrawingsPart != null)
                            if (worksheetPart.DrawingsPart.ChartParts.Any())
                            {
                                saveXlsChart = worksheetPart.DrawingsPart.ChartParts.First();
                            }
                    }


                    newChartPart.FeedData(saveXlsChart.GetStream());
                    slidepartbkMark.Slide.Save();
                    xlsDocument.Close();
                    pptPackage.Close();
                }
            }
        }
    }    
}