Example usage for org.apache.poi.hssf.usermodel HSSFFont BOLDWEIGHT_BOLD

List of usage examples for org.apache.poi.hssf.usermodel HSSFFont BOLDWEIGHT_BOLD

Introduction

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

Prototype

short BOLDWEIGHT_BOLD

To view the source code for org.apache.poi.hssf.usermodel HSSFFont BOLDWEIGHT_BOLD.

Click Source Link

Document

Bold boldness (bold)

Usage

From source file:com.sevenorcas.openstyle.app.service.spreadsheet.SpreadSheet.java

protected void setStyleBold(HSSFWorkbook wb, HSSFCellStyle style, int styleId) { //EX1, int row, int column, SpreadsheetCell cell){
    HSSFFont font = wb.createFont();/*from w  w w .  ja v  a  2s  .  c o  m*/
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    style.setFont(font);
}

From source file:com.siteview.ecc.report.xls.JRXlsExporter.java

License:Open Source License

/**
 *
 *//* ww w  . ja  v a 2 s.com*/
protected HSSFFont getLoadedFont(JRFont font, short forecolor, Map attributes, Locale locale) {
    HSSFFont cellFont = null;

    String fontName = font.getFontName();
    if (fontMap != null && fontMap.containsKey(fontName)) {
        fontName = (String) fontMap.get(fontName);
    } else {
        FontInfo fontInfo = JRFontUtil.getFontInfo(fontName, locale);
        if (fontInfo != null) {
            //fontName found in font extensions
            FontFamily family = fontInfo.getFontFamily();
            String exportFont = family.getExportFont(getExporterKey());
            if (exportFont != null) {
                fontName = exportFont;
            }
        }
    }

    short superscriptType = HSSFFont.SS_NONE;

    if (attributes != null && attributes.get(TextAttribute.SUPERSCRIPT) != null) {
        Object value = attributes.get(TextAttribute.SUPERSCRIPT);
        if (TextAttribute.SUPERSCRIPT_SUPER.equals(value)) {
            superscriptType = HSSFFont.SS_SUPER;
        } else if (TextAttribute.SUPERSCRIPT_SUB.equals(value)) {
            superscriptType = HSSFFont.SS_SUB;
        }

    }
    for (int i = 0; i < loadedFonts.size(); i++) {
        HSSFFont cf = (HSSFFont) loadedFonts.get(i);

        short fontSize = (short) font.getFontSize();
        if (isFontSizeFixEnabled)
            fontSize -= 1;

        if (cf.getFontName().equals(fontName) && (cf.getColor() == forecolor)
                && (cf.getFontHeightInPoints() == fontSize)
                && ((cf.getUnderline() == HSSFFont.U_SINGLE) ? (font.isUnderline()) : (!font.isUnderline()))
                && (cf.getStrikeout() == font.isStrikeThrough())
                && ((cf.getBoldweight() == HSSFFont.BOLDWEIGHT_BOLD) ? (font.isBold()) : (!font.isBold()))
                && (cf.getItalic() == font.isItalic()) && (cf.getTypeOffset() == superscriptType)) {
            cellFont = cf;
            break;
        }
    }

    if (cellFont == null) {
        cellFont = workbook.createFont();

        cellFont.setFontName(fontName);
        cellFont.setColor(forecolor);

        short fontSize = (short) font.getFontSize();
        if (isFontSizeFixEnabled)
            fontSize -= 1;

        cellFont.setFontHeightInPoints(fontSize);

        if (font.isUnderline()) {
            cellFont.setUnderline(HSSFFont.U_SINGLE);
        }
        if (font.isStrikeThrough()) {
            cellFont.setStrikeout(true);
        }
        if (font.isBold()) {
            cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        }
        if (font.isItalic()) {
            cellFont.setItalic(true);
        }

        cellFont.setTypeOffset(superscriptType);
        loadedFonts.add(cellFont);
    }

    return cellFont;
}

From source file:com.square.core.util.poi.DocumentXls.java

License:Open Source License

