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

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

Introduction

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

Prototype

void setAlignment(HorizontalAlignment align);

Source Link

Document

set the type of horizontal alignment for the cell

Usage

From source file:com.netxforge.netxstudio.server.logic.reporting.RFSServiceSummaryReportingLogic.java

License:Open Source License

private void writeSummary(Sheet sheet) {

    // Title/*from  w  ww.  j av  a2  s  .  c  om*/
    Row summaryRow = sheet.createRow(CONTENT_ROW);
    Cell summaryCell = summaryRow.createCell(2);
    summaryCell.setCellValue("Executive Summary");
    sheet.addMergedRegion(new CellRangeAddress(CONTENT_ROW, CONTENT_ROW, 2, 4));

    // Table
    CellStyle borderStyle = this.getWorkBook().createCellStyle();
    borderStyle.setBorderTop(CellStyle.BORDER_THIN);
    borderStyle.setBorderBottom(CellStyle.BORDER_THIN);
    borderStyle.setBorderLeft(CellStyle.BORDER_THIN);
    borderStyle.setBorderRight(CellStyle.BORDER_THIN);
    borderStyle.setAlignment(CellStyle.ALIGN_CENTER);
    borderStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

    Row headerRow = sheet.createRow(HEADER_ROW);
    {
        Cell c1 = headerRow.createCell(4);
        c1.setCellValue("Quantity");
        c1.setCellStyle(borderStyle);
    }

    {
        Cell c1 = headerRow.createCell(5);
        c1.setCellValue("RED");
        c1.setCellStyle(borderStyle);
    }

    {
        Cell c1 = headerRow.createCell(6);
        c1.setCellValue("AMBER");
        c1.setCellStyle(borderStyle);
    }

    {
        Cell c1 = headerRow.createCell(7);
        c1.setCellValue("GREEN");
        c1.setCellStyle(borderStyle);
    }

    writeServicesSummary(sheet, borderStyle);
    writeNodesSummary(sheet, borderStyle);
    writeResourcesSummary(sheet, borderStyle);

}

From source file:com.netxforge.netxstudio.server.reporting.XLSXPOIEmitter.java

License:Open Source License

public void createHeaderStructure() {

    CellStyle baseStyle = this.getWorkBook().createCellStyle();
    baseStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
    baseStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
    baseStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
    baseStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
    baseStyle.setAlignment(CellStyle.ALIGN_LEFT);

    CellStyle typeStyle = this.getWorkBook().createCellStyle();
    typeStyle.cloneStyleFrom(baseStyle);

    Font typeFont = getWorkBook().createFont();
    typeFont.setFontHeightInPoints((short) 24);
    typeStyle.setFont(typeFont);/*w w  w.j  a  v  a2s  .  co m*/

    Row typeRow = sheet.createRow(0);
    typeCell = typeRow.createCell(0);
    typeCell.setCellValue("<Service Type>");
    typeCell.setCellStyle(typeStyle);

    for (int i = 1; i < HEADER_CELL_SIZE; i++) {
        typeRow.createCell(i).setCellStyle(typeStyle);
    }

    CellStyle titleStyle = this.getWorkBook().createCellStyle();
    titleStyle.cloneStyleFrom(baseStyle);

    Font titleFont = getWorkBook().createFont();
    titleFont.setFontHeightInPoints((short) 16);
    titleStyle.setFont(titleFont);

    Row titleRow = sheet.createRow(1);
    titleCell = titleRow.createCell(0);
    titleCell.setCellValue("<Report title>");
    titleCell.setCellStyle(titleStyle);

    for (int i = 1; i < HEADER_CELL_SIZE; i++) {
        titleRow.createCell(i).setCellStyle(titleStyle);
    }

    Row periodRow = sheet.createRow(2);
    periodCell = periodRow.createCell(0);
    periodCell.setCellValue("<Period>");
    periodCell.setCellStyle(titleStyle);

    for (int i = 1; i < HEADER_CELL_SIZE; i++) {
        periodRow.createCell(i).setCellStyle(typeStyle);
    }

    // Merge
    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, HEADER_CELL_SIZE - 1));
    sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, HEADER_CELL_SIZE - 1));
    sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, HEADER_CELL_SIZE - 1));
}

From source file:com.perceptive.epm.perkolcentral.bl.ExcelReportBL.java

private void setCellBorder(Workbook wb, Cell cell) {
    CellStyle style = wb.createCellStyle();
    style.setBorderBottom(CellStyle.BORDER_THIN);
    style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
    style.setBorderLeft(CellStyle.BORDER_THIN);
    style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
    style.setBorderRight(CellStyle.BORDER_THIN);
    style.setRightBorderColor(IndexedColors.BLACK.getIndex());
    style.setBorderTop(CellStyle.BORDER_THIN);
    style.setTopBorderColor(IndexedColors.BLACK.getIndex());

    // Create a new font and alter it.
    Font font = wb.createFont();/*from   w  ww .j  a va2s .  com*/
    font.setFontHeightInPoints((short) 9);
    font.setFontName("Georgia");
    style.setFont(font);

    style.setWrapText(true);
    style.setAlignment(CellStyle.VERTICAL_CENTER);
    style.setAlignment(CellStyle.ALIGN_LEFT);
    cell.setCellStyle(style);
    cell.setCellType(Cell.CELL_TYPE_STRING);
}

