Example #1
0
 private static void CopyChartObjects(ChartPart oldChart, ChartPart newChart)
 {
     foreach (XElement dataReference in newChart.GetXDocument().Descendants(ns_c + "externalData"))
     {
         string relId = dataReference.Attribute(ns_r + "id").Value;
         EmbeddedPackagePart oldPart = (EmbeddedPackagePart)oldChart.GetPartById(relId);
         EmbeddedPackagePart newPart = newChart.AddEmbeddedPackagePart(oldPart.ContentType);
         using (Stream oldObject = oldPart.GetStream(FileMode.Open, FileAccess.Read))
         using (Stream newObject = newPart.GetStream(FileMode.Create, FileAccess.ReadWrite))
         {
             int byteCount;
             byte[] buffer = new byte[65536];
             while ((byteCount = oldObject.Read(buffer, 0, 65536)) != 0)
                 newObject.Write(buffer, 0, byteCount);
         }
         dataReference.Attribute(ns_r + "id").Value = newChart.GetIdOfPart(newPart);
     }
 }
 private static void CopyChartObjects(ChartPart oldChart, ChartPart newChart)
 {
     foreach (XElement dataReference in newChart.GetXDocument().Descendants(C.externalData))
     {
         string relId = dataReference.Attribute(R.id).Value;
         try
         {
             EmbeddedPackagePart oldPart = (EmbeddedPackagePart)oldChart.GetPartById(relId);
             EmbeddedPackagePart newPart = newChart.AddEmbeddedPackagePart(oldPart.ContentType);
             using (Stream oldObject = oldPart.GetStream(FileMode.Open, FileAccess.Read))
             using (Stream newObject = newPart.GetStream(FileMode.Create, FileAccess.ReadWrite))
             {
                 int byteCount;
                 byte[] buffer = new byte[65536];
                 while ((byteCount = oldObject.Read(buffer, 0, 65536)) != 0)
                     newObject.Write(buffer, 0, byteCount);
             }
             dataReference.Attribute(R.id).Value = newChart.GetIdOfPart(newPart);
         }
         catch (ArgumentOutOfRangeException)
         {
             ExternalRelationship oldRelationship = oldChart.GetExternalRelationship(relId);
             Guid g = Guid.NewGuid();
             string newRid = "R" + g.ToString().Replace("-", "");
             var oldRel = oldChart.ExternalRelationships.FirstOrDefault(h => h.Id == relId);
             if (oldRel == null)
                 throw new DocumentBuilderInternalException("Internal Error 0007");
             newChart.AddExternalRelationship(oldRel.RelationshipType, oldRel.Uri, newRid);
             dataReference.Attribute(R.id).Value = newRid;
         }
     }
 }
