Apache POI Excel Comment

I need to add a comment to an HSSF cell in Excel. Everything works fine for the first time, but if I open the same file and run the code again, it will damage the file.

I also noticed that I only need to create a Drawing object on the sheet once:

_sheet.createDrawingPatriarch ();

If the above line is executed more than once, the comments will not work.

So, did anyone try to add comments to the cells, close the file, open the file again and try to add more comments to different cells?

Below is the code, but if I open the file again, comments will not be added, and the file will be damaged.

Is there a way to get an existing Drawing object from a worksheet?

Any ideas are appreciated. Thank!!

_drawing = (HSSFPatriarch) _sheet.createDrawingPatriarch();

Row row = _sheet.getRow(rowIndex_);
Cell cell = row.getCell(0);
CreationHelper factory = _workbook.getCreationHelper();

HSSFAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short)6, 5);
        org.apache.poi.ss.usermodel.Comment comment = _drawing.createComment(anchor);
RichTextString str = factory.createRichTextString("Hello, World "+rowIndex_);
comment.setString(str);

  cell.setCellComment(comment);
+3
1

: .

, Javadoc createDrawingPatriarch() , (, , , , ). , , , ,

HSSFPatriarch drawing = document.getDrawingPatriarch()
if (drawing == null)
    drawing = document.createDrawingPatriarch()

- , getDrawingPatriarch(), , .

+2

Source: https://habr.com/ru/post/1744435/


All Articles