none
Update data from embedded chart in a PowerPoint with C# OpenXML RRS feed

  • Question

  • Hello,

    I would like to update data from a chart into a powerpoint template.

    I try many things to access the embedded data but i don't know how access it. 

    When, i browsed the different part of the slide i founded in OleObject. Is it in this object that the data was store or maybe in the embedObjectPart ?

    For information, the template use Think-Cell plugin.

    My goal is to automate the generation of a PPT based on a template.

    Thanks in advance !

    Tuesday, October 11, 2016 12:59 PM

Answers

  • Hi BobbyJulich,

    According to your description, you could refer to below code:

    using DocumentFormat.OpenXml.Packaging;
    using C = DocumentFormat.OpenXml.Drawing.Charts;
    
    PresentationDocument pptDocument = PresentationDocument.Open("D:\\Original.pptx", true);
    PresentationPart PresPart = pptDocument.PresentationPart;
    SlidePart slidePart = PresPart.SlideParts.FirstOrDefault();
    ChartPart chartPart1 = slidePart.ChartParts.FirstOrDefault();
    ChangeChartPart(chartPart1);
    PresPart.Presentation.Save();
    pptDocument.Close();
    
    public static void ChangeChartPart(ChartPart chartPart1)
    {
        C.ChartSpace chartSpace1 = chartPart1.ChartSpace;
    
        C.Chart chart1 = chartSpace1.GetFirstChild<C.Chart>();
    
        C.PlotArea plotArea1 = chart1.GetFirstChild<C.PlotArea>();
    
        C.BarChart barChart1 = plotArea1.GetFirstChild<C.BarChart>();
    
        C.BarChartSeries barChartSeries1 = barChart1.Elements<C.BarChartSeries>().ElementAt(2);
    
        C.Values values1 = barChartSeries1.GetFirstChild<C.Values>();
    
        C.NumberReference numberReference1 = values1.GetFirstChild<C.NumberReference>();
    
        C.NumberingCache numberingCache1 = numberReference1.GetFirstChild<C.NumberingCache>();
    
        C.NumericPoint numericPoint1 = numberingCache1.GetFirstChild<C.NumericPoint>();
        C.NumericPoint numericPoint2 = numberingCache1.Elements<C.NumericPoint>().ElementAt(1);
        C.NumericPoint numericPoint3 = numberingCache1.Elements<C.NumericPoint>().ElementAt(2);
    
        C.NumericValue numericValue1 = numericPoint1.GetFirstChild<C.NumericValue>();
        numericValue1.Text = "102";
    
    
        C.NumericValue numericValue2 = numericPoint2.GetFirstChild<C.NumericValue>();
        numericValue2.Text = "102";
    
    
        C.NumericValue numericValue3 = numericPoint3.GetFirstChild<C.NumericValue>();
        numericValue3.Text = "103";
    }



    The result:

    In addition I suggest that you could use Open XML SDK productivity Tool to compare two PowerPoint files that One is original and the other is updated data, then "View Part Code", you would get C# code to how to update data from embedded chart.

    Thanks for your understanding.

    Wednesday, October 12, 2016 6:00 AM

All replies

  • Hi BobbyJulich,

    According to your description, you could refer to below code:

    using DocumentFormat.OpenXml.Packaging;
    using C = DocumentFormat.OpenXml.Drawing.Charts;
    
    PresentationDocument pptDocument = PresentationDocument.Open("D:\\Original.pptx", true);
    PresentationPart PresPart = pptDocument.PresentationPart;
    SlidePart slidePart = PresPart.SlideParts.FirstOrDefault();
    ChartPart chartPart1 = slidePart.ChartParts.FirstOrDefault();
    ChangeChartPart(chartPart1);
    PresPart.Presentation.Save();
    pptDocument.Close();
    
    public static void ChangeChartPart(ChartPart chartPart1)
    {
        C.ChartSpace chartSpace1 = chartPart1.ChartSpace;
    
        C.Chart chart1 = chartSpace1.GetFirstChild<C.Chart>();
    
        C.PlotArea plotArea1 = chart1.GetFirstChild<C.PlotArea>();
    
        C.BarChart barChart1 = plotArea1.GetFirstChild<C.BarChart>();
    
        C.BarChartSeries barChartSeries1 = barChart1.Elements<C.BarChartSeries>().ElementAt(2);
    
        C.Values values1 = barChartSeries1.GetFirstChild<C.Values>();
    
        C.NumberReference numberReference1 = values1.GetFirstChild<C.NumberReference>();
    
        C.NumberingCache numberingCache1 = numberReference1.GetFirstChild<C.NumberingCache>();
    
        C.NumericPoint numericPoint1 = numberingCache1.GetFirstChild<C.NumericPoint>();
        C.NumericPoint numericPoint2 = numberingCache1.Elements<C.NumericPoint>().ElementAt(1);
        C.NumericPoint numericPoint3 = numberingCache1.Elements<C.NumericPoint>().ElementAt(2);
    
        C.NumericValue numericValue1 = numericPoint1.GetFirstChild<C.NumericValue>();
        numericValue1.Text = "102";
    
    
        C.NumericValue numericValue2 = numericPoint2.GetFirstChild<C.NumericValue>();
        numericValue2.Text = "102";
    
    
        C.NumericValue numericValue3 = numericPoint3.GetFirstChild<C.NumericValue>();
        numericValue3.Text = "103";
    }



    The result:

    In addition I suggest that you could use Open XML SDK productivity Tool to compare two PowerPoint files that One is original and the other is updated data, then "View Part Code", you would get C# code to how to update data from embedded chart.

    Thanks for your understanding.

    Wednesday, October 12, 2016 6:00 AM
  • How do I do then "View Part Code",  in powerpoint. I have open xml 2.5 sdk installed

    MCTS Sharepoint 2010, MCAD dotnet, MCPDEA, SharePoint Lead

    Tuesday, May 12, 2020 3:53 PM