我需要在Excel的HSSF单元格中添加注释。第一次运行时一切正常,但是如果我打开相同的文件并再次运行代码,它会破坏文件。
我还注意到,我只需要在工作表上创建一个Drawing对象:
_sheet.createDrawingPatriarch();
如果上面的行被执行多次,则注释将无法使用。
那么有人尝试过向单元格添加注释、关闭文件、再次打开文件,并尝试向不同单元格添加更多注释吗?
下面的代码可行,但如果我再次打开文件,就无法添加注释,而且文件会损坏!!!
是否有一种方法可以从工作表中获取现有的Drawing对象?
欢迎任何想法。谢谢!!
_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);