Example usage for org.apache.poi.hssf.usermodel HSSFCellStyle setDataFormat

List of usage examples for org.apache.poi.hssf.usermodel HSSFCellStyle setDataFormat

Introduction

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

Prototype

@Override
public void setDataFormat(short fmt) 

Source Link

Document

set the data format (must be a valid format)

Usage

From source file:net.sf.jasperreports.engine.export.JRXlsMetadataExporter.java

License:Open Source License

protected HSSFCellStyle getLoadedCellStyle(StyleInfo style) {
    HSSFCellStyle cellStyle = loadedCellStyles.get(style);
    if (cellStyle == null) {

        cellStyle = workbook.createCellStyle();
        cellStyle.setFillForegroundColor(style.backcolor);
        cellStyle.setFillPattern(style.mode);
        cellStyle.setAlignment(style.horizontalAlignment);
        cellStyle.setVerticalAlignment(style.verticalAlignment);
        cellStyle.setRotation(style.rotation);
        cellStyle.setFont(style.font);//w w  w  . ja  va  2s.c  om
        cellStyle.setWrapText(style.lcWrapText);
        cellStyle.setLocked(style.lcCellLocked);
        cellStyle.setHidden(style.lcCellHidden);

        if (style.hasDataFormat()) {
            cellStyle.setDataFormat(style.getDataFormat());
        }

        if (!getCurrentItemConfiguration().isIgnoreCellBorder()) {
            BoxStyle box = style.box;
            cellStyle.setBorderTop(box.borderStyle[BoxStyle.TOP]);
            cellStyle.setTopBorderColor(box.borderColour[BoxStyle.TOP]);
            cellStyle.setBorderLeft(box.borderStyle[BoxStyle.LEFT]);
            cellStyle.setLeftBorderColor(box.borderColour[BoxStyle.LEFT]);
            cellStyle.setBorderBottom(box.borderStyle[BoxStyle.BOTTOM]);
            cellStyle.setBottomBorderColor(box.borderColour[BoxStyle.BOTTOM]);
            cellStyle.setBorderRight(box.borderStyle[BoxStyle.RIGHT]);
            cellStyle.setRightBorderColor(box.borderColour[BoxStyle.RIGHT]);
        }
        loadedCellStyles.put(style, cellStyle);
    }
    return cellStyle;
}

From source file:net.vpc.app.vainruling.core.web.jsf.Vr.java

public void postProcessDataExporterXLS(Object document) {
    HSSFWorkbook book = (HSSFWorkbook) document;
    HSSFSheet sheet = book.getSheetAt(0);
    HSSFRow header = sheet.getRow(0);//from  w w  w.  ja  v  a 2s.  c  o m
    int rowCount = sheet.getPhysicalNumberOfRows();
    HSSFCellStyle headerCellStyle = book.createCellStyle();
    headerCellStyle.setFillForegroundColor(HSSFColor.AQUA.index);
    headerCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
    HSSFCreationHelper creationHelper = book.getCreationHelper();

    for (int i = 0; i < header.getPhysicalNumberOfCells(); i++) {
        HSSFCell cell = header.getCell(i);

        cell.setCellStyle(headerCellStyle);
    }

    HSSFCellStyle intStyle = book.createCellStyle();
    intStyle.setDataFormat((short) 1);

    HSSFCellStyle decStyle = book.createCellStyle();
    decStyle.setDataFormat((short) 2);

    HSSFCellStyle dollarStyle = book.createCellStyle();
    dollarStyle.setDataFormat((short) 5);

    int maxColumn = -1;
    Map<String, HSSFCellStyle> datFormats = new HashMap<>();
    for (int rowInd = 1; rowInd < rowCount; rowInd++) {
        HSSFRow row = sheet.getRow(rowInd);
        int colCount = row.getPhysicalNumberOfCells();
        if (maxColumn < colCount) {
            maxColumn = colCount;
        }
        for (int cellInd = 0; cellInd < colCount; cellInd++) {
            HSSFCell cell = row.getCell(cellInd);

            String strVal = cell.getStringCellValue();

            if (strVal.startsWith("$")) {
                //do nothing
            } else {
                if (strVal.startsWith("'")) {
                    strVal = strVal.substring(1);
                }
                if (PlatformUtils.isDouble(strVal)) {
                    cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
                    cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                    if (PlatformUtils.isInteger(strVal)) {
                        int intVal = Integer.valueOf(strVal.trim());
                        cell.setCellStyle(intStyle);
                        cell.setCellValue(intVal);
                    } else if (PlatformUtils.isDouble(strVal)) {
                        double dblVal = Double.valueOf(strVal.trim());
                        cell.setCellStyle(decStyle);
                        cell.setCellValue(dblVal);
                    }
                } else {
                    boolean isDate = false;
                    for (String dteFormat : new String[] { "yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss",
                            "yyyy-MM-dd HH:mm", "yyyy-MM-dd", "HH:mm" }) {
                        if (PlatformUtils.isDate(strVal, dteFormat)) {
                            HSSFCellStyle dateStyle = datFormats.get(dteFormat.trim());
                            if (dateStyle == null) {
                                dateStyle = book.createCellStyle();
                                dateStyle.setDataFormat(creationHelper.createDataFormat().getFormat(dteFormat));
                                datFormats.put(dteFormat, dateStyle);
                            }
                            cell.setCellStyle(dateStyle);
                            try {
                                cell.setCellValue(new SimpleDateFormat(dteFormat).parse(strVal));
                            } catch (ParseException e) {
                                //
                            }
                            isDate = true;
                            break;
                        }
                    }

                }
            }
        }
    }
    if (maxColumn >= 0) {
        for (int cellInd = 0; cellInd < maxColumn; cellInd++) {
            sheet.autoSizeColumn(cellInd);
        }
    }

}