From source file:com.plugin.excel.util.ExcelFileHelper.java

License:Apache License

private static void formatCell(Workbook workbook, Cell cell, ExcelCell excell,
        Map<IndexedColors, CellStyle> s_cellStyle, Font font, Font invisibleFont) {

    if (excell.getFormat() != null) {

        ExcelFormat format = excell.getFormat();

        CellStyle style = s_cellStyle.get(format.getBackgroundColor());

        if (format.isDate()) {
            // for date create a new style
            style = getDateStyle("date", cell.getSheet(), font);
            XSSFCreationHelper createHelper = (XSSFCreationHelper) cell.getSheet().getWorkbook()
                    .getCreationHelper();
            style.setDataFormat(createHelper.createDataFormat().getFormat("MMMM dd, yyyy"));
            font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
            font.setBold(false);/* w  w w .j a v  a 2 s .c  o  m*/
            font.setFontHeightInPoints((short) 12);
            style.setFont(font);
            cell.setCellValue(new Date());
        }

        if (style == null) {
            style = workbook.createCellStyle();
            s_cellStyle.put(format.getBackgroundColor(), style);
        }

        if (format.getAlignment() > 0) {
            style.setAlignment(format.getAlignment());
        }
        if (format.getBackgroundColor() != null && !IndexedColors.WHITE.equals(format.getBackgroundColor())) {
            style.setFillForegroundColor(format.getBackgroundColor().getIndex());
            style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        }
        if (format.getTextColor() != null) {
            font.setColor(format.getTextColor().getIndex());
            style.setFont(font);
        }
        if (format.isBold()) {
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        }
        if (format.getFontHeight() > 0) {
            font.setFontHeightInPoints(format.getFontHeight());
        }
        if (format.isWrapText()) {
            style.setWrapText(true);
        }
        style.setFont(font);
        if (format.isHideText()) {
            invisibleFont.setColor(IndexedColors.WHITE.getIndex());
            style.setFont(invisibleFont);
        }
        cell.setCellStyle(style);

    } else {
        // Let's set default formatting for free text cell
        IndexedColors defaultStyle = IndexedColors.AUTOMATIC; // we are using this index
        CellStyle style = s_cellStyle.get(defaultStyle);
        if (style == null) {
            style = workbook.createCellStyle();
            s_cellStyle.put(defaultStyle, style);
        }
        style.setWrapText(true);
        cell.setCellStyle(style);

    }

}

From source file:com.primovision.lutransport.core.util.TollCompanyTagUploadUtil.java

private static void createColumnHeaders(LinkedList<String> expectedColumnList, Sheet sheet, CellStyle style) {
    Row headerRow = sheet.createRow(0);//from   ww w .  j  a v  a2  s. c  o m
    int columnHeaderIndex = 0;
    for (String columnHeader : expectedColumnList) { // TODO redundant, use actualColumnListMap.keys instead
        sheet.setColumnWidth(columnHeaderIndex, 256 * 20);
        Cell cell = headerRow.createCell(columnHeaderIndex++);
        cell.setCellValue(columnHeader);
        style.setAlignment(CellStyle.ALIGN_CENTER);
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cell.setCellStyle(style);
    }
}

From source file:com.rarediscovery.services.logic.WorkPad.java

protected CellStyle applySelectedStyle() {
    Font font = workbook.createFont();
    if (bold) {//from   w  w w .  ja va 2  s  . c  om
        font.setBold(true);
    } else {
        font.setBold(false);
    }

    CellStyle style = workbook.createCellStyle();
    style.setFont(font);

    if (alignRight) {
        style.setAlignment(CellStyle.ALIGN_RIGHT);
    } else {
        style.setAlignment(CellStyle.ALIGN_LEFT);
    }

    if (backgroundColor) {
        style.setFillBackgroundColor(IndexedColors.ORANGE.getIndex());
        style.setFillPattern(CellStyle.BIG_SPOTS);
    } else {
        style.setFillBackgroundColor(IndexedColors.WHITE.getIndex());
    }

    return style;
}

From source file:com.saba.CalendarDemo.java

