Example usage for org.apache.poi.ss.usermodel DataFormat getFormat

List of usage examples for org.apache.poi.ss.usermodel DataFormat getFormat

Introduction

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

Prototype

String getFormat(short index);

Source Link

Document

get the format string that matches the given format index

Usage

From source file:com.asakusafw.testdriver.excel.WorkbookInfo.java

License:Apache License

/**
 * Creates a new instance./* www .  j a  va 2s . co  m*/
 * @param workbook target workbook
 * @throws IllegalArgumentException if some parameters were {@code null}
 */
public WorkbookInfo(Workbook workbook) {
    if (workbook == null) {
        throw new IllegalArgumentException("workbook must not be null"); //$NON-NLS-1$
    }
    this.workbook = workbook;

    Font font = workbook.createFont();

    commonStyle = workbook.createCellStyle();
    commonStyle.setFont(font);
    commonStyle.setBorderTop(CellStyle.BORDER_THIN);
    commonStyle.setBorderBottom(CellStyle.BORDER_THIN);
    commonStyle.setBorderLeft(CellStyle.BORDER_THIN);
    commonStyle.setBorderRight(CellStyle.BORDER_THIN);

    titleStyle = workbook.createCellStyle();
    titleStyle.cloneStyleFrom(commonStyle);
    titleStyle.setLocked(true);
    titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    titleStyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
    titleStyle.setAlignment(CellStyle.ALIGN_CENTER);

    CreationHelper helper = workbook.getCreationHelper();
    DataFormat df = helper.createDataFormat();

    dataStyle = workbook.createCellStyle();
    dataStyle.cloneStyleFrom(commonStyle);

    dateDataStyle = workbook.createCellStyle();
    dateDataStyle.cloneStyleFrom(commonStyle);
    dateDataStyle.setDataFormat(df.getFormat("yyyy-mm-dd")); //$NON-NLS-1$

    timeDataStyle = workbook.createCellStyle();
    timeDataStyle.cloneStyleFrom(commonStyle);
    timeDataStyle.setDataFormat(df.getFormat("hh:mm:ss")); //$NON-NLS-1$

    datetimeDataStyle = workbook.createCellStyle();
    datetimeDataStyle.cloneStyleFrom(commonStyle);
    datetimeDataStyle.setDataFormat(df.getFormat("yyyy-mm-dd hh:mm:ss")); //$NON-NLS-1$
}

From source file:com.asakusafw.testtools.templategen.ExcelBookBuilder.java

License:Apache License

private void configureColumnStyle() {
    assert workbook != null;
    HSSFFont font = workbook.createFont();
    font.setFontName(" ");

    commonStyle = workbook.createCellStyle();
    commonStyle.setFont(font);// w ww  . j  a  v a  2s  . co  m
    commonStyle.setBorderTop(CellStyle.BORDER_THIN);
    commonStyle.setBorderBottom(CellStyle.BORDER_THIN);
    commonStyle.setBorderLeft(CellStyle.BORDER_THIN);
    commonStyle.setBorderRight(CellStyle.BORDER_THIN);

    titleStyle = workbook.createCellStyle();
    titleStyle.cloneStyleFrom(commonStyle);
    titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    titleStyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
    titleStyle.setAlignment(CellStyle.ALIGN_CENTER);

    centerAlignStyle = workbook.createCellStyle();
    centerAlignStyle.cloneStyleFrom(commonStyle);
    centerAlignStyle.setAlignment(CellStyle.ALIGN_CENTER);

    fixedValueStyle = workbook.createCellStyle();
    fixedValueStyle.cloneStyleFrom(commonStyle);
    fixedValueStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    fixedValueStyle.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());

    centerAlignFixedValueStyle = workbook.createCellStyle();
    centerAlignFixedValueStyle.cloneStyleFrom(fixedValueStyle);
    centerAlignFixedValueStyle.setAlignment(CellStyle.ALIGN_CENTER);

    CreationHelper helper = workbook.getCreationHelper();
    DataFormat df = helper.createDataFormat();

    dateTimeStyle = workbook.createCellStyle();
    dateTimeStyle.cloneStyleFrom(commonStyle);
    dateTimeStyle.setDataFormat(df.getFormat("yyyy-mm-dd hh:mm:ss"));

    dateStyle = workbook.createCellStyle();
    dateStyle.cloneStyleFrom(commonStyle);
    dateStyle.setDataFormat(df.getFormat("yyyy-mm-dd"));
}

From source file:com.b510.excel.client.BusinessPlan.java

License:Apache License

/**
 * create a library of cell styles// w ww  . ja v a2 s . c om
 */