From source file:org.adempiere.impexp.AbstractExcelExporter.java

License:Open Source License

private HSSFCellStyle getStyle(int row, int col) {
    int displayType = getDisplayType(row, col);
    String key = "cell-" + col + "-" + displayType;
    HSSFCellStyle cs = m_styles.get(key);
    if (cs == null) {
        boolean isHighlightNegativeNumbers = true;
        cs = m_workbook.createCellStyle();
        HSSFFont font = getFont(false);/*from w w w . java  2 s . c o m*/
        cs.setFont(font);
        // Border
        cs.setBorderLeft((short) 1);
        cs.setBorderTop((short) 1);
        cs.setBorderRight((short) 1);
        cs.setBorderBottom((short) 1);
        //
        if (DisplayType.isDate(displayType)) {
            cs.setDataFormat(m_dataFormat.getFormat("DD.MM.YYYY"));
        } else if (DisplayType.isNumeric(displayType)) {
            DecimalFormat df = DisplayType.getNumberFormat(displayType, getLanguage());
            String format = getFormatString(df, isHighlightNegativeNumbers);
            cs.setDataFormat(m_dataFormat.getFormat(format));
        }
        m_styles.put(key, cs);
    }
    return cs;
}

From source file:org.adempiere.impexp.AbstractExcelExporter.java

License:Open Source License

private HSSFCellStyle getHeaderStyle(int col) {
    String key = "header-" + col;
    HSSFCellStyle cs_header = m_styles.get(key);
    if (cs_header == null) {
        HSSFFont font_header = getFont(true);
        cs_header = m_workbook.createCellStyle();
        cs_header.setFont(font_header);/*from  w  w w .  j  a v a 2  s.c o  m*/
        cs_header.setBorderLeft((short) 2);
        cs_header.setBorderTop((short) 2);
        cs_header.setBorderRight((short) 2);
        cs_header.setBorderBottom((short) 2);
        cs_header.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
        cs_header.setWrapText(true);
        m_styles.put(key, cs_header);
    }
    return cs_header;
}

From source file:org.anyframe.logmanager.web.LogManagerController.java

License:Apache License

/**
 * log data export for excel file type/* w w w  .  j  av  a 2  s . c o m*/
 * 
 * @param searchCondition
 * @param model
 * @param request
 * @return
 * @throws Exception
 */
