Example usage for org.apache.poi.hssf.usermodel HSSFWorkbook createFont

List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook createFont

Introduction

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

Prototype


@Override
public HSSFFont createFont() 

Source Link

Document

create a new Font and add it to the workbook's font table

Usage

From source file:org.ofbiz.webtools.ExcelConversionFilter.java

License:Open Source License

void applyStylesToSheet(Map<String, ? extends Object> context, HSSFWorkbook workBook, HSSFSheet sheet,
        ServletRequest request) {//  ww w  .j  ava2s  .c  o  m
    List headerKeys = new ArrayList();
    int noOfheads = 0;
    headerKeys = UtilMisc.toList("mainHeader1", "mainHeader2", "mainHeader3", "mainHeader4", "mainHeader5");
    Map<String, Object> stylesMap = FastMap.newInstance();
    stylesMap = (Map) context.get("stylesMap");
    ArrayList allRowAndColData = (ArrayList) context.get("allRowAndColData");
    Integer mainHeadingCell = 5;
    Integer mainHeadercellHeight = null;
    String mainHeaderFontName = null;
    Integer mainHeaderFontSize = null;
    Boolean mainHeaderBold = true;
    Integer columnHeaderCellHeight = null;
    Boolean columnHeaderBold = true;
    Boolean columnHeaderBgColor = null;
    String columnHeaderFontName = null;
    Boolean autoSizeCell = true;
    Integer columnHeaderFontSize = null;
    mainHeadercellHeight = (Integer) stylesMap.get("mainHeadercellHeight");
    mainHeaderFontName = (String) stylesMap.get("mainHeaderFontName");
    mainHeaderFontSize = (Integer) stylesMap.get("mainHeaderFontSize");
    if (stylesMap.get("mainHeaderBold") != null) {
        mainHeaderBold = (Boolean) stylesMap.get("mainHeaderBold");
    }
    if (stylesMap.get("columnHeaderBold") != null) {
        columnHeaderBold = (Boolean) stylesMap.get("columnHeaderBold");
    }
    if (stylesMap.get("autoSizeCell") != null) {
        autoSizeCell = (Boolean) stylesMap.get("autoSizeCell");
    }
    if (stylesMap.get("mainHeadingCell") != null) {
        mainHeadingCell = (Integer) stylesMap.get("mainHeadingCell");
    }
    columnHeaderCellHeight = (Integer) stylesMap.get("columnHeaderCellHeight");
    columnHeaderBgColor = (Boolean) stylesMap.get("columnHeaderBgColor");
    columnHeaderFontName = (String) stylesMap.get("columnHeaderFontName");
    columnHeaderFontSize = (Integer) stylesMap.get("columnHeaderFontSize");
    ArrayList styles = new ArrayList(stylesMap.keySet());
    for (int i = 0; i < styles.size(); i++) {
        ArrayList tempArrayList = new ArrayList<String>();
        if (headerKeys.contains(styles.get(i))) {
            ArrayList<?> innerData = (ArrayList<?>) allRowAndColData.get(i);
            tempArrayList.add(stylesMap.get(styles.get(i)));
            for (int j = 0; j < innerData.size() - 1; j++) {
                tempArrayList.add("");
            }
            allRowAndColData.add(i, tempArrayList);
            ++noOfheads;
        }
    }
    try {
        for (int i = 0; i < allRowAndColData.size(); i++) {
            HSSFCellStyle style = workBook.createCellStyle();
            HSSFFont font = workBook.createFont();
            ArrayList<?> ardata = (ArrayList<?>) allRowAndColData.get(i);
            HSSFRow row = sheet.createRow(i);
            for (int k = 0; k < ardata.size(); k++) {
                HSSFCell cell = row.createCell(k);
                if (k == mainHeadingCell && i <= noOfheads) {
                    if (UtilValidate.isNotEmpty(mainHeadercellHeight)) {
                        row.setHeight((short) mainHeadercellHeight.shortValue());
                    } else {
                        row.setHeight((short) 400);
                    }
                    if (UtilValidate.isNotEmpty(mainHeaderFontName)) {
                        font.setFontName(mainHeaderFontName);
                    }
                    if (UtilValidate.isNotEmpty(mainHeaderBold) && mainHeaderBold) {
                        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                    }
                    if (UtilValidate.isNotEmpty(mainHeaderFontSize)) {
                        font.setFontHeightInPoints((short) mainHeaderFontSize.shortValue());
                    } else {
                        font.setFontHeightInPoints((short) 12); //default value 
                    }
                    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                    style.setFont(font);
                    cell.setCellValue((ardata.get(0).toString()).replaceAll("\"", ""));
                    cell.setCellStyle(style);
                } else if (i == noOfheads + 1) {
                    if (UtilValidate.isNotEmpty(columnHeaderCellHeight)) {
                        row.setHeight((short) columnHeaderCellHeight.shortValue());
                    } else {
                        row.setHeight((short) 300);
                    }
                    if (UtilValidate.isNotEmpty(columnHeaderBold) && columnHeaderBold) {
                        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                    }
                    if (UtilValidate.isNotEmpty(columnHeaderFontSize)) {
                        font.setFontHeightInPoints((short) columnHeaderFontSize.shortValue());
                    } else {
                        font.setFontHeightInPoints((short) 9);
                    }
                    if (UtilValidate.isNotEmpty(columnHeaderFontName)) {
                        font.setFontName(columnHeaderFontName);
                    }
                    if (UtilValidate.isNotEmpty(columnHeaderBgColor) && columnHeaderBgColor) {
                        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                    }
                    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                    style.setFont(font);
                    cell.setCellValue((ardata.get(k).toString()).replaceAll("\"", ""));
                    cell.setCellStyle(style);
                } else if (i > noOfheads) {
                    cell.setCellValue((ardata.get(k).toString()).replaceAll("\"", ""));
                }
                if (UtilValidate.isNotEmpty(autoSizeCell) && autoSizeCell) {
                    sheet.autoSizeColumn(k);
                }
            }
        }
    } catch (Exception e) {
        Debug.logInfo(e.getMessage(), module);
        request.setAttribute("_ERROR_MESSAGE_", e.getMessage());

    }
}

