I have a excel sheet with one chart which is the source. My target is a powerpoint presentation with 3 slides. I need to update slide #3 with the Chart in the excel file.
After executing the application and when I try to open the pptx file I get a “Powerpoint found a problem with content”. After I repair I notice that I always get a blank slide in Slide #2 which shows I am not updating the correct slide.
What should I do to go to the slide based on slide no. (I don’t have chart titles in the powerpoint slide and the excel chart) and why am I getting a invalid content error.
I Would greatly appreciate your help. Thanks
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();
}
}
}
}
}
If there is anybody who is looking how to identify a slide with a slide number you need to do the following
Next iterate through each slide using a loop. Within the foreach loop add the following code to see if a slide has a particular relationship id
I hope this is helpful