@RequestMapping(params = "method=xlsExport")
public void xlsExport(LogSearchCondition searchCondition, Model model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    searchCondition.setPageIndex(-1);
    searchCondition.setCollection(searchCondition.getRepositoryName());

    String fileName = null;
    String sDate = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", new Locale("ko_KR"));
    sDate = sdf.format(new Date());
    StringBuffer sb = new StringBuffer();
    sb.append(searchCondition.getAppName().substring(searchCondition.getAppName().lastIndexOf("/") + 1));
    sb.append("_").append(searchCondition.getCollection()).append("_").append(sDate).append(".xls");
    fileName = sb.toString();

    SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-ddHHmm");
    logger.debug("from:{}",
            searchCondition.getFromDate() + searchCondition.getFromHour() + searchCondition.getFromMinute());
    logger.debug("to:{}",
            searchCondition.getToDate() + searchCondition.getToHour() + searchCondition.getToMinute());
    if (searchCondition.isUseFromDate())
        searchCondition.setFromDateTime(dateTimeFormat.parse(searchCondition.getFromDate()
                + searchCondition.getFromHour() + searchCondition.getFromMinute()));
    if (searchCondition.isUseToDate())
        searchCondition.setToDateTime(dateTimeFormat.parse(
                searchCondition.getToDate() + searchCondition.getToHour() + searchCondition.getToMinute()));

    List<LogDataMap> resultList = service.searchAnalysisLog(searchCondition);

    response.reset();
    response.setContentType("application/x-msexcel;charset=MS949");
    // response.setContentType("application/octet-stream");
    String userAgent = request.getHeader("User-Agent");

    if (userAgent.indexOf("MSIE 5.5") > -1) {
        response.setHeader("Content-Disposition", "filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\";");
    } else if (userAgent.indexOf("MSIE") > -1) {
        response.setHeader("Content-Disposition",
                "attachment; filename=\"" + java.net.URLEncoder.encode(fileName, "UTF-8") + "\";");
    } else {
        response.setHeader("Content-Disposition",
                "attachment; filename=\"" + new String(fileName.getBytes("euc-kr"), "latin1") + "\";");
    }
    response.setHeader("Content-Description", "JSP Generated Data");
    response.setHeader("Content-Transfer-Encoding", "binary;");
    response.setHeader("Pragma", "no-cache;");
    response.setHeader("Expires", "-1;");

    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet(fileName);

    OutputStream fileOut = null;
    try {
        fileOut = response.getOutputStream();
        HSSFRow row = null;
        HSSFRow headerRow = null;

        HSSFDataFormat df = workbook.createDataFormat();

        HSSFCellStyle headerStyle = workbook.createCellStyle();
        HSSFFont boldFont = workbook.createFont();
        boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        headerStyle.setFont(boldFont);
        headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFCellStyle dateStyle = workbook.createCellStyle();
        dateStyle.setDataFormat(df.getFormat("yyyy-mm-dd h:mm:ss.000"));
        dateStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        dateStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFCellStyle messageStyle = workbook.createCellStyle();
        messageStyle.setWrapText(true);
        messageStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        messageStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFCell cell;
        HSSFCell headerCell;

        short width = 265;

        Iterator<String> j = null;
        String key = null;
        int cellIndex = 0;
        int listSize = 0;

        String level = null;
        Date timestamp = null;
        String message = null;

        if (resultList != null) {
            listSize = resultList.size();
            for (int i = 0; i < listSize; i++) {
                LogDataMap log = (LogDataMap) resultList.get(i);
                if (i == 0) {
                    headerRow = sheet.createRow(i); // level header
                    sheet.setColumnWidth(0, 7 * width);
                    headerCell = headerRow.createCell(0);
                    HSSFRichTextString headerValue = new HSSFRichTextString("level");
                    headerCell.setCellValue(headerValue);
                    headerCell.setCellStyle(headerStyle);

                    headerCell = headerRow.createCell(1); // time stamp header
                    sheet.setColumnWidth(1, 24 * width);
                    headerValue = new HSSFRichTextString("timestamp");
                    headerCell.setCellValue(headerValue);
                    headerCell.setCellStyle(headerStyle);

                    headerCell = headerRow.createCell(2); // message header
                    sheet.setColumnWidth(2, 70 * width);
                    headerValue = new HSSFRichTextString("message");
                    headerCell.setCellValue(headerValue);
                    headerCell.setCellStyle(headerStyle);
                }

                row = sheet.createRow(i + 1);

                // level
                level = (String) log.get("level");
                cell = row.createCell(0);
                cell.setCellStyle(style);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(level);

                // timestamp
                timestamp = (Date) log.get("timestamp");
                cell = row.createCell(1);
                cell.setCellStyle(dateStyle);
                cell.setCellValue(timestamp);

                // message
                message = (String) log.get("message");
                HSSFRichTextString messageValue = new HSSFRichTextString(message);
                cell = row.createCell(2);
                cell.setCellStyle(messageStyle);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(messageValue);

                cellIndex = 3;
                j = log.keySet().iterator();
                while (j.hasNext()) {
                    key = j.next();
                    if ("_id".equals(key) || "message".equals(key) || "timestamp".equals(key)
                            || "level".equals(key)) {
                        continue;
                    }
                    //logger.debug("key=" + key);
                    if (i == 0) {
                        sheet.setColumnWidth(cellIndex, 20 * width);

                        headerCell = headerRow.createCell(cellIndex);
                        HSSFRichTextString headerValue = new HSSFRichTextString(key);
                        headerCell.setCellValue(headerValue);
                        headerCell.setCellStyle(headerStyle);
                    }
                    cell = row.createCell(cellIndex);
                    Object value = log.get(key);
                    if (value instanceof Date) {
                        cell.setCellStyle(dateStyle);
                        cell.setCellValue((Date) value);
                    } else {
                        cell.setCellStyle(style);
                        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                        cell.setCellValue((String) log.get(key));
                    }

                    cellIndex++;
                }
            }
            workbook.write(fileOut);
        }
    } catch (Exception e) {
        throw e;
    } finally {
        try {
            if (fileOut != null) {
                fileOut.flush();
                fileOut.close();
            }
        } catch (IOException ex) {
            logger.warn(ex.getMessage(), ex);
        }
    }
}