From source file:org.openelis.bean.DataViewBean.java

License:Open Source License

private CellStyle createStyle(HSSFWorkbook wb) {
    CellStyle headerStyle;/*from  www.jav a2 s.  co m*/
    Font font;

    font = wb.createFont();
    font.setColor(IndexedColors.WHITE.getIndex());
    headerStyle = wb.createCellStyle();
    headerStyle.setAlignment(CellStyle.ALIGN_LEFT);
    headerStyle.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);
    headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    headerStyle.setFillForegroundColor(IndexedColors.GREY_80_PERCENT.getIndex());
    headerStyle.setFont(font);

    return headerStyle;
}

From source file:org.openelis.bean.QcChartReport1Bean.java

License:Open Source License

private HSSFWorkbook fillWorkbook(HSSFWorkbook wb, ArrayList<Value> values, String qcName, String qcType,
        String qcFormat, Integer plotType, ReportStatus status) throws Exception {
    int rowIndex, sheetIndex, valueIndex;
    HSSFSheet sheet;/*from   w w  w.java2s  .  co  m*/
    Row row;
    String lastAnalyte, lastFormat;

    lastAnalyte = "___";
    lastFormat = "___";
    sheet = null;
    sheetIndex = 1;
    valueIndex = 0;

    try {
        baseFont = wb.createFont();
        baseFont.setFontName("Arial");
        baseFont.setFontHeightInPoints((short) 8);
        baseStyle = wb.createCellStyle();
        baseStyle.setFont(baseFont);

        headerFont = wb.createFont();
        headerFont.setColor(IndexedColors.WHITE.getIndex());
        headerFont.setFontName("Arial");
        headerFont.setFontHeightInPoints((short) 8);
        headerStyle = wb.createCellStyle();
        headerStyle.setAlignment(CellStyle.ALIGN_LEFT);
        headerStyle.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);
        headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        headerStyle.setFillForegroundColor(IndexedColors.GREY_80_PERCENT.getIndex());
        headerStyle.setFont(headerFont);

        maxChars = new ArrayList<Integer>();
        worksheetColumnMap = new HashMap<String, HashMap<String, Integer>>();
        worksheetHeaders = new ArrayList<String>();
        worksheetHeaderNames = new ArrayList<String>();

        if (qcColumns != null && !qcColumns.isEmpty())
            rowIndex = 32;
        else
            rowIndex = 3;

        for (Value value : values) {
            valueIndex++;

            if ("N".equals(value.getIsPlot()))
                continue;

            if (!lastAnalyte.equals(value.getAnalyteName())) {
                if (!"___".equals(lastAnalyte)) {
                    while (rowIndex < sheet.getLastRowNum()) {
                        sheet.removeRow(sheet.getRow(rowIndex));
                        rowIndex++;
                    }
                    finishSheet(sheet, wb, qcName, qcType, lastAnalyte);
                }
                sheet = wb.getSheet("Sheet" + (sheetIndex++));
                if (sheet == null)
                    sheet = wb.createSheet();
                lastAnalyte = value.getAnalyteName();
                if (qcColumns != null && !qcColumns.isEmpty())
                    rowIndex = 32;
                else
                    rowIndex = 3;
                lastFormat = "___";

                if (Constants.dictionary().CHART_TYPE_FIXED.equals(plotType))
                    setStatisticCells(wb, sheet, value);
            }

            if (!lastFormat.equals(value.getWorksheetFormat())) {
                lastFormat = value.getWorksheetFormat();
                if (qcColumns == null || qcColumns.isEmpty())
                    loadWorksheetFormat(lastFormat);
            }

            row = sheet.createRow(rowIndex++);
            setBaseCells(value, row);
            setResultCells(value, row, qcFormat, lastFormat);

            status.setPercentComplete(70 * (valueIndex / values.size()) + 20);
            session.setAttribute("qcChartReport", status);
        }

        finishSheet(sheet, wb, qcName, qcType, lastAnalyte);

        while (sheetIndex < wb.getNumberOfSheets())
            wb.removeSheetAt(sheetIndex);
    } finally {
        baseFont = null;
        baseStyle = null;
        headerFont = null;
        headerStyle = null;
        maxChars = null;
        worksheetColumnMap = null;
        worksheetHeaders = null;
        worksheetHeaderNames = null;
    }

    return wb;
}

