Question

I using openxml to create a powerpoint from an web app.I created a ppt with charts and opened ppt in openxml sdk productivity tool and code which was generated with that i modified the chart data which is coming from database,Code for which i created to modify the chart data as

created a class for the code in the sdk,in that CreatePart() i added these links

 ChartPart chartPart1 = slidePart1.AddNewPart<ChartPart>("rId3");
        GenerateChartPart1Content(chartPart1);
       // This is below code added
        #if true    // Injects the chart part modification process
                    var chartModifier1 = new ChartPartModifier();
                    chartModifier1.UpdateSecondChartPart(chartPart1);
        #endif

        EmbeddedPackagePart embeddedPackagePart1 = chartPart1.AddNewPart<EmbeddedPackagePart>("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "rId2");
        GenerateEmbeddedPackagePart1Content(embeddedPackagePart1);

and created a class for ChartPartModifier()

public void UpdateSecondChartPart(ChartPart chartPart)
    {
        // Searchs SeriesText and its Values to replace them with your dynamic data
        var seriesLabels = chartPart.ChartSpace.Descendants<SeriesText>().ToList();
        var seriesValues = chartPart.ChartSpace.Descendants<Values>().ToList();
        var categoryAxis = chartPart.ChartSpace.Descendants<CategoryAxisData>().ToList();

        for (int i = 0; i < this._lineSecCharts.Count; ++i)
        {
            var yourLine = this._lineSecCharts[i];

            var label = seriesLabels[i].Descendants<NumericValue>().FirstOrDefault();
            var values = seriesValues[i].Descendants<NumericValue>().ToList();
            var categories = categoryAxis[i].Descendants<NumericValue>().ToList();

            // Replaces the label of the series
            label.Text = yourLine.Label;
            // Replaces the values of the series
            for (int valIdx = 0; valIdx < values.Count(); ++valIdx)
            {
                values[valIdx].Text = yourLine.Plots[valIdx].Value.ToString();
                categories[valIdx].Text = yourLine.Plots[valIdx].Category;
            }
        }
    }

Like this is there any way to modify the data in the table,If so can any one provide me the solution is much appreciated.

Was it helpful?

Solution

I found answer after a research i'm able to update the table values from database using openxml

the below code which(if condition) added between table appending the rows and graphicdata appending

table1.Append(tableProperties1);
        table1.Append(tableGrid1);
        table1.Append(tableRow1);
        table1.Append(tableRow2);
        table1.Append(tableRow3);
        table1.Append(tableRow4);
        table1.Append(tableRow5);
        table1.Append(tableRow6);
        table1.Append(tableRow7);

 #if true    // Injects the table modification process
        TableModifier tableModifier = new TableModifier();//Create a class
        tableModifier.UpdateTable(table1);//Send the table object of which you wanted to update
 #endif
        graphicData1.Append(table1);

        graphic1.Append(graphicData1);

In class of TableModifier

using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Packaging;

public class TableModifier
{
    public TableModifier()
    {
        this.SetupDataSource();
    }

    public void UpdateTable(Table table)
    {
        var rows = table.Descendants<TableRow>().ToList();

        for (int r = 0; r < rows.Count(); ++r)
        {
            var yourRow = this._rows[r];
            var cells = rows[r].Descendants<TableCell>().ToList();

            for (int c = 0; c < cells.Count(); ++c)
            {
                var yourCell = yourRow.Cells[c];
                var text = cells[c].Descendants<Text>().FirstOrDefault();
                if (text != null)
                {
                    text.Text = yourCell.Value;
                }
            }
        }
    }

    private void SetupDataSource()
    {
        this._rows.Add(new Row()
        {
            Cells = new List<Cell>()
            {
                new Cell(){ Value = "Products" },
                new Cell(){ Value = "2010" },
                new Cell(){ Value = "2011" },
                new Cell(){ Value = "2012" },
            }
        });

        for (int i = 0; i < 6; ++i)
        {
            var productName = string.Format("Product {0}", (char)(i + 'A'));

            this._rows.Add(new Row()
            {
                Cells = new List<Cell>()
                {
                    new Cell(){ Value = productName },
                    new Cell(){ Value = "10%" },
                    new Cell(){ Value = "20%" },
                    new Cell(){ Value = "30%" },
                }
            });
        }
    }

    #region Private Data Structure

    private class Row
    {
        public List<Cell> Cells { get; set; }
    }

    private class Cell
    {
        public string Value { get; set; }
    }

    #endregion

    private List<Row> _rows = new List<Row>();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top