2010-02-17 151 views
3

有人請向我解釋如何在創建單元格註釋時正確使用錨點?我的工作正在進行,但電子表格發生了變化,並且我的問題讓我的單元格評論出現。這是我使用的代碼:在apache poi中使用HSSFClientAnchor創建單元格註釋

Comment c = drawing.createCellComment (new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short)6, 5)); 

這大部分是通過實驗發現的。看看它的API並不完全明確。

基於快速入門指南我自己也嘗試沒有運氣以下:

ClientAnchor anchor = chf.createClientAnchor(); 
Comment c = drawing.createCellComment(anchor); 
c.setString(chf.createRichTextString(message)); 

回答

5

晚了一點,但是這可能會工作(它爲我工作,而從快速Apache的POI例子也開始對我沒有工作):

public void setComment(String text, Cell cell) { 
    final Map<Sheet, HSSFPatriarch> drawingPatriarches = new HashMap<Sheet, HSSFPatriarch>(); 

    CreationHelper createHelper = cell.getSheet().getWorkbook().getCreationHelper(); 
    HSSFSheet sheet = (HSSFSheet) cell.getSheet(); 
    HSSFPatriarch drawingPatriarch = drawingPatriarches.get(sheet); 
    if (drawingPatriarch == null) { 
     drawingPatriarch = sheet.createDrawingPatriarch(); 
     drawingPatriarches.put(sheet, drawingPatriarch); 
    } 

    Comment comment = drawingPatriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5)); 
    comment.setString(createHelper.createRichTextString(text)); 
    cell.setCellComment(comment); 
} 

埃裏克Pragt

+0

這基本上是我終於實現了。 – Casey

4

下面的代碼對我的作品的Office 2007(XLSX)格式的文件。從POI引導想通了這一點 http://poi.apache.org/spreadsheet/quick-guide.html#CellCommentsHow to set comments for 3 cells using apache poi

protected void setCellComment(Cell cell, String message) { 
    Drawing drawing = cell.getSheet().createDrawingPatriarch(); 
    CreationHelper factory = cell.getSheet().getWorkbook() 
      .getCreationHelper(); 
    // When the comment box is visible, have it show in a 1x3 space 
    ClientAnchor anchor = factory.createClientAnchor(); 
    anchor.setCol1(cell.getColumnIndex()); 
    anchor.setCol2(cell.getColumnIndex() + 1); 
    anchor.setRow1(cell.getRowIndex()); 
    anchor.setRow2(cell.getRowIndex() + 1); 
    anchor.setDx1(100); 
    anchor.setDx2(100); 
    anchor.setDy1(100); 
    anchor.setDy2(100); 

    // Create the comment and set the text+author 
    Comment comment = drawing.createCellComment(anchor); 
    RichTextString str = factory.createRichTextString(message); 
    comment.setString(str); 
    comment.setAuthor("Apache POI"); 
    // Assign the comment to the cell 
    cell.setCellComment(comment); 
} 
相關問題