Table of Contents

Notes and threaded comments

XlsxDocumentSheet.Comments lists a sheet's notes, Excel's legacy comments, and adds, changes and removes them as Excel's New Note, Edit Note, Show Note and Delete Note do. XlsxDocumentSheet.ThreadedComments reads the threaded comments Microsoft 365 writes, with their replies, and removes them. A note is written the way Excel writes its own, measured against Excel: Excel opens what DocWright writes without repair and lists its notes as written.

dotnet add package DocWright.Spreadsheet

Add notes

using DocWright.Spreadsheet;

budget.Comments.Add("B2", "Includes the January rise.", "Ada Lovelace");
budget.Comments.Add(3, 2, "Estimate:\nconfirm with the supplier", "Grace Hopper");   // row 3, column 2
XlsxComment heading = budget.Comments.Add("A1", "Costs per month");
heading.Visible = true;                                                              // Excel's Show Note

try
{
    budget.Comments.Add("B2", "A second one");
}
catch (InvalidOperationException error)
{
    Console.WriteLine(error.Message);
}

A note is on one cell, with a text and an author; a line break is \n. A cell has one note: Add refuses a cell that has one (or a threaded comment), and a refused call changes nothing. Visible is Excel's Show Note: the note stays on the sheet, not only when the pointer is over its cell.

Each note gets the box Excel gives a new one: 108 by 59.25 points, just right of the cell and a little above it, or to its left at the sheet's last column. The sheet's column widths and row heights are followed, hidden ones taking no room. The first note of a sheet adds the parts a note needs; the last one removed takes them out.

Change a note

using DocWright.Spreadsheet;

XlsxComment rise = budget.Comments.Find("B2")!;
rise.Text = "Includes the January rise of 4%.";
rise.Author = "Grace Hopper";

foreach (XlsxComment note in budget.Comments)
{
    Console.WriteLine($"{note.Reference,-4} {note.Author,-13} shown: {note.Visible,-5} {note.Text.Replace("\n", " / ")}");
}

Text is the note's runs joined, without their formatting. Setting it writes one run in the look of the note's last run, so formatting inside a note (a bold word, a colour) is not kept when you change the text. Excel's own New Note puts the author's name in bold at the start of the text; that is yours to add if you want it.

Notes follow their cells

using DocWright.Spreadsheet;

budget.InsertRows(2);
Console.WriteLine("After inserting a row: " + string.Join(", ", budget.Comments.Select(c => c.Reference)));

Inserting and deleting rows and columns, sorting and copying a sheet move notes with their cells, boxes included. A note whose cell is deleted goes with it. Download the workbook: budget-notes.xlsx.

Remove notes

using DocWright.Spreadsheet;

XlsxDocument reopened = XlsxDocument.Open(File.OpenRead("budget-notes.xlsx"));
XlsxDocumentSheet sheet = reopened.Sheets[0];
sheet.Comments.Remove(sheet.Comments.Find("A1")!);
Console.WriteLine($"Removed {sheet.Comments.Remove(new XlsxCellRange(1, 1, 3, 2))} more; {sheet.Comments.Count} left: {sheet.Comments.Single().Reference}");

Remove(note) takes one note; Remove(range) takes every note on a cell in the range, as Delete Note over a selection does, and says how many.

Threaded comments

using DocWright.Spreadsheet;

XlsxDocument review = XlsxDocument.Open(File.OpenRead("review.xlsx"));   // saved by Microsoft 365
XlsxDocumentSheet first = review.Sheets[0];
foreach (XlsxThreadedComment thread in first.ThreadedComments)
{
    Console.WriteLine($"{thread} ({thread.Author}), {thread.Replies.Count} repl(ies)");
    foreach (XlsxThreadedComment reply in thread.Replies)
    {
        Console.WriteLine($"    {reply.Author}: {reply.Text}");
    }
}

first.ThreadedComments.Remove(first.ThreadedComments[0]);
Console.WriteLine($"Threads left: {first.ThreadedComments.Count}; notes: {first.Comments.Count}");

A threaded comment is not a note, as Excel counts them. A workbook keeps it in a threads part, with its people in a persons part and a plain note for versions of Excel without threads. That note is not in Comments, and Remove takes it with the thread, its replies and its box. Time is as the file states it; no time zone is written.

Note

Threaded comments are read and removed; adding a thread or a reply is not supported. Add a note instead, which every version of Excel shows.

Excel, measured
Parts A note is a comment in the sheet's comments part and a box in its legacy drawing, which the sheet names. Excel refuses a workbook with a note that has no box, or whose box names another cell.
Order Notes are kept in row order, then column order; out of order, Excel loses one. A note added goes where the order puts it.
Shown Read from the box's own flag, as Excel reads it; the style beside it is written too.
Author Any text. No author is written as an empty name, which Excel opens.
Strict A Strict Open XML workbook gets its note in Strict namespaces, its drawing's relationship in the transitional one, as Excel writes it.

Notes are held in memory while you edit them and written into the workbook once, when the parts are next read or the workbook is saved: 8,000 notes are added and saved in about a tenth of a second.

The whole run prints:

The cell B2 has a note already; a cell has one. Change its text, or remove it first.
A1                 shown: True  Costs per month
B2   Grace Hopper  shown: False Includes the January rise of 4%.
B3   Grace Hopper  shown: False Estimate: / confirm with the supplier
After inserting a row: A1, B3, B4
Removed 1 more; 1 left: B4
B2: First thought (Pat Example), 1 repl(ies)
    Pat Example: A reply
C3: Second thread (Pat Example), 0 repl(ies)
Threads left: 1; notes: 1