[英]Adding Comment to a cell in a spreadsheet document created using OpenXML
I am creating an xlsx file using openXML. 我正在使用openXML创建一个xlsx文件。 I want to add comments to some row cells.
我想向某些行单元格添加注释。 Is there any way to add comments to the cell.
有什么方法可以向单元格添加评论。 Or should I use Microsoft.Office.Interop to add comments to the excel cells ?
还是应该使用Microsoft.Office.Interop向excel单元格添加注释?
I used the Open XML SDK Productivity Tool for Microsoft Office which lets you load an Excel or Word file and then emits the C# code to produce that exact file. 我使用了适用于Microsoft Office的Open XML SDK生产率工具,该工具可让您加载Excel或Word文件,然后发出C#代码以生成确切的文件。
Then I created a file with a comment. 然后,我创建了一个带有注释的文件。 First I tried with two comments but the amount of code it generates makes it hard to tell what's what.
首先,我尝试了两个注释,但是它生成的代码量使得很难分辨出什么是什么。
The result is not pretty to look at. 结果看起来并不漂亮。 You could reduce a lot of it and probably eliminate some.
您可以减少很多,甚至可以减少一些。 But it's a way to find out what's going on under the hood.
但这是找出幕后情况的一种方式。
You can also skip all of this and just use EPPlus , which after looking at what comes next you can imagine why someone felt the need to make this easier. 您也可以跳过所有这些,而仅使用EPPlus ,它在查看接下来的内容时,您可以想象为什么有人觉得有必要简化此过程。
The key steps are: 关键步骤是:
Comments
Comments
CommentsList
(?!!!) CommentsList
(?!!!) Comment
which has a Reference
property indicating which cell it belongs to Reference
属性的新Comment
,该Comment
指示它属于哪个单元格 CommentText
CommentText
Run
Run
Run
to the CommentText
Run
追加到CommentText
CommentText
to the Comment
CommentText
附加到Comment
Comment
to the CommentsList
Comment
追加到CommentsList
CommentsList
to the Comments
CommentsList
追加到Comments
Set the WorksheetCommentsPart.Comments
property to the Comments
. 将
WorksheetCommentsPart.Comments
属性设置为Comments
。
private void GenerateWorksheetCommentsPart1Content(WorksheetCommentsPart worksheetCommentsPart1) { Comments comments1 = new Comments(){ MCAttributes = new MarkupCompatibilityAttributes(){ Ignorable = "xr" } }; comments1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); comments1.AddNamespaceDeclaration("xr", "http://schemas.microsoft.com/office/spreadsheetml/2014/revision"); Authors authors1 = new Authors(); Author author1 = new Author(); author1.Text = "Hannen, Scott"; authors1.Append(author1); CommentList commentList1 = new CommentList(); Comment comment1 = new Comment(){ Reference = "B3", AuthorId = (UInt32Value)0U, ShapeId = (UInt32Value)0U }; comment1.SetAttribute(new OpenXmlAttribute("xr", "uid", "http://schemas.microsoft.com/office/spreadsheetml/2014/revision", "{811649EF-4CB5-4311-BE14-228133003BE4}")); CommentText commentText1 = new CommentText(); Run run1 = new Run(); RunProperties runProperties1 = new RunProperties(); FontSize fontSize3 = new FontSize(){ Val = 9D }; Color color3 = new Color(){ Indexed = (UInt32Value)81U }; RunFont runFont1 = new RunFont(){ Val = "Tahoma" }; RunPropertyCharSet runPropertyCharSet1 = new RunPropertyCharSet(){ Val = 1 }; runProperties1.Append(fontSize3); runProperties1.Append(color3); runProperties1.Append(runFont1); runProperties1.Append(runPropertyCharSet1); Text text1 = new Text(){ Space = SpaceProcessingModeValues.Preserve }; text1.Text = "This is my comment!\\nThis is line 2!\\n"; run1.Append(runProperties1); run1.Append(text1); commentText1.Append(run1); comment1.Append(commentText1); commentList1.Append(comment1); comments1.Append(authors1); comments1.Append(commentList1); worksheetCommentsPart1.Comments = comments1; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.