How To Add Comments In Excel Sheet Cell Using Apache POI Java API


In an excel file, you might have seen there are comments on text cells or columns. So in this tutorial you will see how to add or insert comments in excel sheet cell using Apache POI Java API.

Comments can help you to remember what all are formulae or instructions you want to provide to other excel users.

In this example, I will create an excel file using Apache POI and Java programming language. In the excel file, I will create a sheet and on particular columns of particular rows, I will add or insert comments using Java and Apache POI library.


Java 8+, Apache POI 4.1.2 – 5.2.2, Gradle 5.6, Maven 3.8.5

Project Setup

I am going to create a gradle or maven based project in an IDE. The name of the project is java-apache-poi-excel-add-comments.

For maven based project you can use the following pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">





The build.gradle script is given below:

plugins {
    id 'java-library'

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {
    implementation 'org.apache.poi:poi-ooxml:4.1.2'
    //required only for jdk 9 or above

Add/Insert Comments into Excel Cell

Now you will see how to add or insert comments into excel cell or column.

First of all, I create a WorkBook object and Sheet from WorkBook object using the following lines of code:

Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();

Next I create CreationHelper object that will help us to create comment text.

Then I create a cell with some text value.

Next I create the drawing area to hold the comment text for the cell or column.

Finally I create the Comment object, set the comment text and author and set to the cell or column.

This is a simple comment without any format or text decoration but you can try with different attributes of Comment object.



import org.apache.poi.xssf.usermodel.XSSFCreationHelper;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelAddComments {

	public static void main(String[] args) throws IOException {
		Workbook workbook = new XSSFWorkbook();
		Sheet sheet = workbook.createSheet();
		CreationHelper creationHelper = (XSSFCreationHelper) workbook.getCreationHelper();		

		Cell cell = sheet.createRow(1).createCell(2);
		cell.setCellValue("Cell One String Value");

		Drawing<Shape> drawing = (Drawing<Shape>) sheet.createDrawingPatriarch();
		ClientAnchor clientAnchor = drawing.createAnchor(0, 0, 0, 0, 0, 2, 7, 12);

		Comment comment = (Comment) drawing.createCellComment(clientAnchor);

		RichTextString richTextString = creationHelper.createRichTextString(
				"We can put a long comment here with \n a new line text followed by another \n new line text");


		Cell cellOther = sheet.createRow(3).createCell(2);
		cellOther.setCellValue("Cell Other String Value");
		ClientAnchor clientAnchorOther = drawing.createAnchor(0, 0, 0, 0, 0, 7, 12, 17);

		Comment commentOther = (Comment) drawing.createCellComment(clientAnchorOther);

		RichTextString richTextStringOther = creationHelper.createRichTextString(
				"Other long comment here with \n a new line text followed by another \n new line text");

		commentOther.setAuthor("Roy Tutorials");

		FileOutputStream out = new FileOutputStream("excel-add-comments.xlsx");


Now let’s move on to Testing the Application section to see how comments look like.

Testing Comments in Excel Sheet

Once you are done with coding part then you can mouse over the text on a cell and you will see the comment automatically pops up.

In the below image I have only shown for one cell text. You can also check the other cell comment.

add comments in excel sheet cell using apache poi java api

Source Code


Leave a Reply

Your email address will not be published. Required fields are marked *