We are generating excel using openxml library. Recently we added notes to appear as comment while exporting to excel for that particular column header.
I tried to add comments using below methods,but when I opened exported excel its not showing any comment
public byte[] ExportToExcelWithComments()
{
MemoryStream memoryStream = new MemoryStream();
SpreadsheetDocument spreadsheet = SpreadsheetDocument.Create(memoryStream, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook);
WorkbookPart workbookPart = spreadsheet.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
Sheets sheets = workbookPart.Workbook.AppendChild<Sheets>(new Sheets());
//OpenSheet
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
var sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "test" };
sheets.Append(sheet);
OpenXmlWriter DataWriter = OpenXmlWriter.Create(worksheetPart);
DataWriter.WriteStartElement(worksheetPart.Worksheet);
//Opendata
DataWriter.WriteStartElement(new SheetData());
WorksheetCommentsPart worksheetCommentsPart1 = worksheetPart.AddNewPart<WorksheetCommentsPart>();
GenerateWorksheetCommentsPart1Content(worksheetCommentsPart1);
//closedata
DataWriter.WriteEndElement();
DataWriter.Close();
//
//render
// worksheetPart = null;
workbookPart.Workbook.Save();
spreadsheet.Save();
spreadsheet.Close();
memoryStream.Position = 0;
return memoryStream.ToArray();
}
public void GenerateWorksheetCommentsPart1Content(WorksheetCommentsPart worksheetCommentsPart)
{
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 = "User1";
authors1.Append(author1);
CommentList commentList1 = new CommentList();
Comment comment1 = new Comment() { Reference = "A3", AuthorId = (UInt32Value)12, ShapeId = (UInt32Value)10, Guid = Guid.NewGuid().ToString() };
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);
worksheetCommentsPart.Comments = comments1;
}
Please let me know what is missing or issue with any code snippet.I am not bale to see the comments in exported excel
There is an example of how to add threaded comments on the Open XML SDK here.
I've copied and pasted the code below for reference: