Apache POIのコメント追加ができるようになってるみたい。

前までApachePOIでセルコメントの追加処理ができないみたいだったんだけども。
POI3.2Finalが出てたので試してみたらできた。

以下確認ソース

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.StringTokenizer;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/*
 * 作成日       :2008/11/18
 * パッケージ   :
 * ファイル名   :POIComment.java
 */

/**
 * <i>概要</i>: POIコメントテスト .
 * <p>
 * POIコメントテスト
 * 
 * 
 * @author kensir0u
 * @version 1.0
 * @since JDK 5.0
 * 
 */
public class POIComment {

	private static final String filename = System.getProperty("user.dir") + "\\コメント.xls";
	
	public static void comment() {
		
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("コメントテスト");
		HSSFPatriarch patr = sheet.createDrawingPatriarch();
		HSSFCell cell = sheet.createRow(1).createCell(1);
		cell.setCellValue(new HSSFRichTextString("セル値"));
		//コメントの設定
		cell.setCellComment(createComment(patr,"コメントテスト"));
		
		try {
			FileOutputStream fos = new FileOutputStream(filename);
			wb.write(fos);
			fos.flush();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static HSSFComment createComment(HSSFPatriarch patr,String commentText) {
		
		HSSFComment comment = patr.createComment(
				new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 8, 
						getCommentHeight(commentText)));
		
		comment.setString(new HSSFRichTextString(commentText));
		
		return comment;
	}
	
	public static void addComment()throws Exception{
		
	    //Excelファイルの読み込み		
	    FileInputStream fis = new FileInputStream(filename);		
	    POIFSFileSystem fs = new POIFSFileSystem(fis);		
	 		
	    //ワークブック・オブジェクトの取得		
	    HSSFWorkbook wb = new HSSFWorkbook(fs);
		HSSFSheet sheet = wb.getSheet("コメントテスト");
		HSSFRow row = sheet.getRow(1);
		HSSFCell cell = row.getCell(1);
		HSSFComment comment = cell.getCellComment();
		if (comment == null) {
		    HSSFPatriarch patr = sheet.createDrawingPatriarch();
		    cell.setCellComment(createComment(patr,"新規コメントテスト"));
		} else {
			comment.setString( new HSSFRichTextString(comment.getString() + "\n追加コメント"));
			cell.setCellComment(comment);
		}
		try {
			FileOutputStream fos = new FileOutputStream(filename);
			wb.write(fos);
			fos.flush();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static int getCommentHeight(String commentText) {
		final int defaultHeight = 3;
		
		if (commentText == null || commentText.length() == 0) return defaultHeight;
		
		StringTokenizer st = new StringTokenizer(commentText,"\n");
		return defaultHeight + st.countTokens();
	}

	public static void main(String[] args) throws Exception{
		//POIComment.comment();
		POIComment.addComment();
	}

}

PCにExcel入ってないので確認はOpenOfficeだからできてないかもしれないけども・・・