From source file:org.openelis.bean.WorksheetExcelHelperBean.java

License:Open Source License

private void createStyles(HSSFWorkbook wb) {
    CellStyle dateTimeEditStyle, dateTimeNoEditStyle, headerStyle, rowEditStyle, rowNoEditStyle;
    CreationHelper helper;/*from  ww w.  ja  va2 s .  c  o  m*/
    Font font;

    helper = wb.getCreationHelper();
    styles = new HashMap<String, CellStyle>();

    font = wb.createFont();
    font.setColor(IndexedColors.WHITE.getIndex());
    headerStyle = wb.createCellStyle();
    headerStyle.setAlignment(CellStyle.ALIGN_LEFT);
    headerStyle.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);
    headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    headerStyle.setFillForegroundColor(IndexedColors.GREY_80_PERCENT.getIndex());
    headerStyle.setFont(font);
    headerStyle.setLocked(true);
    styles.put("header", headerStyle);

    rowEditStyle = wb.createCellStyle();
    rowEditStyle.setAlignment(CellStyle.ALIGN_LEFT);
    rowEditStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    rowEditStyle.setLocked(false);
    styles.put("row_edit", rowEditStyle);

    rowNoEditStyle = wb.createCellStyle();
    rowNoEditStyle.setAlignment(CellStyle.ALIGN_LEFT);
    rowNoEditStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    rowNoEditStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
    rowNoEditStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    rowNoEditStyle.setLocked(true);
    styles.put("row_no_edit", rowNoEditStyle);

    dateTimeEditStyle = wb.createCellStyle();
    dateTimeEditStyle.setDataFormat(helper.createDataFormat().getFormat("yyyy-MM-dd hh:mm"));
    dateTimeEditStyle.setAlignment(CellStyle.ALIGN_LEFT);
    dateTimeEditStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    dateTimeEditStyle.setLocked(false);
    styles.put("datetime_edit", dateTimeEditStyle);

    dateTimeNoEditStyle = wb.createCellStyle();
    dateTimeNoEditStyle.setDataFormat(helper.createDataFormat().getFormat("yyyy-MM-dd hh:mm"));
    dateTimeNoEditStyle.setAlignment(CellStyle.ALIGN_LEFT);
    dateTimeNoEditStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    dateTimeNoEditStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
    dateTimeNoEditStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    dateTimeNoEditStyle.setLocked(true);
    styles.put("datetime_no_edit", dateTimeNoEditStyle);
}

