我们有很多由Apache POI生成的Excel报告。其中一些报告包含标题中的注释。由于有很多报告,我们想要创建通用解决方案来添加注释。正如我们所注意到的那样,可以通过以下代码将注释添加到单元格中:
public static void addComment(final Workbook workbook, final Sheet sheet, final Cell cell, final Row row,
final String comment) {
final CreationHelper factory = workbook.getCreationHelper();
final Drawing drawing = sheet.createDrawingPatriarch();
final ClientAnchor anchor = factory.createClientAnchor();
anchor.setCol1(cell.getColumnIndex());
anchor.setCol2(cell.getColumnIndex() + 3);
anchor.setRow1(row.getRowNum());
anchor.setRow2(row.getRowNum() + 5);
final Comment cellComment = drawing.createCellComment(anchor);
final RichTextString richText = factory.createRichTextString(comment);
cellComment.setString(richText);
cell.setCellComment(cellComment);
}
我们还注意到,评论框的大小可以使用列/行索引来设置 - 这对我们来说是主要问题,因为如果第一列有100像素,而第二列有1000像素,那么评论框的宽度将为1000像素。这里是我们的问题 - 是否有可能使用像素而不是列/行索引来设置评论框大小,使用apache poi?或者也许有一些方法可以使用apache poi自动计算评论框的大小?