Excel Cell Comment

In this section, you will learn how to add a comment with a excel cell using Apache POI API.

Excel Cell Comment

Excel Cell Comment

In this section, you will learn how to add a comment with a excel cell using Apache POI API.

A comment is associated with a cell. It is a rich text note. The comment will be showing separately in rectangular text box. So we can say that comment stored separately but attached with a cell.

Given  below the code :

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class XLCellComment {
public static void main(String args[]) throws IOException{
Workbook wb = new HSSFWorkbook(); //or new HSSFWorkbook();

CreationHelper factory = wb.getCreationHelper();

Sheet sheet = (Sheet) wb.createSheet();

Row row = ((org.apache.poi.ss.usermodel.Sheet) sheet).createRow(3);
Cell cell = row.createCell(5);

Drawing drawing = ((org.apache.poi.ss.usermodel.Sheet) sheet).createDrawingPatriarch();

// When the comment box is visible, have it show in a 1x3 space
ClientAnchor anchor = factory.createClientAnchor();

// Create the comment and set the text+author
Comment comment = drawing.createCellComment(anchor);
RichTextString str = factory.createRichTextString("Hello, World!");
comment.setAuthor("Apache POI");

// Assign the comment to the cell

String fname = "comment-xssf.xls";
if(wb instanceof HSSFWorkbook) fname += "x";
FileOutputStream out = new FileOutputStream(fname);



Download Source Code