From source file:org.openmrs.module.tracnetreporting.impl.TracNetIndicatorServiceImpl.java

License:Open Source License

/**
 * Exports data to the Excel File//from www.j  av a2s  .co  m
 * 
 * @throws IOException
 * @see org.openmrs.module.tracnetreporting.service.TracNetIndicatorService#exportDataToExcelFile(java.util.Map)
 */
@SuppressWarnings({ "deprecation", "unchecked" })
public void exportDataToExcelFile(HttpServletRequest request, HttpServletResponse response,
        Map<String, Integer> indicatorsList, String filename, String title, String startDate, String endDate)
        throws IOException {

    HSSFWorkbook workbook = new HSSFWorkbook();
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    HSSFSheet sheet = workbook.createSheet(title);
    int count = 0;
    sheet.setDisplayRowColHeadings(true);

    // Setting Style
    HSSFFont font = workbook.createFont();
    HSSFCellStyle cellStyle = workbook.createCellStyle();
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    font.setColor(HSSFFont.COLOR_RED);
    cellStyle.setFillForegroundColor((short) 0xA);

    // Title
    HSSFRow row = sheet.createRow((short) 0);
    HSSFCell cell = row.createCell((short) 0);
    cell.setCellValue("");
    row.setRowStyle(cellStyle);
    row.createCell((short) 1).setCellValue("" + title + "(Between " + startDate + " and " + endDate + ")");

    // Headers
    row = sheet.createRow((short) 2);
    row.createCell((short) 0).setCellValue("#");
    row.createCell((short) 1).setCellValue("INDICATOR NAME");
    row.createCell((short) 2).setCellValue("INDICATOR");

    Log log = LogFactory.getLog(this.getClass());
    log.info("00000000000000000000000000000000000000000000000000000000000000000");
    // log.info();

    List<String> indicator_msg = null;

    indicator_msg = (ArrayList<String>) request.getSession().getAttribute(request.getParameter("id") + "_msg");

    for (String indicator : indicatorsList.keySet()) {
        count++;
        row = sheet.createRow((short) count + 3);
        row.createCell((short) 0).setCellValue(indicator.toString().substring(4));
        row.createCell((short) 1).setCellValue(indicator_msg.get(count - 1));// .substring(3)
        row.createCell((short) 2).setCellValue(indicatorsList.get(indicator).toString());
        // log.info("========================>>> "+count);
    }
    OutputStream outputStream = response.getOutputStream();
    workbook.write(outputStream);
    outputStream.flush();
    outputStream.close();
}

From source file:org.openmrs.module.tracnetreporting.impl.TracNetIndicatorServiceImpl.java

License:Open Source License

/**
 * Exports data to the Excel File//from   www  . ja va 2s. co m
 * 
 * @throws IOException
 * @see org.openmrs.module.tracnetreporting.service.TracNetIndicatorService#exportDataToExcelFile(java.util.Map)
 */

