Example usage for org.apache.poi.ss.usermodel CreationHelper createRichTextString

List of usage examples for org.apache.poi.ss.usermodel CreationHelper createRichTextString

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel CreationHelper createRichTextString.

Prototype

RichTextString createRichTextString(String text);

Source Link

Document

Creates a new RichTextString instance

Usage

From source file:de.enerko.reports2.engine.Report.java

License:Apache License

/**
 * This method adds a new cell to the sheet of a workbook. It could 
 * (together with {@link #fill(Workbook, Cell, String, String, boolean)}) be moved to
 * the {@link CellDefinition} itself, but that would mean that the {@link CellDefinition} is
 * tied to a specific Excel API. Having those methods here allows the Report to become
 * an interface if a second engine (i.e. JXL) should be added in the future.
 * @param workbook/*from   w  w w  .  ja v  a2s .c  o m*/
 * @param sheet
 * @param cellDefinition
 */
private void addCell(final Workbook workbook, final Sheet sheet, final CellDefinition cellDefinition) {
    final int columnNum = cellDefinition.column, rowNum = cellDefinition.row;

    Row row = sheet.getRow(rowNum);
    if (row == null)
        row = sheet.createRow(rowNum);

    Cell cell = row.getCell(columnNum);
    // If the cell already exists and is no blank cell
    // it will be used including all formating
    if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) {
        cell = fill(workbook, cell, cellDefinition, false);
    }
    // Otherwise a new cell will be created, the datatype set and 
    // optionally a format will be created
    else {
        cell = fill(workbook, row.createCell(columnNum), cellDefinition, true);

        final Sheet referenceSheet;
        if (cellDefinition.getReferenceCell() != null
                && (referenceSheet = workbook.getSheet(cellDefinition.getReferenceCell().sheetname)) != null) {
            final Row referenceRow = referenceSheet.getRow(cellDefinition.getReferenceCell().row);
            final Cell referenceCell = referenceRow == null ? null
                    : referenceRow.getCell(cellDefinition.getReferenceCell().column);
            if (referenceCell != null && referenceCell.getCellStyle() != null)
                cell.setCellStyle(referenceCell.getCellStyle());
        }
    }

    // Add an optional comment      
    if (cellDefinition.hasComment()) {
        final CreationHelper factory = workbook.getCreationHelper();

        final Drawing drawing = sheet.createDrawingPatriarch();
        final ClientAnchor commentAnchor = factory.createClientAnchor();

        final int col1 = cellDefinition.comment.column == null ? cell.getColumnIndex() + 1
                : cellDefinition.comment.column;
        final int row1 = cellDefinition.comment.row == null ? cell.getRowIndex() : cellDefinition.comment.row;

        commentAnchor.setCol1(col1);
        commentAnchor.setRow1(row1);
        commentAnchor.setCol2(col1 + Math.max(1, cellDefinition.comment.width));
        commentAnchor.setRow2(row1 + Math.max(1, cellDefinition.comment.height));

        final Comment comment = drawing.createCellComment(commentAnchor);
        comment.setString(factory.createRichTextString(cellDefinition.comment.text));
        comment.setAuthor(cellDefinition.comment.author);
        comment.setVisible(cellDefinition.comment.visible);

        cell.setCellComment(comment);
    }
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.legacyExcel.exporter.ExportWorkbook.java

License:Open Source License

/**
 * Adds headers stored in <code>headers</code> to the current sheet. If required, a special width
 * for the corresponding column can be set by providing a value in <code>headersWidth</code> using
 * header as key.<br/>/*ww  w . ja va2 s. co  m*/
 * <b>IMPORTANT</b>: Headers are added in the order provided in <code>headers</code>.
 * 
 * @param headers
 *          headers to be added
 */
