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

Ask A Question

Stats

  • Questions 178k
  • Answers 178k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your code did not work (although it would have been… May 12, 2026 at 3:39 pm
  • Editorial Team
    Editorial Team added an answer You typically don't need to deal with the server certificate… May 12, 2026 at 3:39 pm
  • Editorial Team
    Editorial Team added an answer If I were in your shoes, here's a few possibilities… May 12, 2026 at 3:39 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.