package org.gnf.tom.test; import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class PoiTest2 { public static final String FILENAME = "poi_comment.xlsx"; public static final String SHEET_NAME = "Sheet1"; public static final String COMMENTS = "Cell Comments"; public static final String COMMENTS_AUTHOR = "Comments Author"; public static final String CELL_CONTENTS = "You should see comments in this cell"; public static void main(String[] args) throws Exception { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(SHEET_NAME); CreationHelper createHelper = wb.getCreationHelper(); // Create a cell in 0,0 Cell cell = sheet.createRow(0).createCell((short) 0); cell.setCellValue(new XSSFRichTextString(CELL_CONTENTS)); // Add comments to cell Comment comment = sheet.createComment(); comment.setString(createHelper.createRichTextString(COMMENTS)); comment.setAuthor(COMMENTS_AUTHOR); cell.setCellComment(comment); // Write out file FileOutputStream out = new FileOutputStream(FILENAME); wb.write(out); out.close(); // Read the workbook back into POI wb = new XSSFWorkbook(FILENAME); sheet = wb.getSheet(SHEET_NAME); comment = sheet.getCellComment(0, 0); if(comment.getString().toString().equals(COMMENTS)){ System.out.println("Comments readable from POI"); } if(comment.getAuthor().equals(COMMENTS_AUTHOR)){ System.out.println("Comment's author readable from POI"); } } }