How to update a PowerPoint slide with an Excel Chart

2.7k Views Asked by At

I have one Excel file that has a Chart (Bar Chart). I also have a Powerpoint presentation with 3 slides. Please note that there is no chart "Title" either on the Excel sheet ot on the Powerpoint slides. I need to update the chart in slide #2 with the the chart in the excel file.

Please note that I have searched this forum and what I found was ways to create a new slide as well as updating a chart using the tittle.

Any help will be greatly appreciated. Thanks for your time

EDIT: Your solution is good but does not help me with how to replace a chart in a particular slide. In my question I have stated that there are 3 slides. I want to specifically update the chart in Slide 2. There is no chart title so I will need to find the Chart to replace based on the Slide number. How is this done ? Thanks again.

1

There are 1 best solutions below

6
On

This can be done by following these steps: (Note: the powerpoint is the target, and the excel is the source)

  1. Open the target and locate the ChartPart you want to replace. Keep a reference to the parent SlidePart where the ChartPart is and copy the ChartPart's RelationshipId to the SlidePart and then delete the ChartPart from the target.
  2. Save the source and leave the file open.

            foreach (var slidePart in targetPPT.PresentationPart.SlideParts)
            {
                if (slidePart.ChartParts.Any())
                {
                    slidePartBookMark = slidePart;
    
                    var chartPart = slidePart.ChartParts.First();
                    chartPartIdBookMark = slidePart.GetIdOfPart(chartPart);
                    slidePart.DeletePart(chartPart);
                    slidePart.Slide.Save();
                    return;
                }
            }
    
  3. Open the source and locate the ChartPart that you want to copy to the target. It will be in a DrawingsPart of a WorksheetPart somewhere in your source. Keep a reference to the ChartPart and leave the file open.

            foreach (var worksheetPart in sourceXls.WorkbookPart.WorksheetParts)
            {
                if (worksheetPart.DrawingsPart != null)
                    if (worksheetPart.DrawingsPart.ChartParts.Any())
                    {
                        saveXlsChart = worksheetPart.DrawingsPart.ChartParts.First();
                        return;
                    }
    
            }
    
  4. In the SlidePart reference from step 1, create a new ChartPart with the AddNewPart call using the RelationshipId

        var newChartPart = slidePartBookMark.AddNewPart<ChartPart>(chartPartIdBookMark);
    
  5. Copy the contents of the source chart to the new chartpart you created in step 4 with a FeedData/GetStream idea found here. Save the Slide, and close your files.

        newChartPart.FeedData(saveXlsChart.GetStream());
        slidePartBookMark.Slide.Save();
        sourceXls.Close();
        targetPPT.Close();
    

I was able to get it to work with a simple source/target that only contained one chart each. If your files are more complex, your locate logic will be more than a First().

Hope this helps.