gordon613
gordon613

Reputation: 2952

Changing size of cell comments in Apache POI

I use the following java code to successfully generate cell comments in 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);
        }

I copied it from creating cell comments using HSSFClientAnchor in apache poi. Thank you Erik!

How can I change the size of the comment to 300 pixels width and 100 pixels height?

Thanks!

Upvotes: 0

Views: 5839

Answers (3)

Viachaslau Kamkou
Viachaslau Kamkou

Reputation: 315

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);

Just change the values for setCol2 and setRow2.

Upvotes: 2

Dan
Dan

Reputation: 56

From what I can tell there's not an easy way since comment anchor points are specified by cell (column, row parameters) and offset into cell (dx, dy parameters). So you need to compute the width/height of the cells to figure out the second cell coordinates and then the offsets into that cell to make it exactly the pixel size you want.

Upvotes: 4

swamy
swamy

Reputation: 1210

You can assign a column width for any cell as follows:

sheet.setColumnWidth(0, 1000);

Upvotes: -2

Related Questions