简体   繁体   English

向使用OpenXML创建的电子表格文档中的单元格添加注释

[英]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 OfficeOpen 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: 关键步骤是:

  • Create a new Comments 创建一个新Comments
  • Create a new CommentsList (?!!!) 创建一个新的CommentsList (?!!!)
  • Create a new Comment which has a Reference property indicating which cell it belongs to 创建一个具有Reference属性的新Comment ,该Comment指示它属于哪个单元格
  • Create a new CommentText 创建一个新的CommentText
  • Create a new Run 创建一个新的Run
  • Append the Run to the CommentText Run追加到CommentText
  • Append the CommentText to the Comment CommentText附加到Comment
  • Append the Comment to the CommentsList Comment追加到CommentsList
  • Append the 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM