Example usage for org.apache.poi.ss.usermodel CellStyle setBorderTop

List of usage examples for org.apache.poi.ss.usermodel CellStyle setBorderTop

Introduction

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

Prototype

void setBorderTop(BorderStyle border);

Source Link

Document

set the type of border to use for the top border of the cell

Usage

From source file:module.siadap.domain.SiadapRootModule.java

License:Open Source License

private void populateSheet(HSSFSheet sheetToWriteTo, boolean considerQuotas, UnitSiadapWrapper unitToSearchIn,
        HSSFWorkbook wb, boolean shouldIncludeEndOfRole, boolean includeHarmonizationResponsibles,
        boolean shouldIncludeUniverse) {

    CreationHelper creationHelper = wb.getCreationHelper();

    // make the sheet fit the page
    PrintSetup ps = sheetToWriteTo.getPrintSetup();

    sheetToWriteTo.setAutobreaks(true);//w ww. j a  v  a 2 s. co m

    ps.setFitHeight((short) 1);
    ps.setFitWidth((short) 1);

    /* ** styles ** */

    // CostCenter style
    HSSFFont costCenterFont = wb.createFont();
    costCenterFont.setColor(HSSFColor.DARK_BLUE.index);
    costCenterFont.setFontHeightInPoints((short) 12);
    costCenterFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    CellStyle costCenterStyle = wb.createCellStyle();
    costCenterStyle.setFont(costCenterFont);

    // make the Unit header style
    CellStyle unitHeaderStyle = wb.createCellStyle();
    unitHeaderStyle.setBorderBottom(CellStyle.BORDER_THIN);
    unitHeaderStyle.setBorderTop(CellStyle.BORDER_THIN);
    unitHeaderStyle.setBorderLeft(CellStyle.BORDER_THIN);
    unitHeaderStyle.setBorderRight(CellStyle.BORDER_THIN);
    unitHeaderStyle.setAlignment(CellStyle.ALIGN_CENTER);
    unitHeaderStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    HSSFFont headerFont = wb.createFont();
    headerFont.setFontHeightInPoints((short) 12);
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    headerFont.setItalic(true);
    unitHeaderStyle.setFont(headerFont);

    // make the default name style
    CellStyle defaultTextNameStyle = wb.createCellStyle();
    defaultTextNameStyle.setBorderLeft(CellStyle.BORDER_THIN);
    defaultTextNameStyle.setBorderRight(CellStyle.BORDER_THIN);
    defaultTextNameStyle.setBorderBottom(CellStyle.BORDER_NONE);
    defaultTextNameStyle.setBorderTop(CellStyle.BORDER_NONE);
    defaultTextNameStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    HSSFFont defaultFont = wb.createFont();
    defaultFont.setFontHeightInPoints((short) 11);
    defaultTextNameStyle.setFont(defaultFont);

    // make the last line name style
    CellStyle defaultTextNameLastStyle = wb.createCellStyle();
    defaultTextNameLastStyle.setBorderLeft(CellStyle.BORDER_THIN);
    defaultTextNameLastStyle.setBorderRight(CellStyle.BORDER_THIN);
    defaultTextNameLastStyle.setBorderBottom(CellStyle.BORDER_THIN);
    defaultTextNameLastStyle.setBorderTop(CellStyle.BORDER_NONE);
    defaultTextNameLastStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    defaultTextNameLastStyle.setFont(defaultFont);

    // make the default IST-ID last line style
    CellStyle defaultTextIstIdLastStyle = wb.createCellStyle();
    defaultTextIstIdLastStyle.setBorderLeft(CellStyle.BORDER_THIN);
    defaultTextIstIdLastStyle.setBorderBottom(CellStyle.BORDER_THIN);
    defaultTextIstIdLastStyle.setBorderTop(CellStyle.BORDER_NONE);
    defaultTextIstIdLastStyle.setBorderRight(CellStyle.BORDER_THIN);
    defaultTextIstIdLastStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    defaultTextIstIdLastStyle.setAlignment(CellStyle.ALIGN_CENTER);
    defaultTextIstIdLastStyle.setFont(defaultFont);

    // make the default IST-ID style
    CellStyle defaultTextIstIdStyle = wb.createCellStyle();
    defaultTextIstIdStyle.setBorderLeft(CellStyle.BORDER_THIN);
    defaultTextIstIdStyle.setBorderBottom(CellStyle.BORDER_NONE);
    defaultTextIstIdStyle.setBorderTop(CellStyle.BORDER_NONE);
    defaultTextIstIdStyle.setBorderRight(CellStyle.BORDER_THIN);
    defaultTextIstIdStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    defaultTextIstIdStyle.setAlignment(CellStyle.ALIGN_CENTER);
    defaultTextIstIdStyle.setFont(defaultFont);

    // header style

    // CellStyle headerStyle = wb.createCellStyle();
    // HSSFFont headerFont = wb.createFont();
    // headerFont.setFontName(HSSFFont.FONT_ARIAL);
    // headerFont.setFontHeightInPoints((short) 10);
    // headerStyle.setFont(headerFont);
    //

    // first line style
    CellStyle firstLineStyle = wb.createCellStyle();
    HSSFFont firstLineFont = wb.createFont();
    firstLineFont.setColor(HSSFColor.DARK_BLUE.index);
    firstLineFont.setFontHeightInPoints((short) 14);
    firstLineFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    firstLineStyle.setFont(firstLineFont);
    firstLineStyle.setAlignment(CellStyle.ALIGN_CENTER);
    firstLineStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

    // second line style
    CellStyle secondLineStyle = wb.createCellStyle();
    HSSFFont secondLineFont = wb.createFont();
    secondLineFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    secondLineFont.setFontHeightInPoints((short) 14);
    secondLineStyle.setFont(secondLineFont);
    secondLineStyle.setAlignment(CellStyle.ALIGN_CENTER);
    secondLineStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

    // the style for Unit Harmonization responsibles - title
    CellStyle unitHarmonizationTitleStyle = wb.createCellStyle();
    // the BLUE title font - is equal to 'firstLineFont'
    unitHarmonizationTitleStyle.setFont(firstLineFont);
    // now we just have to shade it
    unitHarmonizationTitleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    unitHarmonizationTitleStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    unitHarmonizationTitleStyle.setAlignment(CellStyle.ALIGN_CENTER);

    // the style for Unit Harmonization responsibles - normal

    // let's create the BLUE Arial 14 font for the responsibles of
    // harmonization
    HSSFFont harmonizationResponsibleFont = wb.createFont();
    harmonizationResponsibleFont.setColor(HSSFColor.DARK_BLUE.index);
    harmonizationResponsibleFont.setFontHeightInPoints((short) 14);

    CellStyle unitHarmonizationResponsibleStyle = wb.createCellStyle();
    unitHarmonizationResponsibleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    unitHarmonizationResponsibleStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    unitHarmonizationResponsibleStyle.setFont(harmonizationResponsibleFont);
    unitHarmonizationResponsibleStyle.setAlignment(CellStyle.ALIGN_CENTER);

    /* ** END of styles ** */

    /* ** Immutable IST header ** */

    HSSFHeader header = sheetToWriteTo.getHeader();
    header.setCenter(HSSFHeader.font("Arial", "Normal") + HSSFHeader.fontSize((short) 10));
    header.setCenter("Instituto Superior Tcnico");

    int rowIndex = START_ROW_INDEX;
    int cellIndex = START_CELL_INDEX;

    int firstLineIndex = rowIndex++;
    int secondLineIndex = rowIndex++;
    /* ** Write the first lines with the dates ** */
    HSSFRow row = sheetToWriteTo.createRow(firstLineIndex);
    HSSFCell cell = row.createCell(cellIndex);
    cell.setCellValue("SIADAP - LISTA DE AVALIADORES " + unitToSearchIn.getYear());
    cell.setCellStyle(firstLineStyle);
    sheetToWriteTo
            .addMergedRegion(new CellRangeAddress(firstLineIndex, firstLineIndex, cellIndex, cellIndex + 3));

    // second line
    if (!considerQuotas) {
        cellIndex = START_CELL_INDEX;
        row = sheetToWriteTo.createRow(secondLineIndex);
        cell = row.createCell(cellIndex);
        cell.setCellValue("PESSOAL CONTRATADO PELA ADIST");
        cell.setCellStyle(secondLineStyle);

    }

    /* ** write the IST logo ** */

    int pictureIdx = wb.addPicture(istLogoBytes, Workbook.PICTURE_TYPE_PNG);
    HSSFPatriarch drawingPatriarch = sheetToWriteTo.createDrawingPatriarch();
    ClientAnchor clientAnchor = creationHelper.createClientAnchor();
    clientAnchor.setCol1(cellIndex);
    clientAnchor.setRow1(rowIndex);
    HSSFPicture picture = drawingPatriarch.createPicture(clientAnchor, pictureIdx);

    // let's give the next item some space
    rowIndex += 6;

    /* ** Dynamic IST footer ** */

    HSSFFooter footer = sheetToWriteTo.getFooter();
    footer.setLeft("Lista gerada em: " + HSSFFooter.date() + " " + HSSFFooter.time());
    footer.setCenter(HSSFFooter.page());
    footer.setRight("SIADAP - Lista de avaliadores " + unitToSearchIn.getYear());

    for (UnitSiadapWrapper eachUnit : unitToSearchIn
            .getAllChildUnits(unitToSearchIn.getConfiguration().getUnitRelations())) {

        Collection<Person> harmonizationResponsibles = eachUnit.getHarmonizationResponsibles();
        if (includeHarmonizationResponsibles && !harmonizationResponsibles.isEmpty()) {
            // let's add the section stating the responsible for
            // Harmonization
            cellIndex = START_CELL_INDEX;
            row = sheetToWriteTo.createRow(++rowIndex);
            // let's merge the row
            sheetToWriteTo.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, cellIndex, cellIndex + 3));
            cell = row.createCell(cellIndex);
            cell.setCellStyle(unitHarmonizationTitleStyle);
            cell.setCellValue("UNIDADE DE HARMONIZAO: " + eachUnit.getName());
            // a 'blank' styled line
            row = sheetToWriteTo.createRow(++rowIndex);
            // merge it
            sheetToWriteTo.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, cellIndex, cellIndex + 3));
            row.createCell(cellIndex).setCellStyle(unitHarmonizationResponsibleStyle);
            // each responsible has one of the following lines
            for (Person harmonizationResponsible : harmonizationResponsibles) {
                cellIndex = START_CELL_INDEX;
                row = sheetToWriteTo.createRow(++rowIndex);
                // merge it
                sheetToWriteTo
                        .addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, cellIndex, cellIndex + 3));
                cell = row.createCell(cellIndex);
                cell.setCellStyle(unitHarmonizationResponsibleStyle);
                cell.setCellValue("RESPONS?VEL PELA HARMONIZAO: " + harmonizationResponsible.getName());
            }
            // and let's add an extra 'blank' styled line
            row = sheetToWriteTo.createRow(++rowIndex);
            sheetToWriteTo.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, cellIndex, cellIndex + 3));
            row.createCell(cellIndex).setCellStyle(unitHarmonizationResponsibleStyle);
            // and a regular one! (skip one in the index)
            ++rowIndex;

        }
        if (eachUnit.getQuotaAwareTotalPeopleWorkingInUnit(false, considerQuotas) > 0) {

            row = sheetToWriteTo.createRow(++rowIndex);
            cellIndex = START_CELL_INDEX;
            // write the unit name and cost center
            String unitNameWithCC = eachUnit.getUnit().getPartyName().getContent();
            if (eachUnit.getUnit().getPartyTypesSet().contains(PartyType.readBy("CostCenter"))) {
                unitNameWithCC += " - " + eachUnit.getUnit().getAcronym();
            }
            cell = row.createCell(cellIndex++);
            sheetToWriteTo.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, --cellIndex, ++cellIndex));
            cell.setCellValue(unitNameWithCC);
            cell.setCellStyle(costCenterStyle);

            /* **** write the Unit header ***** */

            // restart the cell's index
            cellIndex = START_CELL_INDEX;

            // IST id avaliado
            int firstLineAfterUnitNameIndex = ++rowIndex;
            int secondLineAfterUnitNameIndex = ++rowIndex;

            row = sheetToWriteTo.createRow(firstLineAfterUnitNameIndex);
            cell = row.createCell(cellIndex);
            cell.setCellStyle(unitHeaderStyle);
            cell.setCellValue("IST id.");

            row = sheetToWriteTo.createRow(secondLineAfterUnitNameIndex);
            cell = row.createCell(cellIndex);
            cell.setCellStyle(unitHeaderStyle);

            // merge the IST id
            sheetToWriteTo.addMergedRegion(new CellRangeAddress(firstLineAfterUnitNameIndex,
                    secondLineAfterUnitNameIndex, cellIndex, cellIndex));

            // Nome avaliado
            row = sheetToWriteTo.getRow(firstLineAfterUnitNameIndex);
            cell = row.createCell(++cellIndex);
            cell.setCellStyle(unitHeaderStyle);
            cell.setCellValue("Nome");

            row = sheetToWriteTo.getRow(secondLineAfterUnitNameIndex);
            cell = row.createCell(cellIndex);
            cell.setCellStyle(unitHeaderStyle);

            // merge
            sheetToWriteTo.addMergedRegion(new CellRangeAddress(firstLineAfterUnitNameIndex,
                    secondLineAfterUnitNameIndex, cellIndex, cellIndex));

            if (shouldIncludeUniverse) {

                // SIADAP do avaliado
                row = sheetToWriteTo.getRow(firstLineAfterUnitNameIndex);
                cell = row.createCell(++cellIndex);
                cell.setCellStyle(unitHeaderStyle);
                cell.setCellValue("SIADAP");

                row = sheetToWriteTo.getRow(secondLineAfterUnitNameIndex);
                cell = row.createCell(cellIndex);
                cell.setCellStyle(unitHeaderStyle);

                // merge
                sheetToWriteTo.addMergedRegion(new CellRangeAddress(firstLineAfterUnitNameIndex,
                        secondLineAfterUnitNameIndex, cellIndex, cellIndex));
            }

            // Ist id do avaliador
            row = sheetToWriteTo.getRow(firstLineAfterUnitNameIndex);
            cell = row.createCell(++cellIndex);
            cell.setCellStyle(unitHeaderStyle);
            cell.setCellValue("IST id.");

            row = sheetToWriteTo.getRow(secondLineAfterUnitNameIndex);
            cell = row.createCell(cellIndex);
            cell.setCellStyle(unitHeaderStyle);

            // merge
            sheetToWriteTo.addMergedRegion(new CellRangeAddress(firstLineAfterUnitNameIndex,
                    secondLineAfterUnitNameIndex, cellIndex, cellIndex));

            // avaliador
            row = sheetToWriteTo.getRow(firstLineAfterUnitNameIndex);
            cell = row.createCell(++cellIndex);
            cell.setCellStyle(unitHeaderStyle);
            cell.setCellValue("Avaliador");

            row = sheetToWriteTo.getRow(secondLineAfterUnitNameIndex);
            cell = row.createCell(cellIndex);
            cell.setCellStyle(unitHeaderStyle);

            // merge
            sheetToWriteTo.addMergedRegion(new CellRangeAddress(firstLineAfterUnitNameIndex,
                    secondLineAfterUnitNameIndex, cellIndex, cellIndex));

            List<PersonSiadapWrapper> listToUse = (considerQuotas) ? eachUnit.getUnitEmployeesWithQuotas(false)
                    : eachUnit.getUnitEmployeesWithoutQuotas(true);

            // now let's take care of exporting the persons
            for (PersonSiadapWrapper personWrapper : listToUse) {
                row = sheetToWriteTo.createRow(++rowIndex);
                // restart the cell's index
                cellIndex = START_CELL_INDEX;
                String istIdEvaluated = personWrapper.getPerson().getUser().getUsername();
                cell = row.createCell(cellIndex++);
                cell.setCellValue(istIdEvaluated);
                cell.setCellStyle(defaultTextIstIdStyle);

                String nameEvaluatedPerson = personWrapper.getPerson().getName();
                cell = row.createCell(cellIndex++);
                cell.setCellValue(nameEvaluatedPerson);
                cell.setCellStyle(defaultTextNameStyle);

                if (shouldIncludeUniverse) {

                    Siadap siadap = personWrapper.getSiadap();
                    String siadapUniverseToBeWritten = (siadap == null
                            || siadap.getDefaultSiadapUniverse() == null) ? "No definido"
                                    : siadap.getDefaultSiadapUniverse().getLocalizedName();
                    cell = row.createCell(cellIndex++);
                    cell.setCellValue(siadapUniverseToBeWritten);
                    cell.setCellStyle(defaultTextNameStyle);
                }

                PersonSiadapWrapper evaluatorWrapper = personWrapper.getEvaluator();
                String istIdEvaluator = evaluatorWrapper == null ? "-"
                        : evaluatorWrapper.getPerson().getUser().getUsername();
                cell = row.createCell(cellIndex++);
                cell.setCellValue(istIdEvaluator);
                cell.setCellStyle(defaultTextIstIdStyle);

                String nameEvaluatorWrapper = evaluatorWrapper == null ? "-" : evaluatorWrapper.getName();
                cell = row.createCell(cellIndex++);
                cell.setCellValue(nameEvaluatorWrapper);
                cell.setCellStyle(defaultTextNameStyle);

            }
            // let's make a bottom border on the last four cells
            for (int i = START_CELL_INDEX; i < START_CELL_INDEX + 4; i++) {
                cell = row.getCell(i);
                // let's diferentaitate between the IST-id and the name
                if (i == START_CELL_INDEX || i == START_CELL_INDEX + 2) // first
                // cell,
                // IST-ID
                // then.
                // or
                // third
                // cell
                // the
                // other
                // IST-ID
                {
                    cell.setCellStyle(defaultTextIstIdLastStyle);
                } else {
                    cell.setCellStyle(defaultTextNameLastStyle);
                }

            }
            row = sheetToWriteTo.createRow(++rowIndex);
            row = sheetToWriteTo.createRow(++rowIndex);

        }

    }

    sheetToWriteTo.autoSizeColumn(START_CELL_INDEX);
    sheetToWriteTo.autoSizeColumn(START_CELL_INDEX + 1);
    sheetToWriteTo.autoSizeColumn(START_CELL_INDEX + 2);
    sheetToWriteTo.autoSizeColumn(START_CELL_INDEX + 3);
    sheetToWriteTo.autoSizeColumn(START_CELL_INDEX + 4);

    // now let's resize the logo
    picture.resize();
}