/** Cree une page. */
private HSSFSheet creerPage() {
    final HSSFSheet page = classeur.createSheet();
    page.setMargin(HSSFSheet.LeftMargin, LARGEUR_MARGE);
    page.setMargin(HSSFSheet.RightMargin, LARGEUR_MARGE);

    row = 0;/* ww  w .j a  v  a 2 s  .  com*/

    if (entetesWidth != null) {
        // Dfinition de la largeur des colonnes
        int col = 0;
        for (Integer enteteWidth : entetesWidth) {
            if (enteteWidth != null) {
                page.setColumnWidth(col, enteteWidth);
            }
            col++;
        }
    }
    final HSSFCellStyle styleEntete = classeur.createCellStyle();
    final HSSFFont policeEntete = classeur.createFont();
    policeEntete.setFontHeightInPoints(ENTETE_FONT_HEIGHT);
    policeEntete.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    styleEntete.setFont(policeEntete);

    // Affichage des titres de colonnes
    ajouterLigne(entetes, styleEntete);

    return page;
}

From source file:com.ssic.education.provider.controller.ProSupplierController.java

@RequestMapping(value = "/excel")
@ResponseBody/*from  ww  w .j av a  2s  .c om*/
public ModelAndView exportExcel(SupplierDto supplierDto, HttpServletRequest request,
        HttpServletResponse response) {
    SessionInfo info = (SessionInfo) request.getSession().getAttribute(ConfigUtil.SESSIONINFONAME);
    if (info == null) {
        return null;
    }
    supplierDto.setReceiverId(info.getSupplierId());
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.M.d");
    Date date = new Date();
    String filename = Tools.date2Str(date, "yyyyMMddHHmmss");
    HSSFSheet sheet;
    HSSFCell cell;
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
    Workbook workbook = new HSSFWorkbook();
    sheet = (HSSFSheet) workbook.createSheet("");
    try {
        List<String> titles = new ArrayList<String>();
        titles.add("????");
        titles.add("???");
        //         titles.add("????");
        //         titles.add("??????");
        titles.add("????");
        titles.add("???");
        //         titles.add("?");
        //         titles.add("???");
        titles.add("?");
        titles.add("?");
        int len = titles.size();
        HSSFCellStyle headerStyle = (HSSFCellStyle) workbook.createCellStyle(); // ?
        headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        HSSFFont headerFont = (HSSFFont) workbook.createFont(); // 
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        headerFont.setFontHeightInPoints((short) 11);
        headerStyle.setFont(headerFont);
        short width = 20, height = 25 * 20;
        sheet.setDefaultColumnWidth(width);
        HSSFRow sheetRow = sheet.createRow(0);
        for (int i = 0; i < len; i++) { // 
            String title = titles.get(i);
            cell = sheetRow.createCell(i);
            cell.setCellStyle(headerStyle);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(title);
        }
        sheet.getRow(0).setHeight(height);
        HSSFCellStyle contentStyle = (HSSFCellStyle) workbook.createCellStyle(); // ?
        contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        DataGrid dg = supplierService.findProSupplier(supplierDto, null);
        List<SupplierDto> expList = dg.getRows();
        List<PageData> varList = new ArrayList<PageData>();
        if (!CollectionUtils.isEmpty(expList)) {
            for (int i = 0; i < expList.size(); i++) {
                PageData vpd = new PageData();
                vpd.put("var1", expList.get(i).getSupplierName());
                vpd.put("var2", expList.get(i).getAddress());
                //               vpd.put("var3", expList.get(i).getFoodServiceCode());
                //               vpd.put("var4", expList.get(i).getFoodBusinessCode());
                vpd.put("var3", expList.get(i).getFoodCirculationCode());
                vpd.put("var4", expList.get(i).getFoodProduceCode());
                //               vpd.put("var7", expList.get(i).getBusinessLicense());
                //               vpd.put("var8", expList.get(i).getSupplierCode());
                vpd.put("var5", expList.get(i).getCorporation());
                vpd.put("var6", expList.get(i).getContactWay());
                varList.add(vpd);
            }
        }
        for (int i = 0; i < varList.size(); i++) {
            HSSFRow row = sheet.createRow(i + 1);
            PageData vpd = varList.get(i);
            for (int j = 0; j < len; j++) {
                String varstr = vpd.getString("var" + (j + 1)) != null ? vpd.getString("var" + (j + 1)) : "";
                cell = row.createCell(j);
                HSSFCellStyle cellStyle2 = (HSSFCellStyle) workbook.createCellStyle();
                HSSFDataFormat format = (HSSFDataFormat) workbook.createDataFormat();
                cellStyle2.setDataFormat(format.getFormat("@"));
                cell.setCellStyle(cellStyle2);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(varstr);

            }

        }
        OutputStream os = response.getOutputStream();
        workbook.write(os);
        os.flush();
        os.close();
    } catch (Exception e) {

    }
    return null;
}