Example #3
0
        private static void UpdateEmbeddedWorkbook(ChartPart chartPart, ChartData chartData)
        {
            XDocument cpXDoc = chartPart.GetXDocument();
            XElement root = cpXDoc.Root;
            var firstSeries = root.Descendants(C.ser).FirstOrDefault();
            if (firstSeries == null)
                return;
            var firstFormula = (string)firstSeries.Descendants(C.f).FirstOrDefault();
            if (firstFormula == null)
                return;
            var sheet = firstFormula.Split('!')[0];
            var embeddedSpreadsheetRid = (string)root.Descendants(C.externalData).Attributes(R.id).FirstOrDefault();
            if (embeddedSpreadsheetRid == null)
                return;
            var embeddedSpreadsheet = chartPart.GetPartById(embeddedSpreadsheetRid);
            if (embeddedSpreadsheet != null)
            {
                using (SpreadsheetDocument sDoc = SpreadsheetDocument.Open(embeddedSpreadsheet.GetStream(), true))
                {
                    var workbookPart = sDoc.WorkbookPart;
                    var wbRoot = workbookPart.GetXDocument().Root;
                    var sheetRid = (string)wbRoot
                        .Elements(S.sheets)
                        .Elements(S.sheet)
                        .Where(s => (string)s.Attribute("name") == sheet)
                        .Attributes(R.id)
                        .FirstOrDefault();
                    if (sheetRid != null)
                    {
                        var sheetPart = workbookPart.GetPartById(sheetRid);
                        var xdSheet = sheetPart.GetXDocument();
                        var sheetData = xdSheet.Descendants(S.sheetData).FirstOrDefault();

                        var stylePart = workbookPart.WorkbookStylesPart;
                        var xdStyle = stylePart.GetXDocument();

                        int categoryStyleId = 0;
                        if (chartData.CategoryFormatCode != 0)
                            categoryStyleId = AddDxfToDxfs(xdSheet, xdStyle, chartData.CategoryFormatCode);
                        stylePart.PutXDocument();

                        var firstRow = new XElement(S.row,
                            new XAttribute("r", "1"),
                            new XAttribute("spans", string.Format("1:{0}", chartData.SeriesNames.Length + 1)),
                            new [] { new XElement(S.c,
                                new XAttribute("r", "A1"),
                                new XAttribute("t", "str"),
                                new XElement(S.v,
                                    new XAttribute(XNamespace.Xml + "space", "preserve"),
                                    " "))}
                                .Concat(
                                    chartData.SeriesNames
                                        .Select((sn, i) => new XElement(S.c,
                                            new XAttribute("r", RowColToString(0, i + 1)),
                                            new XAttribute("t", "str"),
                                            new XElement(S.v, sn)))));
                        var otherRows = chartData
                            .CategoryNames
                            .Select((cn, r) =>
                            {
                                var row = new XElement(S.row,
                                    new XAttribute("r", r + 2),
                                    new XAttribute("spans", string.Format("1:{0}", chartData.SeriesNames.Length + 1)),
                                    new[] {
                                        new XElement(S.c,
                                            new XAttribute("r", RowColToString(r + 1, 0)),
                                            categoryStyleId != 0 ? new XAttribute("s", categoryStyleId) : null,
                                            chartData.CategoryDataType == ChartDataType.String ? new XAttribute("t", "str") : null,
                                            new XElement(S.v, cn))
                                    }.Concat(
                                        Enumerable.Range(0, chartData.Values.Length)
                                            .Select((c, ci) =>
                                            {
                                                var cell = new XElement(S.c,
                                                    new XAttribute("r", RowColToString(r + 1, ci + 1)),
                                                    new XElement(S.v, chartData.Values[ci][r]));
                                                return cell;
                                            })));
                                return row;
                            });
                        var allRows = new[] {
                            firstRow
                        }.Concat(otherRows);
                        var newSheetData = new XElement(S.sheetData,
                            allRows);
                        sheetData.ReplaceWith(newSheetData);
                        sheetPart.PutXDocument();

                        var tablePartRid = (string)xdSheet
                            .Root
                            .Elements(S.tableParts)
                            .Elements(S.tablePart)
                            .Attributes(R.id)
                            .FirstOrDefault();
                        if (tablePartRid != null)
                        {
                            var partTable = sheetPart.GetPartById(tablePartRid);
                            var xdTablePart = partTable.GetXDocument();
                            var xaRef = xdTablePart.Root.Attribute("ref");
                            xaRef.Value = string.Format("A1:{0}", RowColToString(chartData.CategoryNames.Length - 1, chartData.SeriesNames.Length));
                            var xeNewTableColumns = new XElement(S.tableColumns,
                                new XAttribute("count", chartData.SeriesNames.Count() + 1),
                                new[] {
                                    new XElement(S.tableColumn,
                                        new XAttribute("id", 1),
                                        new XAttribute("name", " "))
                                }.Concat(
                                    chartData.SeriesNames.Select((cn, ci) =>
                                        new XElement(S.tableColumn,
                                            new XAttribute("id", ci + 2),
                                            new XAttribute("name", cn)))));
                            var xeExistingTableColumns = xdTablePart.Root.Element(S.tableColumns);
                            if (xeExistingTableColumns != null)
                                xeExistingTableColumns.ReplaceWith(xeNewTableColumns);
                            partTable.PutXDocument();
                        }
                    }
                }
            }
        }