private XElement WriteWrokSheetDrawing_Content_pic(ExcelPicture pic, DrawingCollection drawingCollection, PackagePart wrokSheetDrawingPart, int index) { XElement picNode = new XElement(XName.Get("pic", ExcelCommon.Schema_SheetDrawings)); #region "nvPicPr" XElement nvPicPrNode = new XElement(XName.Get("nvPicPr", ExcelCommon.Schema_SheetDrawings)); XElement cNvPrNode = new XElement(XName.Get("cNvPr", ExcelCommon.Schema_SheetDrawings), new XAttribute(XName.Get("id"), (index + 2).ToString())); if (string.IsNullOrEmpty(pic.Name)) cNvPrNode.Add(new XAttribute(XName.Get("name"), string.Format("图片 {0}", index))); else cNvPrNode.Add(new XAttribute(XName.Get("name"), pic.Name)); nvPicPrNode.Add(cNvPrNode); XElement cNvPicPrNode = new XElement(XName.Get("cNvPicPr", ExcelCommon.Schema_SheetDrawings)); cNvPicPrNode.Add(new XElement(XName.Get("picLocks", ExcelCommon.Schema_Drawings), new XAttribute("noChangeAspect", 1))); nvPicPrNode.Add(cNvPicPrNode); picNode.Add(nvPicPrNode); #endregion "nvPicPr" #region "blipFill" XElement blipFillNode = new XElement(XName.Get("blipFill", ExcelCommon.Schema_SheetDrawings)); XElement blipNode = new XElement(XName.Get("blip", ExcelCommon.Schema_Drawings)); blipNode.Add(new XAttribute(XName.Get("R", XNamespace.Xmlns.NamespaceName), ExcelCommon.Schema_Relationships)); ExcelImageInfo currentImage = SavePicture(pic.Name, pic.ImageFormat, pic.Image, pic.ContentType); if (this.Context.HashImageRelationships.ContainsKey(currentImage.Hash)) blipNode.Add(new XAttribute(XName.Get("embed", ExcelCommon.Schema_Relationships), this.Context.HashImageRelationships[currentImage.Hash])); else { PackageRelationship picRelation = wrokSheetDrawingPart.CreateRelationship(PackUriHelper.GetRelativeUri(drawingCollection.DrawingUri, currentImage.Uri), TargetMode.Internal, ExcelCommon.Schema_Relationships + "/image"); blipNode.Add(new XAttribute(XName.Get("embed", ExcelCommon.Schema_Relationships), picRelation.Id)); this.Context.HashImageRelationships.Add(currentImage.Hash, picRelation.Id); } blipNode.Add(new XAttribute(XName.Get("cstate"), "print")); blipFillNode.Add(blipNode); XElement stretchNode = new XElement(XName.Get("stretch", ExcelCommon.Schema_Drawings), new XElement(XName.Get("fillRect", ExcelCommon.Schema_Drawings))); blipFillNode.Add(stretchNode); picNode.Add(blipFillNode); #endregion #region "spPr" XElement spPrNode = new XElement(XName.Get("spPr", ExcelCommon.Schema_SheetDrawings), new XElement(XName.Get("xfrm", ExcelCommon.Schema_Drawings), new XElement(XName.Get("off", ExcelCommon.Schema_Drawings), new XAttribute("x", 0), new XAttribute("y", 0)), new XElement(XName.Get("ext", ExcelCommon.Schema_Drawings), new XAttribute("cx", 0), new XAttribute("cy", 0))), new XElement(XName.Get("prstGeom", ExcelCommon.Schema_Drawings), new XAttribute("prst", "rect"), new XElement(XName.Get("avLst", ExcelCommon.Schema_Drawings)))); if (pic._Fill != null) WriteWrokSheetDrawing_pic_solidFill(pic._Fill, spPrNode); if (pic._Border != null) WriteWrokSheetDrawing_pic_ln(pic._Border, spPrNode); picNode.Add(spPrNode); #endregion return picNode; }
private void ReadWrokSheetDrawings_DrawingPic_spr_ln(ExcelPicture target, XElement lnElement) { XAttribute xbLn = lnElement.Attribute(XName.Get("w")); if (xbLn != null) { target.Border.Width = GetInt(xbLn.Value) / 12700; } xbLn = lnElement.Attribute(XName.Get("cap")); if (xbLn != null) { target.Border.LineCap = TranslateLineCap(xbLn.Value); } XElement fillNode = lnElement.Element(XName.Get("solidFill", ExcelCommon.Schema_Drawings)); if (fillNode != null) { target.Border._Fill = new DrawingFill(); ReadWrokSheetDrawings_DrawingPic_spr_fill(target.Border._Fill, fillNode); } fillNode = lnElement.Element(XName.Get("prstDash", ExcelCommon.Schema_Drawings)); if (fillNode != null) { xbLn = fillNode.Attribute(XName.Get("val")); if (xbLn != null) { target.Border.LineStyle = TranslateLineStyle(xbLn.Value); } } }
private void ReadWrokSheetDrawings_DrawingPic(ExcelPicture target, XElement xnode, Uri drawingUri) { XElement childNode = xnode.Element(XName.Get("nvPicPr", ExcelCommon.Schema_SheetDrawings)); if (childNode != null) { XElement picChildNode = childNode.Element(XName.Get("cNvPr", ExcelCommon.Schema_SheetDrawings)); if (picChildNode != null) { XAttribute xaName = picChildNode.Attribute(XName.Get("name")); if (xaName != null) target.Name = xaName.Value; } } childNode = xnode.Element(XName.Get("blipFill", ExcelCommon.Schema_SheetDrawings)); if (childNode != null) { XElement blipFillNode = childNode.Element(XName.Get("blip", ExcelCommon.Schema_Drawings)); if (blipFillNode != null) { XAttribute r = blipFillNode.Attribute(XName.Get("embed", ExcelCommon.Schema_Relationships)); if (r != null) { PackagePart drawingsPart = this.Context.Package.GetPart(drawingUri); PackageRelationship drawingRelation = drawingsPart.GetRelationship(r.Value); Uri uriPic = PackUriHelper.ResolvePartUri(drawingUri, drawingRelation.TargetUri); PackagePart part = drawingsPart.Package.GetPart(uriPic); FileInfo picInfo = new FileInfo(uriPic.OriginalString); target.ImageFormat = ExcelHelper.GetImageFormat(picInfo.Extension); target.ContentType = ExcelHelper.GetContentType(picInfo.Extension); target.Image = Image.FromStream(part.GetStream()); } } } childNode = xnode.Element(XName.Get("spPr", ExcelCommon.Schema_SheetDrawings)); if (childNode != null) ReadWrokSheetDrawings_DrawingPic_spr(target, childNode); }
private void ReadWrokSheetDrawings_DrawingPic_spr(ExcelPicture target, XElement blipFillNode) { XElement childElement = blipFillNode.Element(XName.Get("solidFill", ExcelCommon.Schema_Drawings)); if (childElement != null) { target._Fill = new DrawingFill(); ReadWrokSheetDrawings_DrawingPic_spr_fill(target._Fill, childElement); } childElement = blipFillNode.Element(XName.Get("noFill")); if (childElement != null) { target._Fill = new DrawingFill(); target._Fill.FillStyle = ExcelDrawingFillStyle.NoFill; } childElement = blipFillNode.Element(XName.Get("ln", ExcelCommon.Schema_Drawings)); if (childElement != null) { target._Border = new DrawingBorder(); ReadWrokSheetDrawings_DrawingPic_spr_ln(target, childElement); } }
internal void ReadWrokSheetDrawings(DrawingCollection target, XElement targetElement, ExcelLoadContext context) { IEnumerable<XElement> childNodes = targetElement.Elements(XName.Get("twoCellAnchor", ExcelCommon.Schema_SheetDrawings)); ExcelDrawing drTarget = null; foreach (XElement xnode in childNodes) { XElement childNode = xnode.Element(XName.Get("sp", ExcelCommon.Schema_SheetDrawings)); if (childNode != null) { //return new Shape(target); } childNode = xnode.Element(XName.Get("pic", ExcelCommon.Schema_SheetDrawings)); if (childNode != null) { ExcelPicture drPictureTarget = new ExcelPicture(target._WorkSheet); ReadWrokSheetDrawings_DrawingPic(drPictureTarget, childNode, target.DrawingUri); drTarget = drPictureTarget; } childNode = xnode.Element(XName.Get("graphicFrame", ExcelCommon.Schema_SheetDrawings)); if (childNode != null) { string rId = xnode.Element(XName.Get("graphicFrame", ExcelCommon.Schema_SheetDrawings)).Element(XName.Get("graphic", ExcelCommon.Schema_Drawings)).Element(XName.Get("graphicData", ExcelCommon.Schema_Drawings)).Element(XName.Get("chart", ExcelCommon.Schema_Chart)).LastAttribute.Value; PackageRelationship drawingsRelation = context.Package.GetPart(target.DrawingUri).GetRelationship(rId); Uri chartUri = PackUriHelper.ResolvePartUri(drawingsRelation.SourceUri, drawingsRelation.TargetUri); XElement drawingsElement = context.Package.GetXElementFromUri(chartUri); string chartType = ((XElement)(drawingsElement.Element(XName.Get("chart", ExcelCommon.Schema_Chart)).Element(XName.Get("plotArea", ExcelCommon.Schema_Chart)).Element(XName.Get("layout", ExcelCommon.Schema_Chart)).NextNode)).Name.LocalName; XElement element = childNode.Element(XName.Get("nvGraphicFramePr", ExcelCommon.Schema_SheetDrawings)).Element(XName.Get("cNvPr", ExcelCommon.Schema_SheetDrawings)); if (element != null) { string chartName = element.Attribute(XName.Get("name")).Value; ExcelChartType chartTypeEnum; Enum.TryParse(chartType.Replace("Chart", string.Empty), true, out chartTypeEnum); ExcelChart chart = target.AddChart(chartName, chartTypeEnum); chart.DrawingUri = target.DrawingUri; chart.RelationshipID = rId; ((IPersistable)chart).Load(context); drTarget = chart; } } if (drTarget != null) { ReadWrokSheetDrawings_Attribute(drTarget, xnode); ReadWrokSheetDrawings_clientData(drTarget, xnode); ReadWrokSheetDrawings_Common_Position(drTarget, xnode); } } }