From source file:com.ssic.education.provider.controller.WaresController.java

@RequestMapping(value = "/excel")
@ResponseBody/*from  w  w  w .  j  a  va 2 s. c  o  m*/
public ModelAndView exportExcel(ProWaresDto proWaresDto, HttpServletRequest request,
        HttpServletResponse response) {
    SessionInfo info = (SessionInfo) request.getSession().getAttribute(ConfigUtil.SESSIONINFONAME);
    if (info == null) {
        return null;
    }
    proWaresDto.setSupplierId(info.getSupplierId());
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.M.d");
    Date date = new Date();
    String filename = Tools.date2Str(date, "yyyyMMddHHmmss");
    HSSFSheet sheet;
    HSSFCell cell;
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
    Workbook workbook = new HSSFWorkbook();
    sheet = (HSSFSheet) workbook.createSheet("");
    try {
        List<String> titles = new ArrayList<String>();
        titles.add("??");
        titles.add("???");
        titles.add("");
        titles.add("?");
        titles.add("?");
        //         titles.add("??");
        //         titles.add("???");
        //         titles.add("??");
        titles.add("?");
        titles.add("???");
        titles.add("");
        int len = titles.size();
        HSSFCellStyle headerStyle = (HSSFCellStyle) workbook.createCellStyle(); // ?
        headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        HSSFFont headerFont = (HSSFFont) workbook.createFont(); // 
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        headerFont.setFontHeightInPoints((short) 11);
        headerStyle.setFont(headerFont);
        short width = 20, height = 25 * 20;
        sheet.setDefaultColumnWidth(width);
        HSSFRow sheetRow = sheet.createRow(0);
        for (int i = 0; i < len; i++) { // 
            String title = titles.get(i);
            cell = sheetRow.createCell(i);
            cell.setCellStyle(headerStyle);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(title);
        }
        sheet.getRow(0).setHeight(height);
        HSSFCellStyle contentStyle = (HSSFCellStyle) workbook.createCellStyle(); // ?
        contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        List<ProWaresDto> expList = waresService.findAllWares(proWaresDto, null);
        List<PageData> varList = new ArrayList<PageData>();
        if (!CollectionUtils.isEmpty(expList)) {
            for (int i = 0; i < expList.size(); i++) {
                PageData vpd = new PageData();
                vpd.put("var1", expList.get(i).getWaresName());
                vpd.put("var2", expList.get(i).getAmountUnit());
                vpd.put("var3", expList.get(i).getSpec());
                vpd.put("var4", ProductClass.getName(expList.get(i).getWaresType()));
                vpd.put("var5", expList.get(i).getManufacturer());
                //               vpd.put("var5", expList.get(i).getEnName());
                //               vpd.put("var6", expList.get(i).getBarCode());
                //               vpd.put("var7", expList.get(i).getCustomCode());
                vpd.put("var6", expList.get(i).getShelfLife());
                vpd.put("var7", expList.get(i).getUnit());
                vpd.put("var8", expList.get(i).getPlace());
                varList.add(vpd);
            }
        }
        for (int i = 0; i < varList.size(); i++) {
            HSSFRow row = sheet.createRow(i + 1);
            PageData vpd = varList.get(i);
            for (int j = 0; j < len; j++) {
                String varstr = vpd.getString("var" + (j + 1)) != null ? vpd.getString("var" + (j + 1)) : "";
                cell = row.createCell(j);
                HSSFCellStyle cellStyle2 = (HSSFCellStyle) workbook.createCellStyle();
                HSSFDataFormat format = (HSSFDataFormat) workbook.createDataFormat();
                cellStyle2.setDataFormat(format.getFormat("@"));
                cell.setCellStyle(cellStyle2);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(varstr);

            }

        }
        OutputStream os = response.getOutputStream();
        workbook.write(os);
        os.flush();
        os.close();
    } catch (Exception e) {

    }
    return null;
}