@SuppressWarnings("deprecation")
@Override
public void exportPatientsListToExcelFile(HttpServletRequest request, HttpServletResponse response,
        List<Person> patientsList, String filename, String title, String startDate, String endDate)
        throws IOException {

    log.info("exporttttttttttttttttttttttttttttttttttttt" + patientsList);

    Session session = getSessionFactory().getCurrentSession();
    HSSFWorkbook workbook = new HSSFWorkbook();
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    HSSFSheet sheet = workbook.createSheet(title);
    int count = 0;
    sheet.setDisplayRowColHeadings(true);

    // Setting Style
    HSSFFont font = workbook.createFont();
    HSSFCellStyle cellStyle = workbook.createCellStyle();
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    font.setColor(HSSFFont.COLOR_RED);
    cellStyle.setFillForegroundColor((short) 0xA);

    // Title
    HSSFRow row = sheet.createRow((short) 0);
    HSSFCell cell = row.createCell((short) 0);
    cell.setCellValue("");
    row.setRowStyle(cellStyle);
    row.createCell((short) 1).setCellValue("" + title + "(Between " + startDate + " and " + endDate + ")");

    // Headers
    row = sheet.createRow((short) 2);
    row.createCell((short) 0).setCellValue("#");
    row.createCell((short) 1).setCellValue("Patient ID");
    row.createCell((short) 2).setCellValue("Patient Names");
    row.createCell((short) 3).setCellValue("Gender");
    row.createCell((short) 4).setCellValue("Last Encounter Date");
    row.createCell((short) 5).setCellValue("Last Return Visit Date");

    for (Person person : patientsList) {

        // Getting some stuff for Last Encounter Date and Last Return Visit
        // Date.
        Date maxEncounterDateTime = (Date) session
                .createSQLQuery("select cast(max(encounter_datetime)as DATE) from encounter where patient_id = "
                        + person.getPersonId())
                .uniqueResult();

        Date maxReturnVisitDay = (Date) session
                .createSQLQuery("select cast(max(value_datetime) as DATE ) " + "from obs where concept_id = "
                        + ConstantValues.NEXT_SCHEDULED_VISIT + " and person_id = " + person.getPersonId())
                .uniqueResult();

        count++;
        row = sheet.createRow((short) count + 3);
        row.createCell((short) 0).setCellValue(count);
        row.createCell((short) 1).setCellValue(person.getPersonId());
        row.createCell((short) 2).setCellValue(person.getFamilyName() + " " + person.getGivenName());
        row.createCell((short) 3).setCellValue(person.getGender());
        row.createCell((short) 4).setCellValue(maxEncounterDateTime.toString());
        row.createCell((short) 5).setCellValue(maxReturnVisitDay.toString());
    }
    OutputStream outputStream = response.getOutputStream();
    workbook.write(outputStream);
    outputStream.flush();
    outputStream.close();
}

From source file:org.openmrs.module.tracnetreporting.impl.TracNetPatientServiceImpl.java

License:Open Source License

/**
 * Exports data to the Excel File//  w  w  w  .  j  a v a  2 s .c o m
 * 
 * @throws IOException
 * @see org.openmrs.module.tracnetreporting.service.TracNetIndicatorService#exportDataToExcelFile(java.util.Map)
 */

