Domanda

Qualcuno potrebbe spiegare a me come usare correttamente le ancore per la creazione di commenti di cella? La mia lavoravano, ma il foglio di calcolo cambiato e sto avendo problemi di ottenere a comparire i miei commenti di cella. Questo è il codice che stavo usando che ha funzionato:

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

che è stato trovato per lo più attraverso la sperimentazione in giro. Guardando l'API per non esattamente lo rende più chiaro.

In base alla guida rapida Ho anche provato quanto segue senza fortuna:

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

Soluzione

Un po 'in ritardo, ma questo probabilmente funzionerà (per me funziona, mentre l'esempio di Apache POI dal avvio rapido, inoltre, non ha funzionato per me):

    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

Altri suggerimenti

Il seguente codice funziona per me di file in formato 2007 (xlsx) di Office. Capito questo da guida POI http://poi.apache.org/spreadsheet/quick-guide.html# CellComments e Come impostare commenti per 3 celle che utilizzano 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);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top