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; } } }
internal C.ChartSpace ToChartSpace(ref ChartPart chartp) { ImagePart imgp; C.ChartSpace cs = new C.ChartSpace(); cs.AddNamespaceDeclaration("c", "http://schemas.openxmlformats.org/drawingml/2006/chart"); cs.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main"); cs.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); cs.Date1904 = new C.Date1904() { Val = this.Date1904 }; cs.EditingLanguage = new C.EditingLanguage(); cs.EditingLanguage.Val = System.Globalization.CultureInfo.CurrentCulture.Name; cs.RoundedCorners = new C.RoundedCorners() { Val = this.RoundedCorners }; AlternateContent altcontent = new AlternateContent(); altcontent.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); AlternateContentChoice altcontentchoice = new AlternateContentChoice() { Requires = "c14" }; altcontentchoice.AddNamespaceDeclaration("c14", "http://schemas.microsoft.com/office/drawing/2007/8/2/chart"); // why +100? I don't know... ask Microsoft. But there are 48 styles. Even with the // advanced "+100" version, it's 96 total. It's a byte, with 256 possibilities. // As of this writing, Excel 2013 is rumoured to dispense away with this chart styling. // So maybe all this is moot anyway... altcontentchoice.Append(new C14.Style() { Val = (byte)(this.ChartStyle + 100) }); altcontent.Append(altcontentchoice); AlternateContentFallback altcontentfallback = new AlternateContentFallback(); altcontentfallback.Append(new C.Style() { Val = (byte)this.ChartStyle }); altcontent.Append(altcontentfallback); cs.Append(altcontent); C.Chart chart = new C.Chart(); if (this.HasView3D) { chart.View3D = new C.View3D(); if (this.RotateX != null) chart.View3D.RotateX = new C.RotateX() { Val = this.RotateX.Value }; if (this.HeightPercent != null) chart.View3D.HeightPercent = new C.HeightPercent() { Val = this.HeightPercent.Value }; if (this.RotateY != null) chart.View3D.RotateY = new C.RotateY() { Val = this.RotateY.Value }; if (this.DepthPercent != null) chart.View3D.DepthPercent = new C.DepthPercent() { Val = this.DepthPercent }; if (this.RightAngleAxes != null) chart.View3D.RightAngleAxes = new C.RightAngleAxes() { Val = this.RightAngleAxes.Value }; if (this.Perspective != null) chart.View3D.Perspective = new C.Perspective() { Val = this.Perspective.Value }; } if (this.HasTitle) { if (this.Title.Fill.Type == SLA.SLFillType.BlipFill) { imgp = chartp.AddImagePart(SLA.SLDrawingTool.GetImagePartType(this.Title.Fill.BlipFileName)); using (FileStream fs = new FileStream(this.Title.Fill.BlipFileName, FileMode.Open)) { imgp.FeedData(fs); } this.Title.Fill.BlipRelationshipID = chartp.GetIdOfPart(imgp); } chart.Title = this.Title.ToTitle(); } else { chart.AutoTitleDeleted = new C.AutoTitleDeleted() { Val = true }; } if (this.Is3D) { chart.Floor = new C.Floor(); chart.Floor.Thickness = new C.Thickness() { Val = this.Floor.Thickness }; if (this.Floor.ShapeProperties.HasShapeProperties) { if (this.Floor.Fill.Type == SLA.SLFillType.BlipFill) { imgp = chartp.AddImagePart(SLA.SLDrawingTool.GetImagePartType(this.Floor.Fill.BlipFileName)); using (FileStream fs = new FileStream(this.Floor.Fill.BlipFileName, FileMode.Open)) { imgp.FeedData(fs); } this.Floor.Fill.BlipRelationshipID = chartp.GetIdOfPart(imgp); } chart.Floor.ShapeProperties = this.Floor.ShapeProperties.ToCShapeProperties(); } chart.SideWall = new C.SideWall(); chart.SideWall.Thickness = new C.Thickness() { Val = this.SideWall.Thickness }; if (this.SideWall.ShapeProperties.HasShapeProperties) { if (this.SideWall.Fill.Type == SLA.SLFillType.BlipFill) { imgp = chartp.AddImagePart(SLA.SLDrawingTool.GetImagePartType(this.SideWall.Fill.BlipFileName)); using (FileStream fs = new FileStream(this.SideWall.Fill.BlipFileName, FileMode.Open)) { imgp.FeedData(fs); } this.SideWall.Fill.BlipRelationshipID = chartp.GetIdOfPart(imgp); } chart.SideWall.ShapeProperties = this.SideWall.ShapeProperties.ToCShapeProperties(); } chart.BackWall = new C.BackWall(); chart.BackWall.Thickness = new C.Thickness() { Val = this.BackWall.Thickness }; if (this.BackWall.ShapeProperties.HasShapeProperties) { if (this.BackWall.Fill.Type == SLA.SLFillType.BlipFill) { imgp = chartp.AddImagePart(SLA.SLDrawingTool.GetImagePartType(this.BackWall.Fill.BlipFileName)); using (FileStream fs = new FileStream(this.BackWall.Fill.BlipFileName, FileMode.Open)) { imgp.FeedData(fs); } this.BackWall.Fill.BlipRelationshipID = chartp.GetIdOfPart(imgp); } chart.BackWall.ShapeProperties = this.BackWall.ShapeProperties.ToCShapeProperties(); } } if (this.PlotArea.Fill.Type == SLA.SLFillType.BlipFill) { imgp = chartp.AddImagePart(SLA.SLDrawingTool.GetImagePartType(this.PlotArea.Fill.BlipFileName)); using (FileStream fs = new FileStream(this.PlotArea.Fill.BlipFileName, FileMode.Open)) { imgp.FeedData(fs); } this.PlotArea.Fill.BlipRelationshipID = chartp.GetIdOfPart(imgp); } chart.PlotArea = this.PlotArea.ToPlotArea(); if (this.ShowLegend) { if (this.Legend.Fill.Type == SLA.SLFillType.BlipFill) { imgp = chartp.AddImagePart(SLA.SLDrawingTool.GetImagePartType(this.Legend.Fill.BlipFileName)); using (FileStream fs = new FileStream(this.Legend.Fill.BlipFileName, FileMode.Open)) { imgp.FeedData(fs); } this.Legend.Fill.BlipRelationshipID = chartp.GetIdOfPart(imgp); } chart.Legend = this.Legend.ToLegend(); } if (this.ShowHiddenData == false) { chart.PlotVisibleOnly = new C.PlotVisibleOnly() { Val = true }; } else { //don't do anything } chart.DisplayBlanksAs = new C.DisplayBlanksAs() { Val = this.ShowEmptyCellsAs }; cs.Append(chart); if (this.ShapeProperties.HasShapeProperties) { cs.Append(this.ShapeProperties.ToCShapeProperties()); } return cs; }
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); } }
internal void FeedDataChartPart(ChartPart NewPart, ChartPart ExistingPart) { using (StreamReader sr = new StreamReader(ExistingPart.GetStream())) { using (StreamWriter sw = new StreamWriter(NewPart.GetStream(FileMode.Create))) { sw.Write(sr.ReadToEnd()); } } ImagePart imgpNew; if (ExistingPart.ChartDrawingPart != null) { NewPart.AddNewPart<ChartDrawingPart>(ExistingPart.GetIdOfPart(ExistingPart.ChartDrawingPart)); using (StreamReader sr = new StreamReader(ExistingPart.ChartDrawingPart.GetStream())) { using (StreamWriter sw = new StreamWriter(NewPart.ChartDrawingPart.GetStream(FileMode.Create))) { sw.Write(sr.ReadToEnd()); } } // why does a ChartPart contain a ChartDrawingPart that contains a ChartPart?? // Does it never end?? if (ExistingPart.ChartDrawingPart.ChartPart != null) { NewPart.ChartDrawingPart.AddNewPart<ChartPart>(ExistingPart.ChartDrawingPart.GetIdOfPart(ExistingPart.ChartDrawingPart.ChartPart)); this.FeedDataChartPart(NewPart.ChartDrawingPart.ChartPart, ExistingPart.ChartDrawingPart.ChartPart); } foreach (ImagePart imgp in ExistingPart.ChartDrawingPart.ImageParts) { imgpNew = NewPart.ChartDrawingPart.AddImagePart(imgp.ContentType, ExistingPart.ChartDrawingPart.GetIdOfPart(imgp)); this.FeedDataImagePart(imgpNew, imgp); } } if (ExistingPart.EmbeddedPackagePart != null) { NewPart.AddEmbeddedPackagePart(ExistingPart.EmbeddedPackagePart.ContentType); this.FeedDataEmbeddedPackagePart(NewPart.EmbeddedPackagePart, ExistingPart.EmbeddedPackagePart); } foreach (ImagePart imgp in ExistingPart.ImageParts) { imgpNew = NewPart.AddImagePart(imgp.ContentType, ExistingPart.GetIdOfPart(imgp)); this.FeedDataImagePart(imgpNew, imgp); } if (ExistingPart.ThemeOverridePart != null) { NewPart.AddNewPart<ThemeOverridePart>(ExistingPart.GetIdOfPart(ExistingPart.ThemeOverridePart)); this.FeedDataThemeOverridePart(NewPart.ThemeOverridePart, ExistingPart.ThemeOverridePart); } }