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

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

Introduction

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

Prototype

void setDataFormat(short fmt);

Source Link

Document

set the data format (must be a valid format).

Usage

From source file:com.frameworkset.platform.util.POIExcelUtil.java

License:Open Source License

private static CellStyle getDateTimeCellStyle(XSSFWorkbook wb) {// 
    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
    return cellStyle;
}

From source file:com.funtl.framework.smoke.core.commons.excel.ExportExcel.java

License:Apache License

/**
 * ?/* w  w  w. ja v a2s.  c  o m*/
 *
 * @param row    
 * @param column ?
 * @param val    
 * @param align  ??1?23??
 * @return ?
 */
public Cell addCell(Row row, int column, Object val, int align, Class<?> fieldType) {
    Cell cell = row.createCell(column);
    String cellFormatString = "@";
    try {
        if (val == null) {
            cell.setCellValue("");
        } else if (fieldType != Class.class) {
            cell.setCellValue((String) fieldType.getMethod("setValue", Object.class).invoke(null, val));
        } else {
            if (val instanceof String) {
                cell.setCellValue((String) val);
            } else if (val instanceof Integer) {
                cell.setCellValue((Integer) val);
                cellFormatString = "0";
            } else if (val instanceof Long) {
                cell.setCellValue((Long) val);
                cellFormatString = "0";
            } else if (val instanceof Double) {
                cell.setCellValue((Double) val);
                cellFormatString = "0.00";
            } else if (val instanceof Float) {
                cell.setCellValue((Float) val);
                cellFormatString = "0.00";
            } else if (val instanceof Date) {
                cell.setCellValue((Date) val);
                cellFormatString = "yyyy-MM-dd HH:mm";
            } 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));
            }
        }
        if (val != null) {
            CellStyle style = styles.get("data_column_" + column);
            if (style == null) {
                style = wb.createCellStyle();
                style.cloneStyleFrom(styles.get("data" + (align >= 1 && align <= 3 ? align : "")));
                style.setDataFormat(wb.createDataFormat().getFormat(cellFormatString));
                styles.put("data_column_" + column, style);
            }
            cell.setCellStyle(style);
        }
    } catch (Exception ex) {
        log.info("Set cell value [" + row.getRowNum() + "," + column + "] error: " + ex.toString());
        cell.setCellValue(val.toString());
    }
    return cell;
}

From source file:com.github.cutstock.excel.model.SheetBuilder.java

License:Apache License

private void initStyles() {
    CellStyle style;
    Workbook wb = sheet.getWorkbook();/*from   www .  jav  a2  s  .  c o m*/

    Font titleFont = wb.createFont();
    titleFont.setFontHeightInPoints((short) 18);
    titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFont(titleFont);
    styles.put("title", style);

    Font subTitle = wb.createFont();
    subTitle.setFontHeightInPoints((short) 12);
    subTitle.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setFont(subTitle);
    styles.put("header", style);

    style = wb.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    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_LEFT);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    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("highCell", style);

    style = wb.createCellStyle();
    Font imageTitle = wb.createFont();
    imageTitle.setFontHeightInPoints((short) 200);
    style.setFont(imageTitle);
    styles.put("image", 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);

}

From source file:com.github.svrtm.xlreport.CellStyle_p.java

License:Apache License

public void copyTo(final CellStyle poiStyle) {
    if (verticalAlignment != null)
        poiStyle.setVerticalAlignment(verticalAlignment);
    if (alignment != null)
        poiStyle.setAlignment(alignment);

    if (borderBottom != null)
        poiStyle.setBorderBottom(borderBottom);
    if (bottomBorderColor != null)
        poiStyle.setBottomBorderColor(bottomBorderColor);
    if (borderLeft != null)
        poiStyle.setBorderLeft(borderLeft);
    if (leftBorderColor != null)
        poiStyle.setLeftBorderColor(leftBorderColor);
    if (borderRight != null)
        poiStyle.setBorderRight(borderRight);
    if (rightBorderColor != null)
        poiStyle.setRightBorderColor(rightBorderColor);
    if (borderTop != null)
        poiStyle.setBorderTop(borderTop);
    if (topBorderColor != null)
        poiStyle.setTopBorderColor(topBorderColor);

    if (wrapText != null)
        poiStyle.setWrapText(wrapText);/*ww  w  .jav a 2  s  . c  om*/

    if (fillPattern != null)
        poiStyle.setFillPattern(fillPattern);
    if (xssfFgColor == null) {
        if (fillForegroundColor != null)
            poiStyle.setFillForegroundColor(fillForegroundColor);
    } else
        ((XSSFCellStyle) poiStyle).setFillForegroundColor(xssfFgColor);

    if (fmt != null)
        poiStyle.setDataFormat(fmt);
}

From source file:com.globalsight.everest.webapp.pagehandler.administration.reports.generator.OnlineJobsReportForIPTranslatorGenerator.java