From source file:com.tecnosur.util.Excel.java

private static void createTituloCell(HSSFWorkbook wb, Row row, int column, short halign, short valign,
        String strContenido) {/*  ww w  .  j  a va 2s.c o  m*/

    CreationHelper ch = wb.getCreationHelper();
    Cell cell = row.createCell(column);
    cell.setCellValue(ch.createRichTextString(strContenido));

    HSSFFont cellFont = wb.createFont();
    cellFont.setFontHeightInPoints((short) 11);
    cellFont.setFontName(HSSFFont.FONT_ARIAL);
    cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(halign);
    cellStyle.setVerticalAlignment(valign);
    cellStyle.setFont(cellFont);
    cell.setCellStyle(cellStyle);

}

From source file:com.tecnosur.util.Excel.java

private static void createCell(HSSFWorkbook wb, Row row, int column, short halign, short valign,
        String strContenido, boolean booBorde, boolean booCabecera) {
    CreationHelper ch = wb.getCreationHelper();
    Cell cell = row.createCell(column);/*www  .  java 2 s  .  c  om*/
    HSSFFont cellFont = wb.createFont();
    cellFont.setFontHeightInPoints((short) 8);
    cellFont.setFontName(HSSFFont.FONT_ARIAL);
    cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

    cell.setCellValue(ch.createRichTextString(strContenido));
    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(halign);
    cellStyle.setVerticalAlignment(valign);
    cellStyle.setFont(cellFont);
    if (booBorde) {
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBottomBorderColor((short) 8);
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyle.setLeftBorderColor((short) 8);
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cellStyle.setRightBorderColor((short) 8);
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyle.setTopBorderColor((short) 8);
    }
    if (booCabecera) {
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBottomBorderColor((short) 8);
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyle.setLeftBorderColor((short) 8);
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cellStyle.setRightBorderColor((short) 8);
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyle.setTopBorderColor((short) 8);

        cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    }
    cell.setCellStyle(cellStyle);
}

From source file:com.testmax.util.ExcelSheet.java

License:CDDL license

/**
* This method is used to set the styles for all the headers
* of the excel sheet.//from w  w w  .java  2 s  . co m
* @param sampleWorkBook - Name of the workbook.
* @return cellStyle - Styles for the Header data of Excel sheet.
*/
private HSSFCellStyle setHeaderStyle(HSSFWorkbook workBook) {
    HSSFFont font = workBook.createFont();
    font.setFontName(HSSFFont.FONT_ARIAL);
    font.setColor(IndexedColors.PLUM.getIndex());
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    HSSFCellStyle cellStyle = workBook.createCellStyle();
    cellStyle.setFont(font);
    return cellStyle;
}

