Is there source code for your video?

Home Forums Open-Xml-Sdk Is there source code for your video?


This topic contains 0 replies, has 1 voice, and was last updated by  rogersb 8 months, 3 weeks ago.

Viewing 1 post (of 1 total)
  • Author
  • #3879


    Hi Eric,
    your video talks about modifying bar charts. still struggling with this.

    I have gone thru the video step by step, duplicating the code there. All I get is a corrupted word document.

    Is there a code snippet for this? I run this and it seems to modify bar chart data but refuses to open saying the file is corrupt.

      var mdXDoc = mainDoc.GetXDocument();
                        var cc2 = mdXDoc.Descendants(W.sdt)
            .FirstOrDefault(sdt => (string)sdt.Elements(W.sdtPr).Elements(W.tag).Attributes(W.val).First() == "Chart1");
                        if (cc2 != null)
                            var chartRid = "rId5";// (string)cc.Descendants(C.chart).Attributes(;
                            if (chartRid != null)
                                ChartPart chartPart = (ChartPart)mainDoc.GetPartById(chartRid);
                                string externalDataRid = "rId3";
                                //  ExternalData ed = chartPart.ChartSpace.Elements<ExternalData>().FirstOrDefault();
                                C2.ExternalData ed = chartPart.ChartSpace.Elements<C2.ExternalData>().FirstOrDefault();
                                if (ed != null)
                                    externalDataRid = ed.Id;
                                EmbeddedPackagePart epp = (EmbeddedPackagePart)chartPart.Parts
                                    .Where(pt => pt.RelationshipId == externalDataRid).FirstOrDefault()
                             //   using (Stream str = epp.GetStream())
                                using(Stream str2 = epp.GetStream())
                                using (MemoryStream ms = new MemoryStream())
                                    CopyStream(str2, ms);
                                    using (SpreadsheetDocument spreadsheetDoc = SpreadsheetDocument.Open(ms, true))
                                        ss.Sheet ws = (ss.Sheet)spreadsheetDoc.WorkbookPart
                                        string sheetId = ws.Id;
                                        WorksheetPart wsp = (WorksheetPart)spreadsheetDoc.WorkbookPart.Parts
                                            .Where(pt => pt.RelationshipId == sheetId).FirstOrDefault()
                                        ss.SheetData sd = wsp.Worksheet.Elements<ss.SheetData>().FirstOrDefault();
                                        ss.Row secondRow = sd.Elements<ss.Row>().Skip(2).FirstOrDefault();
                                        if(secondRow != null)
                                            ss.CellValue cell1value = secondRow.Elements<ss.Cell>().ElementAt(0)
                                            cell1value.Text = "33";
                                            ss.CellValue cell2value = secondRow.Elements<ss.Cell>().ElementAt(1)
                                            cell2value.Text = "44";
                                            //ss.CellValue cell3value = secondRow.Elements<ss.Cell>().ElementAt(3)
                                            //   .Elements<ss.CellValue>().FirstOrDefault();
                                            //cell3value.Text = "77";
                                    using (Stream s = epp.GetStream())
                                DocumentFormat.OpenXml.Drawing.Charts.Chart chart = chartPart.ChartSpace.Elements<DocumentFormat.OpenXml.Drawing.Charts.Chart>().First();
                                C2.BarChart bc = chart.Descendants<DocumentFormat.OpenXml.Drawing.Charts.BarChart>().FirstOrDefault();
                                if (bc != null)
                                    C2.BarChartSeries bcs = bc.Elements<C2.BarChartSeries>().First();
                                    if (bcs != null)
                                        C2.Values values = bcs.Descendants<C2.Values>().First();
                                        C2.NumberingCache nc = values.Descendants<C2.NumberingCache>().First();
                                   //     C2.NumericValue nv1 = nc.Elements<C2.NumericPoint>().ElementAt(0)
                                            C2.NumericValue nv1 = nc.Elements<C2.NumericPoint>().ElementAt(0)
                                        nv1.Text = "33";
                                      //  C2.NumericValue nv2 = nc.Elements<C2.NumericPoint>().ElementAt(1)
                                              C2.NumericValue nv2 = nc.Elements<C2.NumericPoint>().ElementAt(1)
                                        nv2.Text = "44";
                                        //C2.NumericValue nv3 = nc.Elements<C2.NumericPoint>().ElementAt(2)
                                        //   .Elements<C2.NumericValue>().FirstOrDefault();
                                        //nv3.Text = "77";

    I am only opening up 1 word doc. I have the barchart inside a content control, the other code taking a different approach with XElements does modify the chart data and worked until I had to modify the axis. Im trying this approach.

    also Im wondering why it seems to create a binary file for the chart data? you make a barchart in Excel, copy and say Embed chart. I did this exactly as your example shows. look in the xml tool and its got a binary literal string blob where the data is:

     #region Binary Data

    but this was not there in my original word doc used for a template. I got the embedded RID as rId3

    it should be so simple but week after week still cannot solve this.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.