Example usage for org.apache.poi.ss.usermodel Workbook createCellStyle

List of usage examples for org.apache.poi.ss.usermodel Workbook createCellStyle

Introduction

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

Prototype

CellStyle createCellStyle();

Source Link

Document

Create a new Cell style and add it to the workbook's style table

Usage

From source file:de.iteratec.iteraplan.businesslogic.exchange.elasticExcel.export.template.ExcelStylesCreator.java

License:Open Source License

private static CellStyle getDataHiddenStyle(Workbook workbook, CellStyle dataStyle) {
    CellStyle style = workbook.createCellStyle();
    style.cloneStyleFrom(dataStyle);/*from w  ww . j a v  a2  s  .c om*/
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);

    return style;
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.elasticExcel.export.template.ExcelStylesCreator.java

License:Open Source License

private static CellStyle getDateStyle(Workbook workbook, CellStyle dataStyle) {
    CellStyle cellStyle = workbook.createCellStyle();
    cellStyle.cloneStyleFrom(dataStyle);
    cellStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("m/d/yy"));

    return cellStyle;
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.elasticExcel.export.template.ExcelStylesCreator.java

License:Open Source License

private static CellStyle getHyperlinkStyle(Workbook workbook) {
    CellStyle hyperlinkStyle = workbook.createCellStyle();
    Font hyperlinkFont = workbook.createFont();
    hyperlinkFont.setUnderline(Font.U_SINGLE);
    hyperlinkFont.setColor(IndexedColors.BLUE.getIndex());
    hyperlinkStyle.setFont(hyperlinkFont);

    return hyperlinkStyle;
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.nettoExport.NettoExcelTransformer.java

License:Open Source License

/**{@inheritDoc}**/
@Override/*  w  w  w  . jav a 2s  . c om*/
public void transform(List<?> sourceList, OutputStream out, TypeOfBuildingBlock typeOfBuildingBlock) {

    ColumnStructure[] columns = tableStructure.getColumns();

    try {
        Workbook workbook = createWorkbook();

        Sheet sheet = workbook.createSheet();
        configSheetName(sheet, typeOfBuildingBlock);

        Map<IteraExcelStyle, CellStyle> createdStyles = ExcelStylesCreator.createStyles(workbook);
        CellStyle headerStyle = createdStyles.get(IteraExcelStyle.HEADER);
        CellStyle dataStyle = createdStyles.get(IteraExcelStyle.DATA);
        CellStyle dataDateStyle = createdStyles.get(IteraExcelStyle.DATA_DATE);

        // Create cell style for numbers
        CellStyle numCellStyle = workbook.createCellStyle();
        numCellStyle.cloneStyleFrom(dataStyle);
        short numFormatIndex = workbook.createDataFormat().getFormat("0.00");
        numCellStyle.setDataFormat(numFormatIndex);

        Row headerRow = sheet.createRow(0);

        int nextCol = 0;
        for (ColumnStructure columnStructure : columns) {
            Cell headerCell = headerRow.createCell(nextCol);
            headerCell.setCellValue(columnStructure.getColumnHeader());
            headerCell.setCellStyle(headerStyle);
            nextCol++;
        }

        int nextRow = 1;
        for (Object obj : sourceList) {
            if (obj instanceof BuildingBlock) {
                BuildingBlock bb = (BuildingBlock) obj;

                // skip virutal root element
                if (bb instanceof AbstractHierarchicalEntity<?>) {
                    AbstractHierarchicalEntity<?> hierarchicalEntity = (AbstractHierarchicalEntity<?>) bb;
                    if (hierarchicalEntity.isTopLevelElement()) {
                        continue;
                    }
                }

                Row row = sheet.createRow(nextRow);

                nextCol = 0;
                for (ColumnStructure columnStructure : columns) {
                    Cell cell = row.createCell(nextCol);

                    Object resolvedValue = columnStructure.resolveValue(bb);

                    if (resolvedValue instanceof Date) {
                        cell.setCellStyle(dataDateStyle);
                        cell.setCellValue((Date) resolvedValue);
                    } else if (resolvedValue instanceof Number) {
                        cell.setCellStyle(numCellStyle);
                        double doubleValue = ((Number) resolvedValue).doubleValue();
                        cell.setCellValue(doubleValue);
                    } else {
                        cell.setCellStyle(dataStyle);
                        cell.setCellValue(String.valueOf(resolvedValue));
                    }

                    ++nextCol;
                }

                ++nextRow;
            }
        }

        // auto format
        nextCol = 0;
        for (int col = 0; col < columns.length; col++) {
            sheet.autoSizeColumn(col);
            int columnCharWidth = sheet.getColumnWidth(col) / 256;
            if (columnCharWidth > MAX_COLUM_CHAR_WIDTH) {
                sheet.setColumnWidth(col, MAX_COLUM_CHAR_WIDTH * 256);
            }
        }

        workbook.write(out);
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:de.jpaw.bonaparte.poi.BaseExcelComposer.java

License:Apache License

public BaseExcelComposer(Workbook xls) {
    this.xls = xls;
    // create a few data formats
    xlsDataFormat = xls.createDataFormat();
    csLong = xls.createCellStyle();
    csLong.setDataFormat(xlsDataFormat.getFormat("#,###,###,###,###,###,###,###,###,##0"));
    csBigDecimal = new CellStyle[1 + MAX_DECIMALS];
    csDay = xls.createCellStyle();/*w ww  .  jav  a2s .  c o m*/
    csDay.setDataFormat(xlsDataFormat.getFormat("yyyy-mm-dd"));
    csTime = xls.createCellStyle();
    csTime.setDataFormat(xlsDataFormat.getFormat("hh:mm:ss"));
    csTimestamp = xls.createCellStyle();
    csTimestamp.setDataFormat(xlsDataFormat.getFormat("yyyy-mm-dd hh:mm:ss"));
}

From source file:de.thb.ue.backend.util.EvaluationExcelFileGenerator.java

License:Apache License

private void configureCellStyles(Workbook wb) {
    commonStyle = wb.createCellStyle();
    headerStyle = wb.createCellStyle();/*from   w w  w.  j  av a 2s.  c  o m*/
    questionStyle = wb.createCellStyle();
    positiveStyle = wb.createCellStyle();
    positiveHeaderStyle = wb.createCellStyle();
    negativeStyle = wb.createCellStyle();
    negativeHeaderStyle = wb.createCellStyle();
    improvementStyle = wb.createCellStyle();
    improvementHeaderStyle = wb.createCellStyle();
    furtherStyle = wb.createCellStyle();
    furtherHeaderStyle = wb.createCellStyle();

    Font headerFont = wb.createFont();
    headerFont.setBold(true);

    Font smallFont = wb.createFont();
    smallFont.setFontHeightInPoints((short) 8);

    commonStyle.setBorderBottom(CellStyle.BORDER_HAIR);
    commonStyle.setBorderTop(CellStyle.BORDER_HAIR);
    commonStyle.setBorderLeft(CellStyle.BORDER_HAIR);
    commonStyle.setBorderRight(CellStyle.BORDER_HAIR);
    commonStyle.setAlignment(CellStyle.ALIGN_CENTER);
    questionStyle.cloneStyleFrom(commonStyle);
    questionStyle.setFont(smallFont);
    questionStyle.setAlignment(CellStyle.ALIGN_LEFT);
    headerStyle.cloneStyleFrom(commonStyle);
    headerStyle.setFont(headerFont);

    positiveStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
    positiveStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    //        positiveStyle.setWrapText(true);

    negativeStyle.setFillForegroundColor(HSSFColor.ROSE.index);
    negativeStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    //        negativeStyle.setWrapText(true);

    improvementStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
    improvementStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    //        improvementStyle.setWrapText(true);

    furtherStyle.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
    furtherStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    //        furtherStyle.setWrapText(true);

    positiveHeaderStyle.cloneStyleFrom(positiveStyle);
    positiveHeaderStyle.setFont(headerFont);

    negativeHeaderStyle.cloneStyleFrom(negativeStyle);
    negativeHeaderStyle.setFont(headerFont);

    improvementHeaderStyle.cloneStyleFrom(improvementStyle);
    improvementHeaderStyle.setFont(headerFont);

    furtherHeaderStyle.cloneStyleFrom(furtherStyle);
    furtherHeaderStyle.setFont(headerFont);
}

From source file:de.thb.ue.backend.util.EvaluationExcelFileGenerator.java

License:Apache License

private void createTextualAnswers(@NonNull List<Vote> answers, @NonNull List<String> questionTexts, Sheet sheet,
        Workbook wb) {
    Row row;//from  www .ja  v  a2  s  . com
    Cell cell;
    row = sheet.createRow(sheet.getLastRowNum() + 2);
    cell = row.createCell(1);
    CellStyle helpStyle = wb.createCellStyle();
    helpStyle.cloneStyleFrom(headerStyle);
    helpStyle.setBorderBottom(CellStyle.BORDER_NONE);
    helpStyle.setBorderTop(CellStyle.BORDER_NONE);
    helpStyle.setBorderLeft(CellStyle.BORDER_NONE);
    helpStyle.setBorderRight(CellStyle.BORDER_NONE);
    cell.setCellValue("Kommentare");
    cell.setCellStyle(helpStyle);

    //TODO used to determine style for current line -> its stupid. Think of something better
    int styleCounter = 0;
    for (String textualQuestion : questionTexts) {
        row = sheet.createRow(sheet.getLastRowNum() + 3);
        cell = row.createCell(1);
        cell.setCellValue(textualQuestion);
        setTextQuestionStyle(cell, styleCounter, true);

        //colorize horizontal neighbour cells of headline
        for (int i = 2; i < 5; i++) {
            cell = row.createCell(i);
            setTextQuestionStyle(cell, styleCounter, false);
        }

        int rowNum = sheet.getLastRowNum();
        int counter = 1;

        for (String comment : aggregateTextAnswers(answers, textualQuestion)) {
            row = sheet.createRow(rowNum + 1);
            cell = row.createCell(1, Cell.CELL_TYPE_STRING);

            //              introduces line breaks in long comments
            ArrayList<String> commentChunks = splitComment(comment);
            StringBuilder formattedComment = new StringBuilder();
            formattedComment.append(Integer.toString(counter));
            formattedComment.append(": ");

            int chunkCounter = 0;
            for (String chunk : commentChunks) {
                formattedComment.append(chunk);

                if ((chunkCounter + 1) < commentChunks.size()) {
                    formattedComment.append(System.lineSeparator());
                }
                chunkCounter++;
            }
            cell.setCellValue(formattedComment.toString());

            CellStyle style = setTextQuestionStyle(cell, styleCounter, false);

            // increase height of row based on font size, number of lines and line spacing
            // the origin of 140 % -> http://superuser.com/questions/337181/how-many-pts-is-1-5-line-spacing-in-microsoft-word-2007
            float pointsPerLine = (wb.getFontAt(style.getFontIndex()).getFontHeightInPoints() * 140) / 100;
            row.setHeightInPoints(pointsPerLine * commentChunks.size());

            //colorize horizontal neighbour cells of comment
            for (int i = 2; i < 17; i++) {
                cell = row.createCell(i);
                setTextQuestionStyle(cell, styleCounter, false);
            }
            sheet.addMergedRegion(new CellRangeAddress(row.getRowNum(), row.getRowNum(), 1, 17));

            rowNum++;
            counter++;
        }
        styleCounter++;
    }
}

From source file:de.thb.ue.backend.util.EvaluationExcelFileGenerator.java

License:Apache License

private CellStyle infoPanelStyleConfigurator(InfoPanelBorderStyles style, Workbook wb) throws IOException {
    CellStyle infoPanelStyle = wb.createCellStyle();
    infoPanelStyle.setBorderLeft(CellStyle.BORDER_NONE);
    infoPanelStyle.setBorderRight(CellStyle.BORDER_NONE);
    infoPanelStyle.setBorderBottom(CellStyle.BORDER_NONE);
    infoPanelStyle.setBorderTop(CellStyle.BORDER_NONE);
    switch (style) {
    case topLeftCorner:
        infoPanelStyle.setBorderTop(CellStyle.BORDER_HAIR);
        infoPanelStyle.setBorderLeft(CellStyle.BORDER_HAIR);
        break;//  w ww . ja v a2  s .  co  m
    case topRightCorner:
        infoPanelStyle.setBorderTop(CellStyle.BORDER_HAIR);
        infoPanelStyle.setBorderRight(CellStyle.BORDER_HAIR);
        break;
    case bottomLeftCorner:
        infoPanelStyle.setBorderLeft(CellStyle.BORDER_HAIR);
        infoPanelStyle.setBorderBottom(CellStyle.BORDER_HAIR);
        break;
    case bottomRightCorner:
        infoPanelStyle.setBorderRight(CellStyle.BORDER_HAIR);
        infoPanelStyle.setBorderBottom(CellStyle.BORDER_HAIR);
        break;
    case top:
        infoPanelStyle.setBorderTop(CellStyle.BORDER_HAIR);
        break;
    case bottom:
        infoPanelStyle.setBorderBottom(CellStyle.BORDER_HAIR);
        break;
    case right:
        infoPanelStyle.setBorderRight(CellStyle.BORDER_HAIR);
        break;
    case left:
        infoPanelStyle.setBorderLeft(CellStyle.BORDER_HAIR);
        break;
    default:
        break;
    }

    return infoPanelStyle;
}

From source file:de.thb.ue.backend.util.EvaluationExcelFileGenerator.java

License:Apache License

private Cell colorizeCell(Cell cell, Workbook wb, float grade) {
    CellStyle style = wb.createCellStyle();
    style.cloneStyleFrom(commonStyle);/*w w  w.j a v  a2s.c o m*/

    if (grade >= 1.0 && grade < 2.0f) {
        style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cell.setCellStyle(style);
    } else if ((grade >= 3.0 && grade <= 4.0) || (grade <= -3.0 && grade >= -4.0)) {
        style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cell.setCellStyle(style);
    } else if (grade > 4.0 || grade < -4.0) {
        style.setFillForegroundColor(HSSFColor.MAROON.index);
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cell.setCellStyle(style);
    } else {
        cell.setCellStyle(style);
    }
    return cell;
}

From source file:de.unioninvestment.eai.portal.portlet.crud.export.streaming.ExcelExporter.java

License:Apache License

/**
 * Returns the default header style. Obtained from:
 * http://svn.apache.org/repos/asf/poi/*from  ww  w  . ja  v a  2 s  . c om*/
 * /trunk/src/examples/src/org/apache/poi/ss/examples/TimesheetDemo.java
 * 
 * @param wb
 *            the wb
 * 
 * @return the cell style
 */
private CellStyle defaultHeaderCellStyle(final Workbook wb) {
    CellStyle style;
    final Font headerFont = wb.createFont();
    if (!Strings.isNullOrEmpty(fontName)) {
        headerFont.setFontName(fontName);
    }
    headerFont.setFontHeightInPoints((short) 11);
    headerFont.setColor(IndexedColors.WHITE.getIndex());
    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFont(headerFont);
    style.setWrapText(true);
    return style;
}