import org.apache.poi.xssf.usermodel.*; import java.io.FileOutputStream; import java.io.IOException; public class PoiAddComments { public static void main(String[] args) throws IOException { // 創建工作簿對象 XSSFWorkbook wb = new XSSFWorkbook(); // 創建工作表對象 XSSFSheet sheet = wb.createSheet("測試添加批注"); // 創建繪圖對象 XSSFDrawing p = sheet.createDrawingPatriarch(); // 創建單元格對象,批注插入到1行,1列,B5單元格 XSSFCell cell = sheet.createRow(0).createCell(0); // 插入單元格內容 cell.setCellValue(new XSSFRichTextString("批注")); // 獲取批注對象 // (int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2) // 前四個參數是坐標點,后四個參數是編輯和顯示批注時的大小. XSSFComment comment = p.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 3, (short) 5, 6)); // 輸入批注信息 comment.setString(new XSSFRichTextString("這是批注內容!")); // 添加作者,選中B5單元格,看狀態欄 comment.setAuthor("rongrong"); // 將批注添加到單元格對象中 cell.setCellComment(comment); // 創建輸出流 FileOutputStream out = new FileOutputStream("d:/PoiAddComments.xlsx"); wb.write(out); // 關閉流對象 out.close(); } }