apache poi评论作者错过了

时间:2013-07-18 06:59:12

标签: java excel apache apache-poi

我正在使用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();

here is the generated output and expected output 请提出任何想法

1 个答案:

答案 0 :(得分:2)

正如@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);