@SuppressWarnings("deprecation")
public void exportDataToExcelFile(HttpServletRequest request, HttpServletResponse response,
        Map<String, Integer> indicatorsList, String filename, String title, String startDate, String endDate)
        throws IOException {

    log.info("exporttttttttttttttttttttttttttttttt" + indicatorsList);

    HSSFWorkbook workbook = new HSSFWorkbook();
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    HSSFSheet sheet = workbook.createSheet(title);
    int count = 0;
    sheet.setDisplayRowColHeadings(true);

    // Setting Style
    HSSFFont font = workbook.createFont();
    HSSFCellStyle cellStyle = workbook.createCellStyle();
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    font.setColor(HSSFFont.COLOR_RED);
    cellStyle.setFillForegroundColor((short) 0xA);

    // Title
    HSSFRow row = sheet.createRow((short) 0);
    HSSFCell cell = row.createCell((short) 0);
    cell.setCellValue("");
    row.setRowStyle(cellStyle);
    row.createCell((short) 1).setCellValue("" + title + "(Between " + startDate + " and " + endDate + ")");

    // Headers
    row = sheet.createRow((short) 2);
    row.createCell((short) 0).setCellValue("#");
    row.createCell((short) 1).setCellValue("INDICATOR NAME");
    row.createCell((short) 2).setCellValue("INDICATOR");

    // for (String indicator : indicatorsList.keySet()) {
    // count++;
    // row = sheet.createRow((short) count + 3);
    // row.createCell((short) 0).setCellValue(count);
    // row.createCell((short) 1).setCellValue(indicator.toString());
    // row.createCell((short) 2).setCellValue(
    // indicatorsList.get(indicator).toString());
    // }
    OutputStream outputStream = response.getOutputStream();
    workbook.write(outputStream);
    outputStream.flush();
    outputStream.close();
}

From source file:org.openmrs.module.tracnetreporting.utils.exports.ExportToExcelUtil.java

License:Open Source License

/**
 * Exports data to the Excel File/*ww  w  .  j a v  a2  s  .  c  o m*/
 * 
 * @throws IOException
 * @see org.openmrs.module.tracnetreporting.service.TracNetIndicatorService#exportDataToExcelFile(java.util.Map)
 */

@SuppressWarnings("deprecation")
public static void exportDataToExcelFile(HttpServletRequest request, HttpServletResponse response,
        Map<String, Integer> indicatorsList, String filename, String title, String startDate, String endDate)
        throws IOException {

    HSSFWorkbook workbook = new HSSFWorkbook();
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    HSSFSheet sheet = workbook.createSheet(title);
    int count = 0;
    sheet.setDisplayRowColHeadings(true);

    //Setting Style
    HSSFFont font = workbook.createFont();
    HSSFCellStyle cellStyle = workbook.createCellStyle();
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    font.setColor(HSSFFont.COLOR_RED);
    cellStyle.setFillForegroundColor((short) 0xA);

    // Title
    HSSFRow row = sheet.createRow((short) 0);
    HSSFCell cell = row.createCell((short) 0);
    cell.setCellValue("");
    row.setRowStyle(cellStyle);
    row.createCell((short) 1).setCellValue("" + title + "(Between " + startDate + " and " + endDate + ")");

    // Headers
    row = sheet.createRow((short) 2);
    row.createCell((short) 0).setCellValue("#");
    row.createCell((short) 1).setCellValue("INDICATOR NAME");
    row.createCell((short) 2).setCellValue("INDICATOR");

    for (String indicator : indicatorsList.keySet()) {
        count++;
        row = sheet.createRow((short) count + 3);
        row.createCell((short) 0).setCellValue(count);
        row.createCell((short) 1).setCellValue(indicator.toString());
        row.createCell((short) 2).setCellValue(indicatorsList.get(indicator).toString());
    }
    OutputStream outputStream = response.getOutputStream();
    workbook.write(outputStream);
    outputStream.flush();
    outputStream.close();
}

From source file:org.opensprout.osaf.util.ExcelUtils.java

License:Open Source License

public static HSSFCellStyle getTitleCellStyle(HSSFWorkbook wb) {
    HSSFCellStyle titleStyle = wb.createCellStyle();
    HSSFFont titleFont = wb.createFont();
    //        titleFont.setFontHeight((short)50);
    titleStyle.setFont(titleFont);//from   www.  ja va 2 s.  c o m
    return titleStyle;
}

From source file:org.openswing.swing.export.java.ExportToExcel.java

License:Open Source License