From source file:org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements.EPStyle.java

License:Apache License

/**
 * Override of Initialize() implementation
 * @param attributes the array of Attribute instances; may be empty, will
 *                  never be null//  ww w .j ava 2 s.  c o  m
 * @param parent the parent ElementProcessor; may be null
 * @exception IOException if anything is wrong
 */
public void initialize(final Attribute[] attributes, final ElementProcessor parent) throws IOException {
    super.initialize(attributes, parent);

    EPStyleRegion sregion = (EPStyleRegion) parent;

    if (sregion.isValid()) {
        Hashtable colorhash = sregion.getColorHash();

        HSSFCellStyle style = sregion.getStyle();
        short cnvhalign = convertAlignment(getHorizontalAlignment().getCode());
        style.setAlignment(cnvhalign);
        short cnvvalign = convertVAlignment(getVerticalAlignment().getCode());
        style.setVerticalAlignment(cnvvalign);
        style.setFillPattern((short) getShade());

        Workbook workbook = getWorkbook();
        HSSFDataFormat dataformat = workbook.createDataFormat();
        if (getShade() == 1) {
            // TODO: change to constant when upgrade to new HSSF
            // solid w/foreground, bg doesn't matter
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("shade = 1");
            }
            HSSFColor color = (HSSFColor) colorhash.get(getBackgroundColor().toString());
            if (color == null) {
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("s1 BG couldn't find color for " + getBackgroundColor().toString());
                }
                color = new HSSFColor.WHITE();
            }
            style.setFillForegroundColor(color.getIndex());
            color = (HSSFColor) colorhash.get(getPatternColor().toString());
            if (color == null) {
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("s1 PC couldn't find color for " + getPatternColor().toString());
                }
                color = new HSSFColor.BLACK();
            }
            style.setFillBackgroundColor(color.getIndex());
        } else {
            HSSFColor color = (HSSFColor) colorhash.get(getBackgroundColor().toString());
            if (color == null) {
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("BG couldn't find color for " + getBackgroundColor().toString());
                }
                color = new HSSFColor.BLACK();
            }
            style.setFillBackgroundColor(color.getIndex());
            color = (HSSFColor) colorhash.get(getPatternColor().toString());
            if (color == null) {
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("PC couldn't find color for " + getPatternColor().toString());
                }
                color = new HSSFColor.WHITE();
            }
            style.setFillForegroundColor(color.getIndex());
        }
        style.setWrapText(getWrapText());
        style.setLocked(true);

        String format = null;
        try {
            format = getFormat();
        } catch (NullPointerException e) {
            format = _general_format;
        }

        if (!_general_format.equals(format)) {
            short valuenumber;
            format = kludgeForGnumericMisformats(format);
            format = kludgeForGnumericDateDivergence(format);
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("setting format to " + format);
            }
            Object o = workbook.getValidate(format, dataformat.getFormat(format));
            Short sh = null;
            sh = (Short) o;
            valuenumber = sh.shortValue();
            style.setDataFormat(valuenumber);
        }
    } else {
        invalid = true;
    }
}

From source file:org.beangle.commons.transfer.excel.PoiTest.java