From source file:nc.noumea.mairie.appock.services.impl.ExportExcelServiceImpl.java

License:Open Source License

private CellStyle createCellWithBorderAndColor(Workbook wb, BorderStyle borderStyle,
        IndexedColors indexedColors, boolean bold) {
    CellStyle style = wb.createCellStyle();
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setAlignment(HorizontalAlignment.CENTER);
    if (borderStyle != null) {
        style.setBorderBottom(borderStyle);
        style.setBorderTop(borderStyle);
        style.setBorderLeft(borderStyle);
        style.setBorderRight(borderStyle);
    }//from  w  w w .  j ava  2 s  .  c  o m

    if (indexedColors != null) {
        style.setFillForegroundColor(indexedColors.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    }

    if (bold) {
        Font font = wb.createFont();
        font.setBold(true);
        style.setFont(font);
    }

    return style;
}

From source file:net.algem.planning.export.PlanningExportService.java

License:Open Source License

/**
 *
 * @param wb workbook//  w ww.j a v  a 2  s  . c om
 * @return a map, each key-value composed of a style name and a cell style
 */
private Map<String, CellStyle> createStyles(HSSFWorkbook wb) {
    Map<String, CellStyle> styles = new HashMap<>();

    HSSFFont nf = wb.createFont();
    nf.setFontName("monospace");

    HSSFFont bf = wb.createFont();
    bf.setFontName("monospace");
    bf.setBold(true);

    HSSFFont sf = wb.createFont();
    sf.setFontHeightInPoints((short) 8);
    sf.setFontName("monospace");

    CellStyle style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setWrapText(true);
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    styles.put("header", style);

    //LEFT HEADER
    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    style.setWrapText(true);
    style.setFont(bf);
    style.setBorderLeft(CellStyle.BORDER_THIN);
    style.setBorderRight(CellStyle.BORDER_THIN);
    style.setBorderTop(CellStyle.BORDER_THIN);
    style.setBorderBottom(CellStyle.BORDER_DOTTED);
    styles.put("hour", style);

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    style.setWrapText(false);
    style.setFont(sf);
    style.setBorderLeft(CellStyle.BORDER_THIN);
    style.setBorderRight(CellStyle.BORDER_THIN);
    style.setBorderTop(CellStyle.BORDER_DOTTED);
    style.setBorderBottom(CellStyle.BORDER_THIN);
    styles.put("hour-quarter", style);

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    style.setWrapText(false);
    style.setFont(sf);
    style.setBorderLeft(CellStyle.BORDER_THIN);
    style.setBorderRight(CellStyle.BORDER_THIN);
    style.setBorderTop(CellStyle.BORDER_DASHED);
    style.setBorderBottom(CellStyle.BORDER_THIN);
    styles.put("hour-half", style);

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    style.setWrapText(true);
    style.setFont(nf);
    style.setBorderLeft(CellStyle.BORDER_THIN);
    style.setBorderRight(CellStyle.BORDER_THIN);
    style.setBorderTop(CellStyle.BORDER_THIN);
    style.setBorderBottom(CellStyle.BORDER_THIN);
    styles.put("hour-last", style);

    return styles;
}

From source file:net.algem.planning.export.PlanningExportService.java

License:Open Source License

/**
 *
 * @param wb workbook/* w w w  .j a va 2 s  .  com*/
 * @param schedule current schedule
 * @param cache styles cache
 * @return a cell style
 */
private CellStyle getCourseStyle(HSSFWorkbook wb, ScheduleObject schedule,
        Map<java.awt.Color, CellStyle> cache) {
    java.awt.Color color = colorizer.getScheduleColor(schedule);
    CellStyle cachedStyle = cache.get(color);
    if (cachedStyle != null) {
        return cachedStyle;
    } else {
        CellStyle style = wb.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(CellStyle.VERTICAL_TOP);
        style.setWrapText(true);
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setBorderTop(CellStyle.BORDER_THIN);
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        //the cell is painted with a pattern that consists of foreground and background pixels.
        //If you use SOLID_FOREGROUND, just the foreground pixel are visible.
        //This color is different from the color used to render text, which is set with the font
        style.setFillForegroundColor(getColorIndex(wb, color));

        // Unused because font is applied as richTextString property
        /*java.awt.Color textColor = colorizer.getTextColor(schedule);
        HSSFFont font = wb.createFont();
        font.setColor(getColorIndex(wb, textColor));
        style.setFont(font);
        cache.put(color, style);*/
        return style;
    }
}

From source file:net.ceos.project.poi.annotated.core.CellStyleHandler.java

License:Apache License

/**
 * Apply the border to the cell style./*from w w  w  .j a v  a 2 s.  com*/
 * 
 * @param cs
 *            the {@link CellStyle} to use
 * @param bL
 *            the cell border left
 * @param bR
 *            the cell border right
 * @param bT
 *            the cell border top
 * @param bB
 *            the cell border bottom
 */
protected static void applyBorder(final CellStyle cs, final short bL, final short bR, final short bT,
        final short bB) {
    cs.setBorderLeft(bL);
    cs.setBorderRight(bR);
    cs.setBorderTop(bT);
    cs.setBorderBottom(bB);
}

From source file:net.ceos.project.poi.annotated.core.CellStyleHandler.java

License:Apache License

/**
 * Clone a cell style passed as parameter.
 * /*from  w w w  .  java 2  s.  co  m*/
 * @param wb
 *            the {@link Workbook} in use
 * @param csBase
 *            the {@link CellStyle} base
 * @return the new cell style
 */
private static CellStyle cloneCellStyle(final Workbook wb, final CellStyle csBase) {
    CellStyle cs = cellStyleFactory.newInstance(wb);
    cs.setAlignment(csBase.getAlignment());
    cs.setVerticalAlignment(csBase.getVerticalAlignment());
    cs.setBorderTop(csBase.getBorderTop());
    cs.setBorderBottom(csBase.getBorderBottom());
    cs.setBorderLeft(csBase.getBorderLeft());
    cs.setBorderRight(csBase.getBorderRight());
    cs.setFillForegroundColor(csBase.getFillForegroundColor());
    cs.setFillPattern(csBase.getFillPattern());
    cs.setWrapText(csBase.getWrapText());

    cs.setFont(wb.getFontAt(csBase.getFontIndex()));
    return cs;
}

From source file:nl.detoren.ijc.io.OutputExcel.java

License:Open Source License

private void borderFull(Cell cell) {
    if (cell != null) {
        Workbook wb = cell.getRow().getSheet().getWorkbook();
        CellStyle style = wb.createCellStyle();
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        cell.setCellStyle(style);//from   ww  w  .j av a2s.c  om
    }
}

From source file:nz.ac.auckland.abi.formatting.poi.ModelJSONToExcel.java

License:LGPL

/**
 * Create a library of cell styles//from  www.j a  v  a  2s .  co  m
 */
private static Map<String, CellStyle> createStyles(Workbook wb) {
    Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
    CellStyle style;
    Font titleFont = wb.createFont();
    titleFont.setFontHeightInPoints((short) 18);
    titleFont.setBoldweight(Font.BOLDWEIGHT_NORMAL);
    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFont(titleFont);
    styles.put("title", style);

    Font monthFont = wb.createFont();
    monthFont.setFontHeightInPoints((short) 11);
    monthFont.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(monthFont);
    style.setWrapText(true);
    styles.put("header", style);

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setWrapText(true);
    style.setBorderRight(CellStyle.BORDER_THIN);
    style.setRightBorderColor(IndexedColors.BLACK.getIndex());
    style.setBorderLeft(CellStyle.BORDER_THIN);
    style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
    style.setBorderTop(CellStyle.BORDER_THIN);
    style.setTopBorderColor(IndexedColors.BLACK.getIndex());
    style.setBorderBottom(CellStyle.BORDER_THIN);
    style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
    styles.put("cell", style);

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
    styles.put("formula", style);

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
    styles.put("formula_2", style);

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(IndexedColors.RED.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
    styles.put("MAX", style);

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
    styles.put("MIN", style);

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
    styles.put("AVERAGE", style);

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
    styles.put("STDEV", style);

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
    styles.put("AVGSERIES", style);

    return styles;
}

From source file:org.alanwilliamson.openbd.plugin.spreadsheet.SpreadSheetFormatOptions.java

License:Open Source License

public static CellStyle createCellStyle(Workbook workbook, cfStructData _struct) throws Exception {
    CellStyle style = workbook.createCellStyle();

    if (_struct.containsKey("alignment")) {
        String v = _struct.getData("alignment").getString();
        Short s = lookup_alignment.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'alignment' (" + v + ")");
        } else/* w w w .  j  ava 2s .  c o m*/
            style.setAlignment(s);
    }

    if (_struct.containsKey("bottomborder")) {
        String v = _struct.getData("bottomborder").getString();
        Short s = lookup_border.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'bottomborder' (" + v + ")");
        } else
            style.setBorderBottom(s);
    }

    if (_struct.containsKey("topborder")) {
        String v = _struct.getData("topborder").getString();
        Short s = lookup_border.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'topborder' (" + v + ")");
        } else
            style.setBorderTop(s);
    }

    if (_struct.containsKey("leftborder")) {
        String v = _struct.getData("leftborder").getString();
        Short s = lookup_border.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'leftborder' (" + v + ")");
        } else
            style.setBorderLeft(s);
    }

    if (_struct.containsKey("rightborder")) {
        String v = _struct.getData("rightborder").getString();
        Short s = lookup_border.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'rightborder' (" + v + ")");
        } else
            style.setBorderRight(s);
    }

    if (_struct.containsKey("bottombordercolor")) {
        String v = _struct.getData("bottombordercolor").getString();
        Short s = lookup_colors.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'bottombordercolor' (" + v + ")");
        } else
            style.setBottomBorderColor(s);
    }

    if (_struct.containsKey("topbordercolor")) {
        String v = _struct.getData("topbordercolor").getString();
        Short s = lookup_colors.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'topbordercolor' (" + v + ")");
        } else
            style.setTopBorderColor(s);
    }

    if (_struct.containsKey("leftbordercolor")) {
        String v = _struct.getData("leftbordercolor").getString();
        Short s = lookup_colors.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'leftbordercolor' (" + v + ")");
        } else
            style.setLeftBorderColor(s);
    }

    if (_struct.containsKey("rightbordercolor")) {
        String v = _struct.getData("rightbordercolor").getString();
        Short s = lookup_colors.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'rightbordercolor' (" + v + ")");
        } else
            style.setRightBorderColor(s);
    }

    if (_struct.containsKey("fillpattern")) {
        String v = _struct.getData("fillpattern").getString();
        Short s = lookup_fillpatten.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'fillpattern' (" + v + ")");
        } else
            style.setFillPattern(s);
    }

    if (_struct.containsKey("fgcolor")) {
        String v = _struct.getData("fgcolor").getString();
        Short s = lookup_colors.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'fgcolor' (" + v + ")");
        } else
            style.setFillForegroundColor(s);
    }

    if (_struct.containsKey("bgcolor")) {
        String v = _struct.getData("bgcolor").getString();
        Short s = lookup_colors.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'bgcolor' (" + v + ")");
        } else
            style.setFillBackgroundColor(s);
    }

    if (_struct.containsKey("textwrap")) {
        Boolean b = _struct.getData("textwrap").getBoolean();
        style.setWrapText(b);
    }

    if (_struct.containsKey("hidden")) {
        Boolean b = _struct.getData("hidden").getBoolean();
        style.setHidden(b);
    }

    if (_struct.containsKey("locked")) {
        Boolean b = _struct.getData("locked").getBoolean();
        style.setLocked(b);
    }

    if (_struct.containsKey("indent")) {
        style.setIndention((short) _struct.getData("indent").getInt());
    }

    if (_struct.containsKey("rotation")) {
        style.setRotation((short) _struct.getData("rotation").getInt());
    }

    if (_struct.containsKey("dateformat")) {
        style.setDataFormat(workbook.createDataFormat().getFormat(_struct.getData("dateformat").getString()));
    }

    // Manage the fonts
    Font f = workbook.createFont();

    if (_struct.containsKey("strikeout")) {
        f.setStrikeout(true);
    }

    if (_struct.containsKey("bold")) {
        Boolean b = _struct.getData("bold").getBoolean();
        f.setBoldweight(b ? Font.BOLDWEIGHT_BOLD : Font.BOLDWEIGHT_NORMAL);
    }

    if (_struct.containsKey("underline")) {
        String v = _struct.getData("underline").getString();
        Byte b = lookup_underline.get(v);
        if (b == null) {
            throw new Exception("invalid parameter for 'underline' (" + v + ")");
        } else
            f.setUnderline(b);
    }

    if (_struct.containsKey("color")) {
        String v = _struct.getData("color").getString();
        Short s = lookup_colors.get(v);
        if (s == null) {
            throw new Exception("invalid parameter for 'color' (" + v + ")");
        } else
            f.setColor(s);
    }

    if (_struct.containsKey("fontsize")) {
        int s = _struct.getData("fontsize").getInt();
        f.setFontHeightInPoints((short) s);
    }

    if (_struct.containsKey("font")) {
        f.setFontName(_struct.getData("font").getString());
    }

    style.setFont(f);

    return style;
}

