la creación de los comentarios de células usando HSSFClientAnchor in poi Apache

StackOverflow https://stackoverflow.com/questions/2281221

  •  21-09-2019
  •  | 
  •  

Pregunta

Podría alguien por favor me explique cómo utilizar adecuadamente las anclas al crear comentarios de las celdas? Mina estaban trabajando, pero la hoja de cálculo cambiado y estoy teniendo problemas para conseguir que aparezcan mis comentarios de celdas. Este es el código que estaba usando que trabajaba:

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

Eso se encuentran principalmente mediante la experimentación alrededor. En cuanto a la API para que no se exactamente que sea más claro.

Sobre la base de la guía de inicio rápido También he intentado lo siguiente sin suerte:

ClientAnchor anchor = chf.createClientAnchor();
Comment c = drawing.createCellComment(anchor);
c.setString(chf.createRichTextString(message)); 
¿Fue útil?

Solución

Un poco tarde, pero esto probablemente va a trabajar (que funciona para mí, mientras que el ejemplo Apache POI desde el inicio rápido también no funcionaba para mí):

    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

Otros consejos

El siguiente código funciona para mí para archivos en formato Office 2007 (.xlsx). Cuenta de esto de guía PDI http://poi.apache.org/spreadsheet/quick-guide.html# CellComments y Cómo configurar los comentarios de 3 células por medio de 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);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top