public void addHeaders(int sheetId, List<ExcelSheet.Header> headers) {
    Sheet sheet = getSheetById(sheetId);
    Drawing drawing = sheet.createDrawingPatriarch();
    CreationHelper factory = sheet.getWorkbook().getCreationHelper();
    Row row = sheet.createRow(this.getCurrentRowOfSheet(sheet, 3));
    int columnIndex = 0;
    for (ExcelSheet.Header header : headers) {
        int currColumnIndex = columnIndex;
        Cell cell = row.createCell(columnIndex);
        if (header.getDescription() != null) {
            ClientAnchor commentAnchor = factory.createClientAnchor();
            //Sizing the comment 1x3 cells
            commentAnchor.setCol1(cell.getColumnIndex());
            commentAnchor.setCol2(cell.getColumnIndex() + 1);
            commentAnchor.setRow1(row.getRowNum());
            commentAnchor.setRow2(row.getRowNum() + 3);

            Comment comment = drawing.createCellComment(commentAnchor);
            RichTextString str = factory.createRichTextString(header.getDescription());
            comment.setString(str);
            comment.setAuthor("");
            cell.setCellComment(comment);
        }

        setCellValue(cell, header.getLabel(), getHeaderTableStyle());
        Integer width = header.getWidth();
        if (width != null) {
            sheet.setColumnWidth(currColumnIndex, width.intValue());
        }
        columnIndex++;
    }

    LOGGER.debug("Added headers.");
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.legacyExcel.exporter.ExportWorkbook.java

License:Open Source License

/**
 * Sets given value and style of the given cell.
 * /*from  ww  w.  ja  v a  2s  . c o  m*/
 * @param cell
 *          the cell in question
 * @param value
 *          the value to be set
 * @param style
 *          if <code>null</code> the default style is taken
 */
private void setCellValue(Cell cell, String value, CellStyle style) {
    if (cell == null) {
        return;
    }
    CreationHelper createHelper = getWb().getCreationHelper();
    if (value != null) {
        cell.setCellValue(createHelper.createRichTextString(value));
    }
    CellStyle styleToSet = (style != null ? style : getDefaultStyle());
    cell.setCellStyle(styleToSet);
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.legacyExcel.exporter.ExportWorkbook.java

License:Open Source License

/**
 * Sets given value and style of the given cell and adds the given <code>link</code> as hyperlink
 * to the <code>cell</code>.
 * /*  w w w.java  2  s  . com*/
 * @param cell
 *          the cell in question
 * @param value
 *          the value to be set
 * @param link
 *          the link to be added
 * @param style
 *          if <code>null</code> the default style for hyperlinks is used
 */
private void setHyperlink(Cell cell, String value, Hyperlink link, CellStyle style) {
    if (cell == null) {
        return;
    }
    CreationHelper createHelper = getWb().getCreationHelper();
    if (value != null) {
        cell.setCellValue(createHelper.createRichTextString(value));
    }
    if (link != null) {
        cell.setHyperlink(link);
    }

    CellStyle styleToSet = (style != null ? style : getHyperlinkStyle());
    cell.setCellStyle(styleToSet);
}

From source file:de.ks.idnadrev.expimp.xls.XlsxExporter.java

License:Apache License

protected void setCellValue(CreationHelper creationHelper, Cell cell, Object value) {
    if (value instanceof Number) {
        double cellValue = ((Number) value).doubleValue();
        cell.setCellValue(cellValue);//w  w  w.j  a v  a 2  s. c o m
    } else if (Number.class.isAssignableFrom(Primitives.wrap(value.getClass()))) {
        double cellValue = (double) value;
        cell.setCellValue(cellValue);
    } else if (value instanceof String) {
        RichTextString richTextString = creationHelper.createRichTextString((String) value);
        cell.setCellValue(richTextString);
    } else if (value instanceof LocalDateTime) {
        long time = Timestamp.valueOf(((LocalDateTime) value)).getTime();
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        cell.setCellValue(cal);
    }
}

From source file:de.ks.idnadrev.expimp.xls.XlsxExporterCellValueTest.java

License:Apache License

@Test
public void testString() throws Exception {
    XlsxExporter exporter = new XlsxExporter(null);

    Cell cell = Mockito.mock(Cell.class);
    CreationHelper helper = Mockito.mock(CreationHelper.class);
    RichTextString richTextString = Mockito.mock(RichTextString.class);

    Mockito.when(helper.createRichTextString("Hello World")).thenReturn(richTextString);

    exporter.setCellValue(helper, cell, "Hello World");
    Mockito.verify(cell).setCellValue(richTextString);
    Mockito.verify(helper).createRichTextString("Hello World");
}

From source file:de.viaboxx.nlstools.formats.MBExcelPersistencer.java

License:Apache License

private Comment comment(HSSFCell cell, String text) {
    CreationHelper factory = wb.getCreationHelper();
    Drawing drawing = sheet.createDrawingPatriarch();
    // 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.getRow().getRowNum());
    anchor.setRow2(cell.getRow().getRowNum() + 3);
    Comment comment = drawing.createCellComment(anchor);
    RichTextString str = factory.createRichTextString(text);
    comment.setString(str);//from   w  w  w.  ja  va  2s  .co  m
    cell.setCellComment(comment);
    return comment;
}

From source file:Documentos.ClaseAlmacenGeneral.java

private static void createTituloCell(HSSFWorkbook wb, Row row, int column, short halign, short valign,
        String strContenido) {//w w  w  . j  av a2  s  .  c  o m
    CreationHelper ch = wb.getCreationHelper();
    Cell cell = row.createCell(column);
    cell.setCellValue(ch.createRichTextString(strContenido));

    HSSFFont cellFont = wb.createFont();
    cellFont.setFontHeightInPoints((short) 11);
    cellFont.setFontName(HSSFFont.FONT_ARIAL);
    cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(halign);
    cellStyle.setVerticalAlignment(valign);
    cellStyle.setFont(cellFont);
    cell.setCellStyle(cellStyle);

}

From source file:Documentos.ClaseAlmacenGeneral.java

private static void creandoCelda(Workbook wb, Row row, int column, String strContenido) {
    CreationHelper ch = wb.getCreationHelper();

    Cell cell = row.createCell(column);/*from  w  w  w  . ja  v  a2 s. co m*/
    cell.setCellValue(ch.createRichTextString(strContenido));

}

From source file:Documentos.ClaseAlmacenGeneral.java

private static void createCell(Workbook wb, Row row, int column, short halign, short valign,
        String strContenido, boolean booBorde, boolean booCabecera) {
    CreationHelper ch = wb.getCreationHelper();
    Cell cell = row.createCell(column);//  w w w.  java2  s.c o m
    cell.setCellValue(ch.createRichTextString(strContenido));
    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(halign);
    cellStyle.setVerticalAlignment(valign);

    if (booBorde) {
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_DOTTED);
        cellStyle.setBottomBorderColor((short) 8);
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_DOTTED);
        cellStyle.setLeftBorderColor((short) 8);
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_DOTTED);
        cellStyle.setRightBorderColor((short) 8);
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_DOTTED);
        cellStyle.setTopBorderColor((short) 8);
    }
    if (booCabecera) {
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
        cellStyle.setBottomBorderColor((short) 8);
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
        cellStyle.setLeftBorderColor((short) 8);
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
        cellStyle.setRightBorderColor((short) 8);
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
        cellStyle.setTopBorderColor((short) 8);

        cellStyle.setFillForegroundColor(HSSFColor.ROYAL_BLUE.index);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    }
    cell.setCellStyle(cellStyle);
}