License:Open Source License

public static void main(String[] args) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");

    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow(0);/*  w  ww  .  ja  v a  2s  .  com*/

    // Create a cell and put a date value in it. The first cell is not
    // styled as a date.
    HSSFCell cell = row.createCell(0);
    cell.setCellValue(new Date());

    // we style the second cell as a date (and time). It is important to
    // create a new cell style from the workbook
    // otherwise you can end up modifying the built in style and effecting
    // not only this cell but other cells.
    HSSFCellStyle cellStyle = wb.createCellStyle();
    DataFormat df = wb.createDataFormat();
    // cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
    cellStyle.setDataFormat(df.getFormat("YYYY-MM-DD HH:MM:SS"));
    cell = row.createCell(1);
    cell.setCellValue(new Date());
    cell.setCellStyle(cellStyle);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("src/test/resources/workbook.xls");
    wb.write(fileOut);
    fileOut.close();
}

From source file:org.displaytag.export.excel.ExcelUtils.java

License:Open Source License

/**
 * We cache the styles; they are expensive to construct.
 * @param properties props for this run// w  w w .  ja  v a 2s.c  om
 */
public void initCellStyles(TableProperties properties) {
    // Integer
    HSSFCellStyle style = getNewCellStyle();
    style.setAlignment(CellStyle.ALIGN_RIGHT);
    style.setDataFormat(
            HSSFDataFormat.getBuiltinFormat(properties.getProperty(ExcelUtils.EXCEL_FORMAT_INTEGER)));
    this.cellStyles.put(STYLE_INTEGER, style);

    // NUMBER
    style = getNewCellStyle();
    style.setAlignment(CellStyle.ALIGN_RIGHT);
    style.setDataFormat(
            HSSFDataFormat.getBuiltinFormat(properties.getProperty(ExcelUtils.EXCEL_FORMAT_NUMBER)));
    this.cellStyles.put(STYLE_NUMBER, style);

    // style = HSSFDataFormat.getBuiltinFormat("0.00%");

    // Date
    style = getNewCellStyle();
    style.setAlignment(CellStyle.ALIGN_RIGHT);
    style.setDataFormat(HSSFDataFormat.getBuiltinFormat(properties.getProperty(ExcelUtils.EXCEL_FORMAT_DATE)));
    style.setAlignment(CellStyle.ALIGN_RIGHT);
    this.cellStyles.put(STYLE_DATE, style);

    // Long text
    style = getNewCellStyle(); // http://jakarta.apache.org/poi/hssf/quick-guide.html#NewLinesInCells
    style.setWrapText(true);
    this.cellStyles.put(STYLE_LONGSTRING, style);

    // Regular text
    this.cellStyles.put(STYLE_STRING, getNewCellStyle());

    this.wrapAt = Integer.valueOf(properties.getProperty(ExcelUtils.EXCEL_WRAPAT));
}

From source file:org.eclipse.scada.ae.ui.views.export.excel.impl.ExportEventsImpl.java

License:Open Source License

private IStatus storeExcel(final File file, final List<Event> events, final List<Field> columns,
        final IProgressMonitor monitor) throws IOException {
    final HSSFWorkbook workbook = new HSSFWorkbook();

    final HSSFDataFormat dateFormat = workbook.createDataFormat();
    final HSSFCellStyle dateCellStyle = workbook.createCellStyle();
    dateCellStyle.setDataFormat(dateFormat.getFormat("YYYY-MM-DD hh:mm:ss.000"));

    try {//from   w  w w .  j  a  v  a2  s . c om
        monitor.beginTask(Messages.ExportImpl_Progress_ExportingEvents, events.size() + 3 + columns.size());

        try {
            monitor.subTask(Messages.ExportImpl_Progress_CreateWorkbook);
            monitor.worked(1);

            final HSSFSheet sheet = createSheet(events, workbook, columns);
            monitor.worked(1);

            monitor.setTaskName(Messages.ExportImpl_Progress_ExportEvents);

            for (int i = 0; i < events.size(); i++) {
                final HSSFRow row = sheet.createRow(i + 1);

                final Event e = events.get(i);
                for (int j = 0; j < columns.size(); j++) {
                    final Field field = columns.get(j);
                    final ExcelCell cell = new ExcelCell(row, j, dateCellStyle);
                    field.render(e, cell);
                }
                monitor.worked(1);
                if (monitor.isCanceled()) {
                    return Status.CANCEL_STATUS;
                }
            }

            sheet.setRepeatingRows(new CellRangeAddress(0, 1, -1, -1));

            monitor.setTaskName("Auto sizing");
            for (int i = 0; i < columns.size(); i++) {
                monitor.subTask(String.format("Auto sizing column: %s", columns.get(i).getHeader()));
                sheet.autoSizeColumn(i);
                monitor.worked(1);

                if (monitor.isCanceled()) {
                    return Status.CANCEL_STATUS;
                }
            }

        } finally {
            monitor.subTask(Messages.ExportImpl_Progress_CloseFile);
            if (workbook != null) {
                makeDocInfo(workbook);

                final FileOutputStream stream = new FileOutputStream(file);
                workbook.write(stream);
                stream.close();
            }
            monitor.worked(1);
        }
    } finally {
        monitor.done();
    }

    return Status.OK_STATUS;
}

