// 创建工作簿
XSSFWorkbook workBook = new XSSFWorkbook(inputStream);
// 创建sheet
XSSFSheet sheet = workBook.createSheet(sheetName);
XSSFDrawing draw = newSheet.createDrawingPatriarch();
// 定义注释的大小和位置
/*
* @param dx1 the x coordinate within the first cell.
* @param dy1 the y coordinate within the first cell.
* @param dx2 the x coordinate within the second cell.
* @param dy2 the y coordinate within the second cell.
* @param col1 the column (0 based) of the first cell.
* @param row1 the row (0 based) of the first cell.
* @param col2 the column (0 based) of the second cell.
* @param row2 the row (0 based) of the second cell.
*/
XSSFComment comment = draw.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, 4, 2, 9, 7));
XSSFRichTextString rtf = new XSSFRichTextString("添加批注内容!");
XSSFFont commentFormatter = workBook.createFont();
commentFormatter.setFontName("宋体");
//设置字体大小
commentFormatter.setFontHeightInPoints((short) 9);
rtf.applyFont(commentFormatter);
comment.setString(rtf);
comment.setAuthor("Zhihui Li");
// 将批注添加到单元格对象中
sheet.getRow(rowIndex).getCell(columnIndex).setCellComment(comment);
如果有不熟悉如何操作Excel的,可以参考前面的博文:
poi操作excel总结