解决方案:
示例代码如下:
/** * 给某一格设置注释 * @param sheet * @param rowIndex * @param colIndex * @param value */ public static void setCellCommon(Sheet sheet, int rowIndex, int colIndex, String value) { Row row = sheet.getRow(rowIndex); if (row == null) { // log.debug(ExcelConfig.ERROR + " setCellCommon," + sheet.getSheetName() + "第" + rowIndex + "为NULL"); return; } Cell cell = row.getCell(colIndex); if (cell == null) { cell = row.createCell(colIndex); } if(value == null){ cell.removeCellComment(); return; } Drawing drawing = sheet.createDrawingPatriarch(); CreationHelper factory = sheet.getWorkbook().getCreationHelper(); ClientAnchor anchor = factory.createClientAnchor(); //(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2) //前四个参数是坐标点,后四个参数是编辑和显示批注时的大小. //以上参数不设置时会有默认值,当一个被重复设置批注时会报错Multiple cell comments in one cell are not allowed //故在设置批注前检查锚点位置有无批注,有的话移除 Row row1 = sheet.getRow(anchor.getRow1()); if(row1 != null){ Cell cell1 = row1.getCell(anchor.getCol1()); if(cell1 != null){ cell1.removeCellComment(); } } Comment comment = drawing.createCellComment(anchor); RichTextString str = factory.createRichTextString(value); comment.setString(str); comment.setAuthor("Auto+"); cell.setCellComment(comment); }