【问题标题】:apache poi comments author missedapache poi 评论作者错过
【发布时间】:2013-07-18 06:59:12
【问题描述】:

我正在使用 apache poi 3.8 并尝试使用下面的代码为 HSSF 表运行单元格注释示例,但它在注释中错过了作者

Workbook wb = new HSSFWorkbook(); //or new HSSFWorkbook();

    CreationHelper factory = wb.getCreationHelper();

    Sheet sheet = wb.createSheet();

    Row row  = sheet.createRow(3);
    Cell cell = row.createCell(5);
    cell.setCellValue("asdads");

    Drawing drawing = sheet.createDrawingPatriarch();

    ClientAnchor anchor = factory.createClientAnchor();
    anchor.setCol1(cell.getColumnIndex());
    anchor.setCol2(cell.getColumnIndex()+5);
    anchor.setRow1(row.getRowNum());
    anchor.setRow2(row.getRowNum()+2);

    // Create the comment and set the text+author
    Comment comment = drawing.createCellComment(anchor);
    RichTextString str = factory.createRichTextString("Hello, World! the new world");
    comment.setAuthor("roy");
    comment.setString(str);
    comment.setVisible(Boolean.FALSE);
    // Assign the comment to the cell
    cell.setCellComment(comment);

    System.out.println(comment.getAuthor());

    String fname = "C:/Users/roy/Desktop/comment.xls";

    FileOutputStream out = new FileOutputStream(fname);
    wb.write(out);
    out.close();

请提出任何想法

【问题讨论】:

  • 为什么要设置 setVisible(false)?看到评论了吗?
  • 我希望 cmets 只在鼠标悬停时可见,但这与作者无关
  • 好吧,代码好像没问题,不知道为什么看不到作者,可以附上你的结果xls吗?
  • 请查看图片
  • 这似乎是一个小错误。您可以将作者姓名设置为 setText() 的一部分,直到错误得到修复。

标签: java excel apache apache-poi


【解决方案1】:

正如@Avik 上面指出的,这可以很容易地设置......我不认为这是一个错误,因为在 Office 中这只是一个便利功能,因为评论不需要以作者姓名开头你可以删除它...

String author = "roy";
Comment comment = drawing.createCellComment(anchor);
RichTextString str = factory.createRichTextString(author+":\nHello, World! the new world");
str.applyFont(0, author.length()+2, font);
comment.setAuthor(author);
comment.setString(str);
comment.setVisible(Boolean.FALSE);
// Assign the comment to the cell
cell.setCellComment(comment);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多