private XElement WriteWorkSheet_legacyDrawing(WorkSheet sheet) { XElement commentsParts = new XElement(ExcelCommon.Schema_WorkBook_Main + "legacyDrawing"); CommentCollection comments = new CommentCollection(sheet); ((IPersistable)comments).Save(this.Context); commentsParts.Add(new XAttribute(XName.Get("id", ExcelCommon.Schema_Relationships), this.Context.CommentsSheetRelationships[sheet.Name])); return commentsParts; }
internal void WriteComments(CommentCollection sheetComments) { if (this.Context.Comments.ContainsKey(sheetComments._WorkSheet.Name)) { List<Comment> cellsComment = this.Context.Comments[sheetComments._WorkSheet.Name]; Dictionary<string, int> authorsList = new Dictionary<string, int>(); SortedDictionary<int, SortedDictionary<int, XElement>> cells_Comments = new SortedDictionary<int, SortedDictionary<int, XElement>>(); SortedDictionary<int, SortedDictionary<int, XElement>> cells_Comments_Drawing_shape = new SortedDictionary<int, SortedDictionary<int, XElement>>(); int index = 1; foreach (Comment cellComment in cellsComment) { int rowIndex = cellComment._Cell.Row.Index, columnIndex = cellComment._Cell.Column.Index; if (cells_Comments.ContainsKey(rowIndex)) { cells_Comments[rowIndex].Add(columnIndex, WriteComments_comment(cellComment, authorsList)); cells_Comments_Drawing_shape[rowIndex].Add(columnIndex, WriteCommentsVmlDrawing_shape(cellComment, rowIndex, columnIndex, index)); } else { SortedDictionary<int, XElement> rowCell = new SortedDictionary<int, XElement>(); rowCell.Add(columnIndex, WriteComments_comment(cellComment, authorsList)); cells_Comments.Add(rowIndex, rowCell); SortedDictionary<int, XElement> rowCell_VmlDrawing_shap = new SortedDictionary<int, XElement>(); rowCell_VmlDrawing_shap.Add(columnIndex, WriteCommentsVmlDrawing_shape(cellComment, rowIndex, columnIndex, index)); cells_Comments_Drawing_shape.Add(rowIndex, rowCell_VmlDrawing_shap); } index++; } this.Context.CommentsDrawing.Add(sheetComments._WorkSheet.Name, cells_Comments_Drawing_shape); cells_Comments_Drawing_shape = null; XDocument commentsDocXml = new XDocument(new XDeclaration("1.0", "utf-8", "yes")); XElement commentsNode = new XElement(XName.Get("comments", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); XElement authorsNode = new XElement(XName.Get("authors", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); foreach (KeyValuePair<string, int> authorkey in authorsList) { authorsNode.Add(new XElement(XName.Get("author", ExcelCommon.Schema_WorkBook_Main.NamespaceName)) { Value = authorkey.Key }); } commentsNode.Add(authorsNode); XElement commentListNode = new XElement(XName.Get("commentList", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); foreach (KeyValuePair<int, SortedDictionary<int, XElement>> rowKey in cells_Comments) { foreach (KeyValuePair<int, XElement> commentKey in rowKey.Value) { commentListNode.Add(commentKey.Value); } } commentsNode.Add(commentListNode); commentsDocXml.Add(commentsNode); Uri CommentUri = new Uri(string.Format(@"/xl/comments{0}.xml", sheetComments._WorkSheet.PositionID + 1), UriKind.Relative); sheetComments.RelationshipID = this.Context.Package.GetPart(sheetComments._WorkSheet.SheetUri).CreateRelationship(PackUriHelper.GetRelativeUri(sheetComments._WorkSheet.SheetUri, CommentUri), TargetMode.Internal, ExcelCommon.Schema_Relationships + "/comments").Id; PackagePart commentPart = this.Context.Package.CreatePart(CommentUri, ExcelCommon.ContentType_Comments, sheetComments._WorkSheet.WorkBook.Compression); using (Stream stream = commentPart.GetStream(FileMode.Create, FileAccess.Write)) { commentsDocXml.Save(stream); stream.Flush(); } cellsComment = null; authorsList = null; cells_Comments = null; } }
internal void WriteCommentsVmlDrawing(CommentCollection sheetComments) { XDocument vmlDrawingXml = new XDocument(); XElement vmlDrawingXml_xml = new XElement("xml"); vmlDrawingXml_xml.Add(new XAttribute(XName.Get("v", XNamespace.Xmlns.NamespaceName), ExcelCommon.schemaMicrosoftVml)); vmlDrawingXml_xml.Add(new XAttribute(XName.Get("o", XNamespace.Xmlns.NamespaceName), ExcelCommon.schemaMicrosoftOffice)); vmlDrawingXml_xml.Add(new XAttribute(XName.Get("x", XNamespace.Xmlns.NamespaceName), ExcelCommon.schemaMicrosoftExcel)); XElement vmlDrawingXml_shapelayout = new XElement(XName.Get("shapelayout", ExcelCommon.schemaMicrosoftOffice), new XAttribute(XName.Get("ext", ExcelCommon.schemaMicrosoftVml), "edit")); XElement vmlDrawingXml_shapelayout_idmap = new XElement(XName.Get("idmap", ExcelCommon.schemaMicrosoftOffice), new XAttribute(XName.Get("ext", ExcelCommon.schemaMicrosoftVml), "edit"), new XAttribute("data", 1)); vmlDrawingXml_shapelayout.Add(vmlDrawingXml_shapelayout_idmap); vmlDrawingXml_xml.Add(vmlDrawingXml_shapelayout); XElement vmlDrawingXml_shapetype = new XElement(XName.Get("shapetype", ExcelCommon.schemaMicrosoftVml), new XAttribute("id", "_x0000_t202"), new XAttribute("coordsize", "21600,21600"), new XAttribute(XName.Get("spt", ExcelCommon.schemaMicrosoftOffice), 202), new XAttribute("path", "m,l,21600r21600,l21600,xe")); XElement vmlDrawingXml_shapetype_stroke = new XElement(XName.Get("stroke", ExcelCommon.schemaMicrosoftVml), new XAttribute("joinstyle", "miter")); vmlDrawingXml_shapetype.Add(vmlDrawingXml_shapetype_stroke); XElement vmlDrawingXml_shapetype_path = new XElement(XName.Get("path", ExcelCommon.schemaMicrosoftVml), new XAttribute("gradientshapeok", "t"), new XAttribute(XName.Get("connecttype", ExcelCommon.schemaMicrosoftOffice), "rect")); vmlDrawingXml_shapetype.Add(vmlDrawingXml_shapetype_path); vmlDrawingXml_xml.Add(vmlDrawingXml_shapetype); if (this.Context.CommentsDrawing.ContainsKey(sheetComments._WorkSheet.Name)) { SortedDictionary<int, SortedDictionary<int, XElement>> shapeDic = this.Context.CommentsDrawing[sheetComments._WorkSheet.Name]; foreach (KeyValuePair<int, SortedDictionary<int, XElement>> shapeKey in shapeDic) { foreach (KeyValuePair<int, XElement> itemXmlNode in shapeKey.Value) { vmlDrawingXml_xml.Add(itemXmlNode.Value); } } shapeDic = null; this.Context.CommentsDrawing.Remove(sheetComments._WorkSheet.Name); } vmlDrawingXml.Add(vmlDrawingXml_xml); string relationshipID = this.Context.Package.GetPart(sheetComments._WorkSheet.SheetUri).CreateRelationship(PackUriHelper.GetRelativeUri(sheetComments._WorkSheet.SheetUri, sheetComments.vmlDrawingsUri), TargetMode.Internal, ExcelCommon.Schema_Relationships + "/vmlDrawing").Id; PackagePart vmlDrawingsPart = this.Context.Package.CreatePart(sheetComments.vmlDrawingsUri, ExcelCommon.ContentType_vmlDrawing, sheetComments._WorkSheet.WorkBook.Compression); using (StreamWriter stream = new StreamWriter(vmlDrawingsPart.GetStream(FileMode.Create, FileAccess.Write))) { stream.Write(vmlDrawingXml.ToString(SaveOptions.None).Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", string.Empty)); stream.Flush(); } this.Context.CommentsSheetRelationships.Add(sheetComments._WorkSheet.Name, relationshipID); }
internal void ReadSheetCommentVmlDrawing_shape_ClientData(CommentCollection target, XElement clientDataElement, Comment comment) { XElement childElement = clientDataElement.Element(XName.Get("ClientData", ExcelCommon.schemaMicrosoftExcel)); if (childElement != null) { ReadSheetCommentVmlDrawing_shape_ClientData_TextVAlign(childElement, comment); ReadSheetCommentVmlDrawing_shape_ClientData_TextHAlign(childElement, comment); ReadSheetCommentVmlDrawing_shape_ClientData_Visible(childElement, comment); ReadSheetCommentVmlDrawing_shape_ClientData_Locked(childElement, comment); ReadSheetCommentVmlDrawing_shape_ClientData_AutoFill(childElement, comment); ReadSheetCommentVmlDrawing_shape_ClientData_LockText(childElement, comment); ReadSheetCommentVmlDrawing_shape_ClientData_anchor(childElement, comment); } }
internal void ReadSheetCommentVmlDrawing_shape(CommentCollection target, XElement vmlDrawingElement) { IEnumerable<XElement> shapetypes = vmlDrawingElement.Elements(XName.Get("shape", ExcelCommon.schemaMicrosoftVml)); List<Comment> listComment = new List<Comment>(); foreach (XElement shapetypeNode in shapetypes) { Comment ct = new Comment(); ReadSheetCommentVmlDrawing_shape_BackgroundColor(shapetypeNode, ct); ReadSheetCommentVmlDrawing_shape_stroke(shapetypeNode, ct); ReadSheetCommentVmlDrawing_shape_strokecolor(shapetypeNode, ct); ReadSheetCommentVmlDrawing_shape_strokeweight(shapetypeNode, ct); ReadSheetCommentVmlDrawing_shape_textbox(shapetypeNode, ct); if (shapetypeNode.Attribute(XName.Get("id", ExcelCommon.schemaMicrosoftVml)) != null) { ct.ID = shapetypeNode.Attribute(XName.Get("id", ExcelCommon.schemaMicrosoftVml)).Value; } ReadSheetCommentVmlDrawing_shape_ClientData(target, shapetypeNode, ct); listComment.Add(ct); } this.Context.DrawingShapes.Add(target._WorkSheet.Name, listComment); }
internal void ReadSheetCommentVmlDrawing(CommentCollection target, XElement vmlDrawingElement) { XElement childNode = vmlDrawingElement.Element(XName.Get("shapelayout", ExcelCommon.schemaMicrosoftOffice)); if (childNode != null) { } childNode = vmlDrawingElement.Element(XName.Get("shapetype", ExcelCommon.schemaMicrosoftVml)); if (childNode != null) { } ReadSheetCommentVmlDrawing_shape(target, vmlDrawingElement); }
internal void ReadSheetComments(CommentCollection target, XElement commentElement) { List<string> authors = new List<string>(); XElement childNode = commentElement.Element(XName.Get("authors", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (childNode != null) { IEnumerable<XElement> authorsNodes = childNode.Elements(XName.Get("author", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); foreach (XElement author in authorsNodes) { authors.Add(author.Value); } } childNode = commentElement.Element(XName.Get("commentList", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (childNode != null) { IEnumerable<XElement> commentListNode = childNode.Elements(XName.Get("comment", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); int index = 0; List<Comment> driwingComments = this.Context.DrawingShapes[target._WorkSheet.Name]; foreach (XElement commentMode in commentListNode) { if (commentMode.Attribute("ref") != null) { Cell cell = target._WorkSheet.Cells[commentMode.Attribute("ref").Value]; Comment cellComment = driwingComments[index]; cellComment._Cell = cell; ReadSheetComments_Comment(authors, commentMode, cellComment); cell._Comment = cellComment; index++; } } } }
/// <summary> /// FileName:sheet1.xml /// <para>NodePath:worksheet</para> /// </summary> /// <param name="bookViewsRoot"></param> internal void ReadWorkSheet(WorkSheet target, XElement sheetRoot) { XElement sheetNode = sheetRoot.Element(XName.Get("dimension", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { if (sheetNode.Attribute("ref") != null) { target.Dimension = Range.Parse(target, sheetNode.Attribute("ref").Value); } } sheetNode = sheetRoot.Element(XName.Get("sheetViews", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { ReadWorkSheet_sheetViews(target, sheetNode); } sheetNode = sheetRoot.Element(XName.Get("cols", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { ReadWorkSheet_cols(target, sheetNode); } sheetNode = sheetRoot.Element(XName.Get("sheetData", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { ReadWorkSheet_sheetData(target, sheetNode); } sheetNode = sheetRoot.Element(XName.Get("hyperlinks", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { ReadWorkSheet_hyperlinks(target, sheetNode); } sheetNode = sheetRoot.Element(XName.Get("phoneticPr", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { foreach (XAttribute att in sheetNode.Attributes()) { target.PhoneticProperties.SetAttribute(att.Name.LocalName, att.Value); } } sheetNode = sheetRoot.Element(XName.Get("dataValidations", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { ReadWorkSheet_dataValidations(target, sheetNode); } sheetNode = sheetRoot.Element(XName.Get("extLst", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { ReadWorkSheet_dataValidationsExtList(target, sheetNode); } sheetNode = sheetRoot.Element(XName.Get("pageMargins", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { ReadWorkSheet_pageMargins(target, sheetNode); } sheetNode = sheetRoot.Element(XName.Get("pageSetup", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { ReadWorkSheet_pageSetup(target, sheetNode); } sheetNode = sheetRoot.Element(XName.Get("printOptions", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { ReadWorkSheet_printOptions(target, sheetNode); } sheetNode = sheetRoot.Element(XName.Get("tableParts", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { ReadWorkSheet_tableParts(target, sheetNode); } sheetNode = sheetRoot.Element(XName.Get("sheetProtection", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { ReadWorkSheet_sheetProtection(target, sheetNode); } sheetNode = sheetRoot.Element(XName.Get("mergeCells", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { ReadWorkSheet_mergeCells(target, sheetNode); } sheetNode = sheetRoot.Element(XName.Get("sheetPr", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { ReadWorkSheet_sheetPr(target, sheetNode); } sheetNode = sheetRoot.Element(XName.Get("legacyDrawing", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { CommentCollection sheetComments = new CommentCollection(target, sheetNode.Attribute(XName.Get("id", ExcelCommon.Schema_Relationships)).Value); ((IPersistable)sheetComments).Load(this.Context); this.Context.Comments.Add(target.Name, sheetComments); } sheetNode = sheetRoot.Element(XName.Get("headerFooter", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { target._HeaderFooter = new HeaderFooter(target); XElement drawingHFNode = sheetRoot.Element(XName.Get("legacyDrawingHF", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (drawingHFNode != null) { target._HeaderFooter._Pictures = new VmlDrawingPictureCollection(target, drawingHFNode.Attribute(XName.Get("id", ExcelCommon.Schema_Relationships)).Value); ((IPersistable)target._HeaderFooter._Pictures).Load(this.Context); } ReadWorkSheet_headerFooter(target, sheetNode); } //XElement(XName.Get("drawing", ExcelCommon.Schema_WorkBook_Main.NamespaceName), new XAttribute(XName.Get("id", ExcelCommon.Schema_Relationships) sheetNode = sheetRoot.Element(XName.Get("drawing", ExcelCommon.Schema_WorkBook_Main.NamespaceName)); if (sheetNode != null) { target._Drawings = new DrawingCollection(target, sheetNode.Attribute(XName.Get("id", ExcelCommon.Schema_Relationships)).Value); ((IPersistable)target._Drawings).Load(this.Context); } }