private static Map<String, CellStyle> createStyles(Workbook wb) {
    Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
    DataFormat df = wb.createDataFormat();

    CellStyle style;
    Font headerFont = wb.createFont();
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFont(headerFont);
    styles.put("header", style);

    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFont(headerFont);
    style.setDataFormat(df.getFormat("d-mmm"));
    styles.put("header_date", style);

    Font font1 = wb.createFont();
    font1.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setFont(font1);
    styles.put("cell_b", style);

    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setFont(font1);
    styles.put("cell_b_centered", style);

    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_RIGHT);
    style.setFont(font1);
    style.setDataFormat(df.getFormat("d-mmm"));
    styles.put("cell_b_date", style);

    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_RIGHT);
    style.setFont(font1);
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setDataFormat(df.getFormat("d-mmm"));
    styles.put("cell_g", style);

    Font font2 = wb.createFont();
    font2.setColor(IndexedColors.BLUE.getIndex());
    font2.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setFont(font2);
    styles.put("cell_bb", style);

    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_RIGHT);
    style.setFont(font1);
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setDataFormat(df.getFormat("d-mmm"));
    styles.put("cell_bg", style);

    Font font3 = wb.createFont();
    font3.setFontHeightInPoints((short) 14);
    font3.setColor(IndexedColors.DARK_BLUE.getIndex());
    font3.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setFont(font3);
    style.setWrapText(true);
    styles.put("cell_h", style);

    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setWrapText(true);
    styles.put("cell_normal", style);

    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setWrapText(true);
    styles.put("cell_normal_centered", style);

    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_RIGHT);
    style.setWrapText(true);
    style.setDataFormat(df.getFormat("d-mmm"));
    styles.put("cell_normal_date", style);

    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_LEFT);
    style.setIndention((short) 1);
    style.setWrapText(true);
    styles.put("cell_indented", style);

    style = createBorderedStyle(wb);
    style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    styles.put("cell_blue", style);

    return styles;
}

From source file:com.ebay.xcelite.styles.CellStyles.java

License:Apache License

private void createTextStyle() {
    textStyle = wb.createCellStyle();//from w w w  .ja v a 2s. c  om
    DataFormat df = wb.createDataFormat();
    textStyle.setDataFormat(df.getFormat("@"));
}

From source file:com.ebay.xcelite.styles.CellStyles.java

License:Apache License

private void createBoldStyle() {
    boldStyle = wb.createCellStyle();/* w w w  .  j  a v  a2 s.  c  o m*/
    Font font = wb.createFont();
    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
    boldStyle.setFont(font);
    DataFormat df = wb.createDataFormat();
    boldStyle.setDataFormat(df.getFormat("@"));
}

From source file:com.ebay.xcelite.styles.CellStyles.java

License:Apache License

private void createDateFormatStyle() {
    dateStyle = wb.createCellStyle();//from   w  w w  .ja va  2s  . co m
    DataFormat df = wb.createDataFormat();
    dateStyle.setDataFormat(df.getFormat(DEFAULT_DATE_FORMAT));
}

From source file:com.ebay.xcelite.styles.CellStyles.java

License:Apache License

public CellStyle getCustomDataFormatStyle(String dataFormat) {
    CellStyle cellStyle = wb.createCellStyle();
    DataFormat df = wb.createDataFormat();
    cellStyle.setDataFormat(df.getFormat(dataFormat));
    return cellStyle;
}

From source file:com.efficio.fieldbook.web.nursery.service.impl.ExcelExportStudyServiceImpl.java

License:Open Source License

private void writeObservationRow(int currentRowNum, HSSFSheet xlsSheet, MeasurementRow dataRow,
        HSSFWorkbook xlsBook) {/* www. ja  v  a2s  . c om*/
    HSSFRow row = xlsSheet.createRow(currentRowNum);
    int currentColNum = 0;
    CellStyle style = xlsBook.createCellStyle();
    DataFormat format = xlsBook.createDataFormat();
    style.setDataFormat(format.getFormat("0.#"));
    for (MeasurementData dataCell : dataRow.getDataList()) {
        HSSFCell cell = row.createCell(currentColNum++);
        /*
        if(AppConstants.NUMERIC_DATA_TYPE.getString().equalsIgnoreCase(dataCell.getDataType())){
           cell.setCellType(Cell.CELL_TYPE_BLANK);
           cell.setCellType(Cell.CELL_TYPE_NUMERIC);            
        }*/
        cell.setCellValue(dataCell.getValue());

    }
}

From source file:com.github.gaborfeher.grantmaster.framework.base.ExcelExporter.java

License:Open Source License

