Insert cell comments in excel programmatically
Asked Answered
S

4

5

What's the better way to insert cell comments in excel 2007 files programmatically using c# and .net 3.5?

Stonewort answered 21/10, 2008 at 17:43 Comment(2)
How are you currently working with the document?Opening
I'm decompressing the open xml documents and writing the necessary comments?.xml by myself. I'm also writing the relationships, vmlDrawing docs and adding the legacyDrawing element in sheet?.xml. But when i compress the folder, change its extension to .xlsx and try open it in excel i get a error.Stonewort
E
7

I just did exactly that but with MS Word (using Microsoft.Office.Interop.Word

range.Comments.Add ( range, ref _categoryMessage );

So, I would suggest using Microsoft.Office.Interop.Excel and the similar method. Consider this from MSDN:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.range.addcomment

Also see this too

Eructate answered 21/10, 2008 at 17:59 Comment(6)
probably need to play with the editor to get the whole link to workDiorite
Thanks, Kenny. But when i do this my program is starting a process called excel. Does it means i need to have ms excel installed on my computer?Stonewort
Yes. Sorry, these interop calls require Excel.Eructate
Something also worth mentioning is that there are no Excel library redistributables when you use the Interop.Excel. So users of the application also need to have Excel installed on their machine. If you need to write the application for targets that don't have Excel installed locally, you need to modify the .xml directly. I believe there are some good utilities for this out there.Corticosterone
For those stumbling on this. Since this answer I've used ClosedXML to create/modify Excel files directly. I'm not 100% they allow access to comments, but highly likely. closedxml.codeplex.comEructate
The actual syntax is: Excel.Range a; a.AddComment("My comment");Secularism
S
3

The accepted answer points in the right direction, but the correct syntax is:

Excel.Range cell; 
cell.AddComment("My comment");
Secularism answered 20/8, 2013 at 19:35 Comment(0)
N
1
Excel._Worksheet oSheet =
  (Microsoft.Office.Interop.Excel._Worksheet) excelWorkbook.ActiveSheet;
oSheet.Cells[2, 3].Cells.AddComment("Selam");
Nanice answered 12/5, 2016 at 6:49 Comment(0)
O
0

Have you tried using VSTO ? You can easily load an Excel document and manipulate it. To add a comment to a cell, load the file, activate the worksheet, then select the cell as a range and set the comment.

Om answered 21/10, 2008 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.