From source file:com.ts.excelservlet.UDR_Driver_Excel.java

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 *//*from  w  ww .j  a v a 2  s .c om*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // TODO Auto-generated method stub

    DBTransaction dbtranobj = new DBTransaction();
    String vehicle_number = request.getParameter("vehicle_number");
    HttpSession session = request.getSession(true);
    String[] select = (String[]) session.getAttribute("id");
    String driver_name = request.getParameter("driver_name");

    //System.out.println("IMEI : " +imeinumber);

    //String vehicle_number="";
    int index = 2;
    System.out.println("************** doGet ************");
    OutputStream out = null;
    try {
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=Driver_UDR.xls");
        Connection con = dbtranobj.connect();

        // Class.forName("org.postgresql.Driver").newInstance();
        //conn = DriverManager.getConnection("jdbc:postgresql://182.72.167.34:5432/master_database","postgres", "postgres");
        ResultSet rs = null;
        Statement st = null;
        st = con.createStatement();
        rs = st.executeQuery(
                "SELECT * FROM driver_info WHERE driver_name='" + driver_name + "' order by driver_name");

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("Driver  Sheet");

        sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 1));
        sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 2));
        /* sheet.addMergedRegion(new Region(0,(short)0,0,(short)3));
         sheet.addMergedRegion(new Region(0,(short)0,0,(short)4));
         sheet.addMergedRegion(new Region(0,(short)0,0,(short)5));*/

        HSSFRow rowhead = sheet.createRow((short) 0);
        rowhead.setHeight((short) 500);
        /*rowhead.createCell((short) 0).setCellValue("Fuel Information For " +vehicle_number);
         * */
        HSSFCell cell2B = rowhead.createCell(0);
        cell2B.setCellValue(new HSSFRichTextString("Report For Driver : " + driver_name));

        // Style Font in Cell 2B  
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle = wb.createCellStyle();
        HSSFFont hSSFFont = wb.createFont();
        hSSFFont.setFontName(HSSFFont.FONT_ARIAL);
        hSSFFont.setFontHeightInPoints((short) 14);
        hSSFFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        hSSFFont.setColor(HSSFColor.BLUE.index);
        cellStyle.setFont(hSSFFont);
        cell2B.setCellStyle(cellStyle);
        HSSFRow rowhead1 = sheet.createRow((short) 1);
        rowhead1.setHeight((short) 600);
        Cell cell = rowhead1.createCell((short) 0);
        HSSFCellStyle cellStyle1 = wb.createCellStyle();
        cellStyle1 = wb.createCellStyle();
        HSSFFont hSSFFont1 = wb.createFont();
        hSSFFont1.setFontName(HSSFFont.FONT_ARIAL);
        hSSFFont1.setFontHeightInPoints((short) 12);
        hSSFFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        hSSFFont1.setColor(HSSFColor.BLACK.index);
        cellStyle1.setFont(hSSFFont1);
        cell.setCellStyle(cellStyle1);
        // cell.setCellValue("SNO");
        sheet.setColumnWidth(0, 7000);
        // rowhead.createCell((short) 0).setCellValue("Date");

        if (select != null && select.length != 0) {

            for (int i = 0; i < select.length; i++) {

                if (select[i].equalsIgnoreCase("doe")) {

                    Cell cell1 = rowhead1.createCell((short) i);
                    cell1.setCellStyle(cellStyle1);
                    cell1.setCellValue("LICENCE EXPIRY");
                    sheet.setColumnWidth(i, 7000);

                } else if (select[i].equalsIgnoreCase("doj")) {

                    Cell cell1 = rowhead1.createCell((short) i);
                    cell1.setCellStyle(cellStyle1);
                    cell1.setCellValue("JOINED DATE");
                    sheet.setColumnWidth(i, 7000);

                }

                else if (select[i].equalsIgnoreCase("license_number")) {

                    Cell cell1 = rowhead1.createCell((short) i);
                    cell1.setCellStyle(cellStyle1);
                    cell1.setCellValue("LICENCE NUMBER ");
                    sheet.setColumnWidth(i, 7000);

                }

                else if (select[i].equalsIgnoreCase("vehicle_number")) {

                    Cell cell1 = rowhead1.createCell((short) i);
                    cell1.setCellStyle(cellStyle1);
                    cell1.setCellValue("VEHICLE NUMBER");
                    sheet.setColumnWidth(i, 7000);

                }

                else if (select[i].equalsIgnoreCase("shift_number")) {

                    Cell cell1 = rowhead1.createCell((short) i);
                    cell1.setCellStyle(cellStyle1);
                    cell1.setCellValue("SHIFT NUMBER");
                    sheet.setColumnWidth(i, 7000);

                }

                else if (select[i].equalsIgnoreCase("route_number")) {

                    Cell cell1 = rowhead1.createCell((short) i);
                    cell1.setCellStyle(cellStyle1);
                    cell1.setCellValue("ROUTE NUMBER");
                    sheet.setColumnWidth(i, 7000);

                }

                else if (select[i].equalsIgnoreCase("address")) {

                    Cell cell1 = rowhead1.createCell((short) i);
                    cell1.setCellStyle(cellStyle1);
                    cell1.setCellValue("ADDRESS");
                    sheet.setColumnWidth(i, 7000);

                }

                else {
                    Cell cell1 = rowhead1.createCell((short) i);
                    cell1.setCellStyle(cellStyle1);
                    cell1.setCellValue(select[i]);
                    sheet.setColumnWidth(i, 7000);
                }

            }
        }

        rs = st.executeQuery(
                "SELECT * FROM driver_info WHERE driver_name='" + driver_name + "' order by driver_name");

        while (rs.next()) {

            HSSFRow row = sheet.createRow((short) index);
            row.setHeight((short) 500);

            if (select != null && select.length != 0) {

                for (int i = 0; i < select.length; i++) {

                    row.createCell((short) i).setCellValue(rs.getString(select[i]));

                }
            }

            index++;
        }

        out = response.getOutputStream();
        wb.write(out);

    } catch (Exception e) {
        throw new ServletException("Exception in Excel Sample Servlet", e);
    } finally {
        if (out != null)
            out.close();
    }

}

