2010-02-17 15 views
3

Est-ce que quelqu'un pourrait m'expliquer comment utiliser correctement les Ancres lors de la création de commentaires de cellules? Les miennes travaillaient, mais la feuille de calcul a changé et j'ai des problèmes pour faire apparaître mes commentaires. C'est le code que j'utilisais qui fonctionnait:création de commentaires de cellule à l'aide de HSSFClientAnchor dans apache poi

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

Cela a été trouvé principalement en expérimentant autour. Regarder l'api pour cela ne le rend pas vraiment plus clair.

Basé sur le guide de démarrage rapide J'ai aussi essayé ce qui suit sans succès:

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

Répondre

5

Un peu en retard, mais cela fonctionnera probablement (ça marche pour moi, alors que l'exemple Apache POI du rapide commencer aussi ne fonctionne pas pour moi):

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

Erik Pragt

+0

Ceci est essentiellement ce que je fini par faire. – Casey

4

Le code suivant fonctionne pour moi pour les fichiers au format Office 2007 (xlsx). Compris cela du guide de POI http://poi.apache.org/spreadsheet/quick-guide.html#CellComments et How 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); 
}