2013-07-18 60 views
3

我使用的Apache POI 3.8,並試圖運行HSSF片與下面的代碼單元格批註的例子,但它在評論錯過筆者的Apache POI評論作者錯過

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 請提出想法

+0

爲什麼設置setVisible(false)?你看到評論嗎? – surfealokesea

+0

我只想在鼠標懸停時看到評論,但這與作者無關 –

+0

好吧,代碼似乎是好的,不知道爲什麼你看不到作者,你可以附上你的結果xls嗎? – surfealokesea

回答

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