Skip to content

Comments (Notes)

Mats Alm edited this page May 18, 2022 · 16 revisions

You can add/edit/delete Comments (in later Excel versions called Notes) either via the ExcelRange class or via the Comments Property on the ExcelWorksheet class. EPPlus supports Comments/Notes with plain as well as richtext, see examples below.

Examples

Add new comments

using(var package = new ExcelPackage(@"c:\temp\commentstest.xlsx"))
{
    var ws = package.Workbook.Worksheets.Add("comments");
    //Add Comments using the range class
    var comment = ws.Cells["A3"].AddComment("Jan Källman:\r\n", "JK");
    comment.Font.Bold = true;
    var rt = comment.RichText.Add("Here is some text that isn't bold...");
    rt.Bold = false;
    rt.FontName = "Helvetica";
    rt.Color = Color.Purple;
    comment.AutoFit = true;

    //Add a comment using the worksheet's Comments collection
    var comment2 = ws.Comments.Add(ws.Cells["B3"],"Jan Källman:", "JK");

    //This sets the size and position. (The position is only when the comment is visible)
    comment2.From.Column = 7;
    comment2.From.Row = 3;
    comment2.To.Column = 16;
    comment2.To.Row = 8;
    comment2.BackgroundColor = Color.White;
    comment2.RichText.Add("\r\nThis comment has white background and size/position set...\r\n");
}

Editing a comment

The code below is using the workbook that was created in the Add new comments section above.

using (var package = new ExcelPackage(@"c:\temp\commentstest.xlsx"))
{
    var ws = package.Workbook.Worksheets["comments"];
    // Edit Comment using the range class
    var comment = ws.Cells["A3"].Comment;
    comment.RichText[0].Color = Color.DarkGreen;
    comment.RichText[1].Color = Color.Blue;
    package.Save();
}

You can also edit comments via the Comments collection on the worksheet.

using (var package = new ExcelPackage(@"c:\temp\commentstest.xlsx"))
{
    var ws = package.Workbook.Worksheets["comments"];
    // Edit Comment using the Comments collection on the worksheet
    var comment = ws.Comments["A3"];
    comment.RichText[0].Color = Color.DarkRed;
    comment.RichText[1].Color = Color.Orange;
    package.Save();
}

Deleting a comment

The code below is using the workbook that was created in the Add new comments section above.

using (var package = new ExcelPackage(@"c:\temp\commentstest.xlsx"))
{
    var ws = package.Workbook.Worksheets["comments"];
    // Remove comment in cell B3 using the Comments collection on worksheet.
    ws.Comments.Remove(ws.Comments["B3"]);
    package.Save();
}

See also

Sample 20-.NET Framework or sample 20-.NET Core

EPPlus wiki

Versions

Worksheet & Ranges

Styling

Import/Export data

Formulas and filters

Charts & Drawing objects

Tables & Pivot Tables

VBA & Protection

Clone this wiki locally