private void setExcelCell(HSSFWorkbook workbook, Object cellValue, Cell excelCell) {
    if (cellValue instanceof BigDecimal) {
        double doubleValue = ((BigDecimal) cellValue).doubleValue();
        excelCell.setCellValue(doubleValue);
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        HSSFDataFormat hssfDataFormat = workbook.createDataFormat();
        cellStyle.setDataFormat(hssfDataFormat.getFormat("#,##0.00"));
        excelCell.setCellStyle(cellStyle);
        excelCell.setCellType(Cell.CELL_TYPE_NUMERIC);
    } else if (cellValue instanceof LocalDate) {
        LocalDate localDate = (LocalDate) cellValue;
        Calendar calendar = Calendar.getInstance();
        calendar.set(localDate.getYear(), localDate.getMonthValue() - 1, localDate.getDayOfMonth());
        excelCell.setCellValue(calendar);

        String excelFormatPattern = DateFormatConverter.convert(Locale.US, "yyyy-MM-DD");
        CellStyle cellStyle = workbook.createCellStyle();
        DataFormat poiFormat = workbook.createDataFormat();
        cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern));
        excelCell.setCellStyle(cellStyle);
    } else if (cellValue != null) {
        excelCell.setCellValue(cellValue.toString());
    }//  www.j a va 2  s  .co m
}

From source file:com.globalsight.everest.webapp.pagehandler.administration.reports.customize.ExcelReportWriter.java

License:Apache License

private void setFormats() throws IOException {
    // Set title format
    Font titleFont = this.workbook.createFont();
    titleFont.setUnderline(Font.U_NONE);
    titleFont.setFontName("Times");
    titleFont.setFontHeightInPoints((short) 14);
    titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    titleFont.setColor(IndexedColors.BLACK.getIndex());

    this.titleStyle = this.workbook.createCellStyle();
    titleStyle.setFont(titleFont);//  www  . ja v  a2s . c  om
    titleStyle.setWrapText(false);

    // Set header format
    Font headerFont = this.workbook.createFont();
    headerFont.setUnderline(Font.U_NONE);
    headerFont.setFontName("Times");
    headerFont.setFontHeightInPoints((short) 11);
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    headerFont.setColor(IndexedColors.BLACK.getIndex());

    this.headerStyle = this.workbook.createCellStyle();
    headerStyle.setFont(headerFont);
    headerStyle.setWrapText(true);
    headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    headerStyle.setBorderBottom(CellStyle.BORDER_THIN);
    headerStyle.setBorderRight(CellStyle.BORDER_THIN);
    headerStyle.setBorderTop(CellStyle.BORDER_THIN);
    headerStyle.setBorderLeft(CellStyle.BORDER_THIN);

    // Set content format
    Font font = this.workbook.createFont();
    font.setFontName("Arial");
    font.setFontHeightInPoints((short) 10);

    this.stringStyle = this.workbook.createCellStyle();
    stringStyle.setAlignment(CellStyle.ALIGN_LEFT);
    stringStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    this.intStyle = this.workbook.createCellStyle();
    intStyle.setAlignment(CellStyle.ALIGN_LEFT);
    intStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

    // Set money format
    String moneyFormatString = CurrencyThreadLocal.getMoneyFormatString();
    DataFormat moneyFormat = this.workbook.createDataFormat();
    this.moneyStyle = this.workbook.createCellStyle();
    moneyStyle.setDataFormat(moneyFormat.getFormat(moneyFormatString));
    moneyStyle.setWrapText(false);

    // Set total format
    Font totalFont = this.workbook.createFont();
    totalFont.setUnderline(Font.U_NONE);
    totalFont.setFontName("Arial");
    totalFont.setFontHeightInPoints((short) 10);
    totalFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    totalFont.setColor(IndexedColors.BLACK.getIndex());

    this.totalStyle = this.workbook.createCellStyle();
    totalStyle.setFont(totalFont);
    totalStyle.setWrapText(true);
    totalStyle.setBorderTop(CellStyle.BORDER_THIN);
    totalStyle.setBorderBottom(CellStyle.BORDER_THIN);
    totalStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    totalStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());

    // Set total money format
    this.totalMoneyStyle = this.workbook.createCellStyle();
    totalMoneyStyle.setDataFormat(moneyFormat.getFormat(moneyFormatString));
    totalMoneyStyle.setWrapText(false);
    totalMoneyStyle.setBorderTop(CellStyle.BORDER_THIN);
    totalMoneyStyle.setBorderBottom(CellStyle.BORDER_THIN);
    totalMoneyStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    totalMoneyStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
}