private int prepareGenericComponent(int rownum, HSSFWorkbook wb, HSSFSheet s, ExportOptions exportOptions,
        ComponentExportOptions opt) throws Throwable {
    Object[] row = null;/*from  w ww.  ja va 2 s  . co m*/
    Object obj = null;
    HSSFRow r = null;
    HSSFCell c = null;

    HSSFCellStyle csText = wb.createCellStyle();
    csText.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
    csText.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    csText.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    csText.setBorderRight(HSSFCellStyle.BORDER_THIN);
    csText.setBorderTop(HSSFCellStyle.BORDER_THIN);

    HSSFCellStyle csTitle = wb.createCellStyle();
    csTitle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
    csTitle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    csTitle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    csTitle.setBorderRight(HSSFCellStyle.BORDER_THIN);
    csTitle.setBorderTop(HSSFCellStyle.BORDER_THIN);
    HSSFFont f = wb.createFont();
    f.setBoldweight(f.BOLDWEIGHT_NORMAL);
    csTitle.setFont(f);

    HSSFCellStyle csBool = wb.createCellStyle();
    csBool.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    csBool.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    csBool.setBorderRight(HSSFCellStyle.BORDER_THIN);
    csBool.setBorderTop(HSSFCellStyle.BORDER_THIN);

    HSSFCellStyle csDecNum = wb.createCellStyle();
    csDecNum.setDataFormat(wb.createDataFormat().getFormat("#,##0.#####"));
    csDecNum.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    csDecNum.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    csDecNum.setBorderRight(HSSFCellStyle.BORDER_THIN);
    csDecNum.setBorderTop(HSSFCellStyle.BORDER_THIN);

    HSSFCellStyle csIntNum = wb.createCellStyle();
    csIntNum.setDataFormat(wb.createDataFormat().getFormat("#,##0"));
    csIntNum.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    csIntNum.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    csIntNum.setBorderRight(HSSFCellStyle.BORDER_THIN);
    csIntNum.setBorderTop(HSSFCellStyle.BORDER_THIN);

    HSSFCellStyle csDateTime = wb.createCellStyle();
    csDateTime.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"/*opt.getDateTimeFormat()*/));
    csDateTime.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    csDateTime.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    csDateTime.setBorderRight(HSSFCellStyle.BORDER_THIN);
    csDateTime.setBorderTop(HSSFCellStyle.BORDER_THIN);

    if (opt.getCellsContent() != null)
        for (int i = 0; i < opt.getCellsContent().length; i++) {
            row = opt.getCellsContent()[i];
            r = s.createRow(rownum);

            for (short j = 0; j < row.length; j++) {
                c = r.createCell(j);
                obj = row[j];
                if (obj != null) {

                    if (obj instanceof String) {
                        try {
                            c.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
                        } catch (NoSuchMethodError ex) {
                        }
                        c.setCellValue(obj.toString());
                        c.setCellStyle(csText);
                    } else if (obj instanceof BigDecimal || obj instanceof Double || obj instanceof Float
                            || obj.getClass() == Double.TYPE || obj.getClass() == Float.TYPE) {
                        c.setCellValue(Double.parseDouble(obj.toString()));
                        c.setCellStyle(csDecNum);
                    } else if (obj instanceof Integer || obj instanceof Short || obj instanceof Long
                            || obj.getClass() == Integer.TYPE || obj.getClass() == Short.TYPE
                            || obj.getClass() == Long.TYPE) {
                        c.setCellValue(Double.parseDouble(obj.toString()));
                        c.setCellStyle(csIntNum);
                    } else if (obj instanceof Boolean) {
                        c.setCellValue(((Boolean) obj).booleanValue());
                        c.setCellStyle(csBool);
                    } else if (obj.getClass().equals(boolean.class)) {
                        c.setCellValue(((Boolean) obj).booleanValue());
                        c.setCellStyle(csBool);
                    } else if (obj instanceof Date || obj instanceof java.util.Date
                            || obj instanceof java.sql.Timestamp) {
                        c.setCellValue((java.util.Date) obj);
                        c.setCellStyle(csDateTime);
                    }
                } else {
                    c.setCellValue("");
                    c.setCellStyle(csText);
                }

            }
            rownum++;
        }
    return rownum;
}