From source file:com.util.poi.ExcelView.java

License:Open Source License

/**
 * ?Excel//from   ww  w.  jav a2s  .c  o m
 * 
 * @param model
 *            ?
 * @param workbook
 *            workbook
 * @param request
 *            request
 * @param response
 *            response
 */
public void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    Assert.notEmpty(properties);
    HSSFSheet sheet;
    if (StringUtils.isNotEmpty(sheetName)) {
        sheet = workbook.createSheet(sheetName);
    } else {
        sheet = workbook.createSheet();
    }
    int rowNumber = 0;
    if (titles != null && titles.length > 0) {
        HSSFRow header = sheet.createRow(rowNumber);
        header.setHeight((short) 400);
        for (int i = 0; i < properties.length; i++) {
            HSSFCell cell = header.createCell(i);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            HSSFFont font = workbook.createFont();
            font.setFontHeightInPoints((short) 11);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            if (i == 0) {
                HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
                HSSFComment comment = patriarch
                        .createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 4, 4));
                //comment.setString(new HSSFRichTextString("P" + "o" + "w" + "e" + "r" + "e" + "d" + " " + "B" + "y" + " " + "S" + "H" + "O" + "P" + "+" + "+"));
                cell.setCellComment(comment);
            }
            if (titles.length > i && titles[i] != null) {
                cell.setCellValue(titles[i]);
            } else {
                cell.setCellValue(properties[i]);
            }
            if (widths != null && widths.length > i && widths[i] != null) {
                sheet.setColumnWidth(i, widths[i]);
            } else {
                sheet.autoSizeColumn(i);
            }
        }
        rowNumber++;
    }
    if (data != null) {
        for (Object item : data) {
            HSSFRow row = sheet.createRow(rowNumber);
            for (int i = 0; i < properties.length; i++) {
                HSSFCell cell = row.createCell(i);
                if (converters != null && converters.length > i && converters[i] != null) {
                    Class<?> clazz = PropertyUtils.getPropertyType(item, properties[i]);
                    ConvertUtils.register(converters[i], clazz);
                    /*   Map<String, Object> map=toHashMap(item);
                       cell.setCellValue(map.get(properties[i]).toString());*/
                    cell.setCellValue(BeanUtils.getProperty(item, properties[i]));
                    ConvertUtils.deregister(clazz);
                    if (clazz.equals(Date.class)) {
                        DateConverter dateConverter = new DateConverter();
                        dateConverter.setPattern(DEFAULT_DATE_PATTERN);
                        ConvertUtils.register(dateConverter, Date.class);
                    }
                } else {
                    /*Map<String, Object> map=toHashMap(item);
                    cell.setCellValue(map.get(properties[i]).toString());*/
                    cell.setCellValue(BeanUtils.getProperty(item, properties[i]));
                }
                if (rowNumber == 0 || rowNumber == 1) {
                    if (widths != null && widths.length > i && widths[i] != null) {
                        sheet.setColumnWidth(i, widths[i]);
                    } else {
                        sheet.autoSizeColumn(i);
                    }
                }
            }
            rowNumber++;
        }
    }
    if (contents != null && contents.length > 0) {
        rowNumber++;
        for (String content : contents) {
            HSSFRow row = sheet.createRow(rowNumber);
            HSSFCell cell = row.createCell(0);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            HSSFFont font = workbook.createFont();
            font.setColor(HSSFColor.GREY_50_PERCENT.index);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(content);
            rowNumber++;
        }
    }
    response.setContentType("application/force-download");
    if (StringUtils.isNotEmpty(filename)) {
        response.setHeader("Content-disposition",
                "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
    } else {
        response.setHeader("Content-disposition", "attachment");
    }
}