License:Apache License

private CellStyle getDateStyle(Workbook p_workbook) throws Exception {
    if (dateStyle == null) {
        DataFormat dataFormat = p_workbook.createDataFormat();

        Font dateFont = p_workbook.createFont();
        dateFont.setFontName("Arial");
        dateFont.setFontHeightInPoints((short) 10);

        CellStyle cs = p_workbook.createCellStyle();
        cs.setWrapText(false);//from  w  ww. j  a va  2 s. co m
        cs.setFont(dateFont);
        cs.setDataFormat(dataFormat.getFormat("M/d/yy"));

        dateStyle = cs;
    }
    return dateStyle;
}

From source file:com.globalsight.everest.webapp.pagehandler.administration.reports.generator.OnlineJobsReportForIPTranslatorGenerator.java

License:Apache License

private CellStyle getMoneyStyle(Workbook p_workbook) throws Exception {
    if (moneyStyle == null) {
        String euroJavaNumberFormat = getCurrencyNumberFormat();
        DataFormat euroNumberFormat = p_workbook.createDataFormat();

        CellStyle cs = p_workbook.createCellStyle();
        cs.setDataFormat(euroNumberFormat.getFormat(euroJavaNumberFormat));
        cs.setWrapText(false);//from   w  w  w .  j av  a  2s.  c o  m

        moneyStyle = cs;
    }

    return moneyStyle;
}

From source file:com.globalsight.everest.webapp.pagehandler.administration.reports.generator.OnlineJobsReportForIPTranslatorGenerator.java

License:Apache License

private CellStyle getFailedDateStyle(Workbook p_workbook) throws Exception {
    if (failedDateStyle == null) {
        DataFormat dataFormat = p_workbook.createDataFormat();

        Font dateFont = p_workbook.createFont();
        dateFont.setFontName("Arial");
        dateFont.setFontHeightInPoints((short) 10);

        CellStyle cs = p_workbook.createCellStyle();
        cs.setWrapText(false);/*www  .j  a v  a  2  s . c o  m*/
        cs.setFont(dateFont);
        cs.setDataFormat(dataFormat.getFormat("M/d/yy"));
        cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cs.setFillForegroundColor(IndexedColors.RED.getIndex());
        failedDateStyle = cs;
    }
    return failedDateStyle;
}

From source file:com.globalsight.everest.webapp.pagehandler.administration.reports.generator.OnlineJobsReportForIPTranslatorGenerator.java

License:Apache License

private CellStyle getFailedTimeStyle(Workbook p_workbook) throws Exception {
    if (failedTimeStyle == null) {
        DataFormat dataFormat = p_workbook.createDataFormat();

        CellStyle cs = p_workbook.createCellStyle();
        cs.setWrapText(false);// w w  w.ja v  a2 s  . c o  m
        cs.setDataFormat(dataFormat.getFormat("h:mm:ss AM/PM"));
        cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cs.setFillForegroundColor(IndexedColors.RED.getIndex());
        failedTimeStyle = cs;
    }
    return failedTimeStyle;
}

From source file:com.globalsight.everest.webapp.pagehandler.administration.reports.generator.OnlineJobsReportForIPTranslatorGenerator.java

License:Apache License

private CellStyle getFailedMoneyStyle(Workbook p_workbook) throws Exception {
    if (failedMoneyStyle == null) {
        String euroJavaNumberFormat = getCurrencyNumberFormat();
        DataFormat euroNumberFormat = p_workbook.createDataFormat();

        CellStyle cs = p_workbook.createCellStyle();
        cs.setWrapText(false);//from  w ww  .  ja va 2s. c  o m
        cs.setDataFormat(euroNumberFormat.getFormat(euroJavaNumberFormat));
        cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cs.setFillForegroundColor(IndexedColors.RED.getIndex());
        failedMoneyStyle = cs;
    }
    return failedMoneyStyle;
}

From source file:com.globalsight.everest.webapp.pagehandler.administration.reports.generator.OnlineJobsReportForIPTranslatorGenerator.java

License:Apache License

private CellStyle getTotalMoneyStyle(Workbook p_workbook) throws Exception {
    if (totalMoneyStyle == null) {
        String euroJavaNumberFormat = getCurrencyNumberFormat();
        DataFormat euroNumberFormat = p_workbook.createDataFormat();

        CellStyle cs = p_workbook.createCellStyle();
        cs.setDataFormat(euroNumberFormat.getFormat(euroJavaNumberFormat));
        cs.setWrapText(false);//www  .  jav a  2s.c om
        cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cs.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        cs.setBorderTop(CellStyle.BORDER_THIN);
        cs.setBorderBottom(CellStyle.BORDER_THIN);

        totalMoneyStyle = cs;
    }
    return totalMoneyStyle;
}