From source file:org.azkfw.doclet.jaxrs.writer.JAXRSXlsxDocletWriter.java

License:Apache License

private void printSheet(final XSSFWorkbook wb, final XSSFSheet sheet, final List<APIModel> apis) {

    XSSFCell cell = null;/*w w  w  . jav  a2  s . c o m*/
    XSSFRow row = null;

    ////////////////////////////////////////////////////
    XSSFFont fontBold = wb.createFont();
    fontBold.setBold(true);

    CellStyle styleHeader = wb.createCellStyle();
    styleHeader.setFillPattern(CellStyle.SOLID_FOREGROUND);
    styleHeader.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
    styleHeader.setFont(fontBold);
    styleHeader.setBorderTop(CellStyle.BORDER_THIN);
    styleHeader.setBorderLeft(CellStyle.BORDER_THIN);
    styleHeader.setBorderRight(CellStyle.BORDER_THIN);
    styleHeader.setBorderBottom(CellStyle.BORDER_DOUBLE);

    row = sheet.createRow(0); //////////////////////////
    cell = row.createCell(1, Cell.CELL_TYPE_STRING);

    CellStyle style1 = wb.createCellStyle();
    style1.setFont(fontBold);
    cell.setCellStyle(style1);
    cell.setCellValue("I/F (API) ");

    row = sheet.createRow(1); //////////////////////////
    cell = row.createCell(1, Cell.CELL_TYPE_STRING);
    cell.setCellValue("API???");

    row = sheet.createRow(4); //////////////////////////
    cell = row.createCell(1, Cell.CELL_TYPE_STRING);
    cell.setCellValue("ID");
    cell.setCellStyle(styleHeader);
    cell = row.createCell(2, Cell.CELL_TYPE_STRING);
    cell.setCellValue("Group");
    cell.setCellStyle(styleHeader);
    cell = row.createCell(3, Cell.CELL_TYPE_STRING);
    cell.setCellValue("Name");
    cell.setCellStyle(styleHeader);
    cell = row.createCell(4, Cell.CELL_TYPE_STRING);
    cell.setCellValue("Comment");
    cell.setCellStyle(styleHeader);

    int offsetRow = 5;
    int offsetCol = 1;
    for (int i = 0; i < apis.size(); i++) {
        APIModel api = apis.get(i);

        row = sheet.createRow(offsetRow + i);

        cell = row.createCell(offsetCol + 0, Cell.CELL_TYPE_STRING);
        cell.setCellValue(s(api.getId()));

        cell = row.createCell(offsetCol + 2, Cell.CELL_TYPE_STRING);
        cell.setCellValue(s(api.getName()));

        cell = row.createCell(offsetCol + 3, Cell.CELL_TYPE_STRING);
        cell.setCellValue(s(api.getComment()));
    }
}