License:Apache License

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) 14);
    titleFont.setFontName("Trebuchet MS");
    titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style = createBorderedStyle(wb);/*from w  ww.  j  a va  2 s  . c om*/
    style.setFont(titleFont);
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    styles.put("title", style);

    Font itemFontLeft = wb.createFont();
    itemFontLeft.setFontHeightInPoints((short) 11);
    itemFontLeft.setFontName("Trebuchet MS");
    itemFontLeft.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setFont(itemFontLeft);
    styles.put("item_left", style);

    Font itemFontRight = wb.createFont();
    itemFontRight.setFontHeightInPoints((short) 10);
    itemFontRight.setFontName("Trebuchet MS");
    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setFont(itemFontRight);
    styles.put("item_right", style);

    return styles;
}

From source file:com.saba.CalendarDemo.java

License:Apache License

/**
* createHeaderStyle : //from   www.j  a  va  2 s. c  o  m
* Header row setting for sheet
* @param wb
* @return
*/
private static CellStyle createHeaderStyleForAward(Workbook wb) {
    CellStyle headerStyle;
    Font headerFont = wb.createFont();
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    headerStyle = createBorderedStyle(wb);
    headerStyle.setAlignment(CellStyle.ALIGN_LEFT);
    headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    headerStyle.setFont(headerFont);
    return headerStyle;
}

From source file:com.sccl.attech.common.utils.excel.ExportExcel.java

License:Open Source License

/**
 * ?/*from   w  ww. jav  a2  s .  com*/
 * @param row 
 * @param column ?
 * @param val 
 * @param align ??1?23??
 * @return ?
 */
public Cell addCellStyle(Row row, int column, Object val, int align, Class<?> fieldType) {
    Cell cell = row.createCell(column);
    CellStyle style = styles.get("data" + (align >= 1 && align <= 3 ? align : ""));
    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
    style.setWrapText(true);
    try {
        if (val == null) {
            cell.setCellValue("");
        } else if (val instanceof String) {
            cell.setCellValue((String) val);
        } else if (val instanceof Integer) {
            cell.setCellValue((Integer) val);
        } else if (val instanceof Long) {
            cell.setCellValue((Long) val);
        } else if (val instanceof Double) {
            cell.setCellValue((Double) val);
        } else if (val instanceof Float) {
            cell.setCellValue((Float) val);
        } else if (val instanceof Date) {
            DataFormat format = wb.createDataFormat();
            style.setDataFormat(format.getFormat("yyyy-MM-dd"));
            cell.setCellValue((Date) val);
        } else {
            if (fieldType != Class.class) {
                cell.setCellValue((String) fieldType.getMethod("setValue", Object.class).invoke(null, val));
            } else {
                cell.setCellValue((String) Class
                        .forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
                                "fieldtype." + val.getClass().getSimpleName() + "Type"))
                        .getMethod("setValue", Object.class).invoke(null, val));
            }
        }
    } catch (Exception ex) {
        log.info("Set cell value [" + row.getRowNum() + "," + column + "] error: " + ex.toString());
        cell.setCellValue(val.toString());
    }
    if (column == 8) {
        CellRangeAddressList regions = new CellRangeAddressList(25, 25, 8, 8);
        // ?  
        DVConstraint constraint = DVConstraint
                .createExplicitListConstraint(new String[] { "2", "3", "" });
        //   
        HSSFDataValidation data_validation = new HSSFDataValidation(regions, constraint);
        // sheet  
        data_validation.createErrorBox("Error", "Error");
        data_validation.createPromptBox("", null);
        sheet.addValidationData(data_validation);
    }
    cell.setCellStyle(style);
    return cell;
}

From source file:com.sec.ose.osi.report.standard.BillOfMaterialsSheetTemplate.java

License:Open Source License

public void writeLicenseRow(ArrayList<LicenseSummaryRow> licenseList) {
    curRow += 3;/*from  w w w .  j  a v  a  2 s .  co m*/

    CellStyle styleLicenseTitle = getCellStyle(YELLOW, getFont(FONT_BLACK, (short) 10, true));
    styleLicenseTitle.setAlignment(CellStyle.ALIGN_LEFT);
    CellStyle styleLicenseSum = getCellStyle(YELLOW, getFont(FONT_BLACK, (short) 10, true));

    Row row = sheet.createRow(curRow++);

    Cell cell = row.createCell(ISheetTemplate.COL_B);
    cell.setCellValue("License State");
    cell.setCellStyle(styleLicenseTitle);

    cell = row.createCell(ISheetTemplate.COL_C);
    cell.setCellFormula("SUM(C" + (curRow + 1) + ":C" + (curRow + licenseList.size()) + ")");
    cell.setCellStyle(styleLicenseSum);

    for (LicenseSummaryRow licenseRow : licenseList) {
        row = sheet.createRow(curRow++);

        cell = row.createCell(ISheetTemplate.COL_B);
        cell.setCellValue(licenseRow.getLicense());
        cell.setCellStyle(leftStyle);

        cell = row.createCell(ISheetTemplate.COL_C);
        cell.setCellValue(licenseRow.getCount());
        cell.setCellStyle(normalStyle);

    }

}