From source file:org.extremecomponents.table.view.ExtendXlsView.java

License:Apache License

private Map initStyles(HSSFWorkbook wb, short fontHeight) {
    Map result = new HashMap();
    HSSFCellStyle titleStyle = wb.createCellStyle();
    HSSFCellStyle textStyle = wb.createCellStyle();
    HSSFCellStyle boldStyle = wb.createCellStyle();
    HSSFCellStyle numericStyle = wb.createCellStyle();
    HSSFCellStyle numericStyleBold = wb.createCellStyle();
    HSSFCellStyle moneyStyle = wb.createCellStyle();
    HSSFCellStyle moneyStyleBold = wb.createCellStyle();
    HSSFCellStyle percentStyle = wb.createCellStyle();
    HSSFCellStyle percentStyleBold = wb.createCellStyle();

    result.put("titleStyle", titleStyle);
    result.put("textStyle", textStyle);
    result.put("boldStyle", boldStyle);
    result.put("numericStyle", numericStyle);
    result.put("numericStyleBold", numericStyleBold);
    result.put("moneyStyle", moneyStyle);
    result.put("moneyStyleBold", moneyStyleBold);
    result.put("percentStyle", percentStyle);
    result.put("percentStyleBold", percentStyleBold);

    HSSFDataFormat format = wb.createDataFormat();

    // Global fonts
    HSSFFont font = wb.createFont();/*from   w  w  w .jav  a 2  s  .co m*/
    font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
    font.setColor(HSSFColor.BLACK.index);
    font.setFontName(HSSFFont.FONT_ARIAL);
    font.setFontHeightInPoints(fontHeight);

    HSSFFont fontBold = wb.createFont();
    fontBold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    fontBold.setColor(HSSFColor.BLACK.index);
    fontBold.setFontName(HSSFFont.FONT_ARIAL);
    fontBold.setFontHeightInPoints(fontHeight);

    // Money Style
    moneyStyle.setFont(font);
    moneyStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
    moneyStyle.setDataFormat(format.getFormat(moneyFormat));

    // Money Style Bold
    moneyStyleBold.setFont(fontBold);
    moneyStyleBold.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
    moneyStyleBold.setDataFormat(format.getFormat(moneyFormat));

    // Percent Style
    percentStyle.setFont(font);
    percentStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
    percentStyle.setDataFormat(format.getFormat(percentFormat));

    // Percent Style Bold
    percentStyleBold.setFont(fontBold);
    percentStyleBold.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
    percentStyleBold.setDataFormat(format.getFormat(percentFormat));

    // Standard Numeric Style
    numericStyle.setFont(font);
    numericStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);

    // Standard Numeric Style Bold
    numericStyleBold.setFont(fontBold);
    numericStyleBold.setAlignment(HSSFCellStyle.ALIGN_RIGHT);

    // Title Style
    titleStyle.setFont(font);
    titleStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    titleStyle.setBottomBorderColor(HSSFColor.BLACK.index);
    titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    titleStyle.setLeftBorderColor(HSSFColor.BLACK.index);
    titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
    titleStyle.setRightBorderColor(HSSFColor.BLACK.index);
    titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
    titleStyle.setTopBorderColor(HSSFColor.BLACK.index);
    titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

    // Standard Text Style
    textStyle.setFont(font);
    textStyle.setWrapText(true);

    // Standard Text Style
    boldStyle.setFont(fontBold);
    boldStyle.setWrapText(true);

    return result;
}