In our previous article, Using NPOI for Spreadsheets in .NET, we went through and showed how you can create a Workbook with NPOI in C#. We also showed examples of how to read data from a spreadsheet and save the workbook as an XLSX. NPOI is a powerful open-source API that lets you work with Microsoft Office Excel Spreadsheets and Word Documents. In this article, we further explore the features of inserting a comment in an Excel worksheet using NPOI with .NET.

Insert Comment in Spreadsheet using Microsoft Excel

Before we can dive into the details of inserting a comment in a worksheet, let’s first have a look at how to insert a Comment in a worksheet using Microsoft Excel. This will give us a clear idea about what we are going to achieve at the end of this article.

What is a Comment in Excel?

A comment in Excel is a text that is associated with a particular cell for showing additional information. You can add a comment to a cell and others can reply to that comment to start a discussion. To view a comment in a cell, just hover your mouse on the cell containing the comment and it will show the contents of the comment.

Steps to Insert Comment in Excel Worksheet with Microsoft Excel

In order to insert a Comment in a specific cell in worksheet, the following steps can be used.

  1. Open Excel and select New from the File menu option
  2. Click in the Cell where you want to insert the comment
  3. Now either right-click and select New Comment or Select New Comment from the Insert menu at the top
  4. A pop-up window will appear where you enter the text for the Comment
  5. Once done, press Enter and the comment will be saved with the selected cell
  6. To view the comment, hover your mouse on the cell where you added the comment.
  7. This will show the comment
comment-in-excel-workbook

Insert Comment in Spreadsheet using NPOI in C#

Now that you have got an idea of how to insert a comment in an Excel worksheet using Microsoft Excel, let’s have a look at how application developers can leverage the power to carry out the same using NPOI POI in .NET.

Installing NPOI

Before you can start with using NPOI in your .NET application, you need to install NPOI in your project to get started. You can learn about this in our detailed instructions guide of Installing NPOI for .NET.

Insert a Comment in Excel Spreadsheet

At this stage, your development environment should be ready to start working with NPOI in your .NET project. Create a new Console-based project and add the following code sample to it.

//Create workbook
IWorkbook wb = new XSSFWorkbook();
            
ISheet ws = wb.CreateSheet("MySheet");

//Create the drawing patriarch
IDrawing drawing = ws.CreateDrawingPatriarch();
            
//Create cell and set its value
ICell cell = ws.CreateRow(2).CreateCell(2);
            
cell.SetCellValue("Comment is added here.");
            
//Create comment
IClientAnchor anchor = wb.GetCreationHelper().CreateClientAnchor();
            
IComment comment = drawing.CreateCellComment(anchor);
            
comment.String = new XSSFRichTextString("Comment in Cell using NPOI");
            
comment.Author = ("FileFormat.com");
            
cell.CellComment = (comment);
            
//Save the file
FileStream file = File.Create("ExcelComment.xlsx");
            
wb.Write(file, false);
            
file.Close();

That’s it. Save your project, build it, and execute it. You will find the output file ExcelComment.xlsx generated in the output folder of your project. Open it in Microsoft Excel and hover your mouse cursor on Cell C2 where we inserted the comment. You will see the comment as shown in the screenshot below.

Conclusion

In this article, we discussed about inserting a comment in an Excel worksheet using NPOI. The source code used in the project was written in C# that generated the output Excel with comments inserted to the worksheet. We’ll be further exploring NPOI API for working with Spreadsheets, so stay tuned.