Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 147793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:50:37+00:00 2026-05-11T08:50:37+00:00

I’m trying to add comments to cells in an Excel 2007 spreadsheet. I’m using

  • 0

I’m trying to add comments to cells in an Excel 2007 spreadsheet. I’m using the OpenXml SDK 2.0 to do so.

My use case is this: I’ve created a template Excel file that I copy and use that as my starting point, rather than create an OpenXML document from scratch. My template file has a comment in cell A1 so that Excel has already created a WorksheetCommentPart for me.

Now my problem is that when I add Comment nodes to the Comments part the spreadsheet doesn’t load and Excel asks if I want to recover.

What really bothers me is that my original comment in A1 is still there, but any comments I added programmatically are gone!

Here’s the code I’m working with:

using (MemoryStream spreadsheetStream = new MemoryStream()) { GetGradebookSpreadsheetTemplate(spreadsheetStream);

using (SpreadsheetDocument spDoc = SpreadsheetDocument.Open(spreadsheetStream, true)) {     WorkbookPart wbPart = spDoc.WorkbookPart;     WorksheetPart wsPart = wbPart.WorksheetParts.First();     SheetData sheet = wsPart.Worksheet.GetFirstChild<SheetData>();     Comments comments = wsPart.WorksheetCommentsPart.Comments;     comments.Descendants<Author>().First().Text = string.Format('{0}, {1}', instructor.LastName, instructor.FirstName);     comments.Descendants<Text>().First().Text = string.Format('{0}, {1}', instructor.LastName, instructor.FirstName);     List<DefinedName> definedNames = new List<DefinedName>();     definedNames.Add(CreateDefinedName('COLWeb_Gradebook', sheet.NamespaceURI, 'Gradebook', '1', 'A'));      uint index = 4;     foreach (User u in users)         CreateUserDataRow(index++, definedNames, comments.CommentList, sheet, u, coursesForUsers[u], assignments, submissions[u]);     Cell lastCell = sheet.Descendants<Cell>().Last();     OpenXmlElement dimensionsElement = wsPart.Worksheet.Elements().Where(x => x.LocalName == 'dimension').First();     dimensionsElement.SetAttribute(new OpenXmlAttribute('ref', null, 'A1:' + lastCell.CellReference));      comments.Save();     wsPart.Worksheet.Save();     wbPart.Workbook.Save(); }  return spreadsheetStream.ToArray(); 

}

And ‘CreateUserDataRow’ creates a new row, but the relevant part is (where ‘comment’ is my comment string and ‘c’ is my Cell that I want to create the comment about):

if (!string.IsNullOrEmpty(comment)) {     List<OpenXmlElement> runs = new List<OpenXmlElement>();      foreach (string row in comment.Split(new string[] { '<p>', '</p>' }, StringSplitOptions.RemoveEmptyEntries))     {         string trimmed = row.Trim();         if (!string.IsNullOrEmpty(trimmed))         {             string escaped = System.Security.SecurityElement.Escape(trimmed);             runs.Add(new Run(new RunProperties(), new Text(escaped)));         }     }      Comment commentCell = new Comment();     commentCell.Reference = c.CellReference;     commentCell.AuthorId = 0;     commentCell.AppendChild(new CommentText(runs));     comments.AppendChild(commentCell); } 

Now as far as my eye can see, and KDiff3 for that matter, my files are pretty much identical to the files that would be output if I were to open Excel and put the comments into the cells by hand in Excel.

Does anyone have a good example of attaching a comment to a cell with OpenXml? Is there something I should know about maybe a relationship? Does it have something to do with using an Excel file that I created and then I’m using as a template (maybe some dimensions aren’t set)?

Thanks for any help I can get.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:50:38+00:00Added an answer on May 11, 2026 at 8:50 am

    Unfortunately, it is not that simple.

    Cell comments also have a graphics object which is in a VML drawing part. VML is a cryptic legacy specification and is not in the approved ECMA standard. You can find documentation on it in Microsoft’s Open XML documents, but it is not pretty. Hopefully Microsoft will address this in Excel 14 by adding full cell comment support as well as support for controls which are also written to VML.

    Having said that, I have not used the Open XML SDK and I cannot say whether or not it is possible to add comments with it. I just thought this might help get you pointed in the right direction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.