更改Apache POI中单元格注释的大小

时间:2012-08-02 15:01:59

标签: java apache-poi

我使用以下java代码在Apache POI中成功生成单元格注释

public static void setComment(String text, Cell cell) {
            final Map<Sheet, HSSFPatriarch> drawingPatriarches = new HashMap<Sheet, HSSFPatriarch>();

            CreationHelper createHelper = cell.getSheet().getWorkbook().getCreationHelper();
            HSSFSheet sheet = (HSSFSheet) cell.getSheet();
            HSSFPatriarch drawingPatriarch = drawingPatriarches.get(sheet);
            if (drawingPatriarch == null) {
                drawingPatriarch = sheet.createDrawingPatriarch();
                drawingPatriarches.put(sheet, drawingPatriarch);
            }

            Comment comment = drawingPatriarch.createComment(new HSSFClientAnchor(100, 100, 100, 100, (short)1, 1, (short) 10, 5));
            comment.setString(createHelper.createRichTextString(text));
            cell.setCellComment(comment);
        }

我是从creating cell comments using HSSFClientAnchor in apache poi复制的。谢谢Erik!

如何将注释的大小更改为300像素宽和100像素高?

谢谢!

3 个答案:

答案 0 :(得分:4)

据我所知,这不是一个简单的方法,因为注释锚点由单元格(列,行参数)指定并偏移到单元格(dx,dy参数)。因此,您需要计算单元格的宽度/高度,以计算出第二个单元格坐标,然后计算出该单元格的偏移量,使其完全符合您想要的像素大小。

答案 1 :(得分:1)

Busy Developers' Guide to HSSF and XSSF Features

// When the comment box is visible, have it show in a 1x3 space
ClientAnchor anchor = factory.createClientAnchor();
anchor.setCol1(cell.getColumnIndex());
anchor.setCol2(cell.getColumnIndex()+1);
anchor.setRow1(row.getRowNum());
anchor.setRow2(row.getRowNum()+3);

只需更改setCol2和setRow2的值。

答案 2 :(得分:-1)

您可以为任何单元格指定列宽,如下所示:

sheet.setColumnWidth(0, 1000);