Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Excel Cell Comment

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

Ads

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);
cell.setCellValue("F4");

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();
anchor.setCol1(cell.getColumnIndex());
anchor.setCol2(cell.getColumnIndex()+1);
anchor.setRow1(row.getRowNum());
anchor.setRow2(row.getRowNum()+3);

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

// Assign the comment to the cell
cell.setCellComment(comment);

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

}
}

OUTPUT

Download Source Code

Advertisements

Share on Google+Share on Google+

Excel Cell Comment

Posted on: August 25, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Excel Cell Comment  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Soham Desai
August 29, 2012
make comment visible false

hello sir/madam, i use above same code to get comment in XSSF excel or .xlsx file. but additionally i want comment as hidden .when my mouse over the cell that time only comment is visible othrewise it should be hidden. but in xssf when i do below code comment will not hide. comment.setVisible(true); but it's not working so please help me to hide comment in xlsx format using XSSF . thank you

Ads

 

Ads