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

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

Introduction

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

Prototype


public void setItalic(boolean italic) 

Source Link

Document

set whether to use italics or not

Usage

From source file:org.oep.cmon.report.portlet.util.ActionUtil.java

License:Apache License

/**
 * This is function excel BaoCaochungthuc
 * Version: 1.0/*w w  w  .j ava 2  s .  c  o m*/
 *  
 * History: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  ------------------------------------------------- 
 *  3-March-2013  Nam Dinh    Create new
 * @param req
 * @param res
 */
public static void excelBaocaochungthuc(ResourceRequest req, ResourceResponse res) {
    try {
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        String coQuanQuanLyId = req.getParameter("coQuanQuanLyId");
        CoQuanQuanLy coQuanQuanLy = new CoQuanQuanLyImpl();
        if (coQuanQuanLyId != null && !coQuanQuanLyId.equals("0")) {
            coQuanQuanLy = CoQuanQuanLyLocalServiceUtil.getCoQuanQuanLy(Long.valueOf(coQuanQuanLyId));
        }
        HSSFWorkbook workbook = new HSSFWorkbook();

        HSSFSheet sheet = workbook.createSheet("Baocaohosochungthuc");

        // define a cell style
        HSSFFont tableHeaderFont = (HSSFFont) workbook.createFont();
        tableHeaderFont.setBoldweight((short) 5000);

        HSSFFont ngaythangFont = (HSSFFont) workbook.createFont();
        ngaythangFont.setItalic(true);

        HSSFFont titleFont = (HSSFFont) workbook.createFont();
        titleFont.setBoldweight((short) 5000);
        titleFont.setFontHeightInPoints((short) 15);

        HSSFCellStyle styleCellHeader = (HSSFCellStyle) workbook.createCellStyle();
        styleCellHeader.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        styleCellHeader.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        styleCellHeader.setFont(tableHeaderFont);
        styleCellHeader.setWrapText(true);

        HSSFCellStyle styleCellNgaythang = (HSSFCellStyle) workbook.createCellStyle();
        styleCellNgaythang.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        styleCellNgaythang.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        styleCellNgaythang.setFont(ngaythangFont);
        styleCellNgaythang.setWrapText(true);

        HSSFCellStyle styleCellCenter = (HSSFCellStyle) workbook.createCellStyle();
        styleCellCenter.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        styleCellCenter.setBorderTop(HSSFCellStyle.BORDER_THIN);
        styleCellCenter.setBorderRight(HSSFCellStyle.BORDER_THIN);
        styleCellCenter.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        styleCellCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        styleCellCenter.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        styleCellCenter.setWrapText(true);

        HSSFCellStyle styleCellLeft = (HSSFCellStyle) workbook.createCellStyle();
        styleCellLeft.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        styleCellLeft.setBorderTop(HSSFCellStyle.BORDER_THIN);
        styleCellLeft.setBorderRight(HSSFCellStyle.BORDER_THIN);
        styleCellLeft.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        styleCellLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        styleCellLeft.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        styleCellLeft.setWrapText(true);

        HSSFCellStyle styleCellRight = (HSSFCellStyle) workbook.createCellStyle();
        styleCellRight.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        styleCellRight.setBorderTop(HSSFCellStyle.BORDER_THIN);
        styleCellRight.setBorderRight(HSSFCellStyle.BORDER_THIN);
        styleCellRight.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        styleCellRight.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
        styleCellRight.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        styleCellRight.setWrapText(true);
        styleCellRight.setFont(tableHeaderFont);

        HSSFCellStyle styleHeader = (HSSFCellStyle) workbook.createCellStyle();
        styleHeader.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        styleHeader.setBorderTop(HSSFCellStyle.BORDER_THIN);
        styleHeader.setBorderRight(HSSFCellStyle.BORDER_THIN);
        styleHeader.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        styleHeader.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        styleHeader.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        styleHeader.setFont(tableHeaderFont);
        styleHeader.setWrapText(true);

        HSSFCellStyle styleCellNhomTTHC = (HSSFCellStyle) workbook.createCellStyle();
        styleCellNhomTTHC.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        styleCellNhomTTHC.setBorderTop(HSSFCellStyle.BORDER_THIN);
        styleCellNhomTTHC.setBorderRight(HSSFCellStyle.BORDER_THIN);
        styleCellNhomTTHC.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        styleCellNhomTTHC.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        styleCellNhomTTHC.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        styleCellNhomTTHC.setFont(tableHeaderFont);

        HSSFCellStyle styleTitle = (HSSFCellStyle) workbook.createCellStyle();
        styleTitle.setFont(titleFont);
        styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        styleTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // end style
        String tungay = ParamUtil.getString(req, "tungay");
        String denngay = ParamUtil.getString(req, "denngay");
        int rowNum = 1;
        sheet.addMergedRegion(new Region(rowNum, (short) 0, rowNum, (short) 1));
        HSSFRow rowUBND = sheet.createRow(rowNum);
        rowUBND.createCell(0).setCellValue("");
        rowUBND.getCell(0).setCellStyle(styleCellHeader);

        sheet.addMergedRegion(new Region(rowNum, (short) 2, rowNum, (short) 11));
        rowUBND.createCell(2).setCellValue(ActionUtil.getLanguage(req, "vn.dtt.cmon.report.excel.chxhcnvn"));
        rowUBND.getCell(2).setCellStyle(styleCellHeader);

        rowNum++;

        sheet.addMergedRegion(new Region(rowNum, (short) 0, rowNum, (short) 1));
        HSSFRow rowTenDonVi = sheet.createRow(rowNum);
        rowTenDonVi.createCell(0).setCellValue(coQuanQuanLy.getTen());
        rowTenDonVi.getCell(0).setCellStyle(styleCellHeader);

        sheet.addMergedRegion(new Region(rowNum, (short) 2, rowNum, (short) 11));
        rowTenDonVi.createCell(2).setCellValue(ActionUtil.getLanguage(req, "vn.dtt.cmon.report.excel.dltdhp"));
        rowTenDonVi.getCell(2).setCellStyle(styleCellHeader);
        rowNum++;
        String str_loaihoso = ParamUtil.getString(req, "id_loaihoso");
        List<HoSoChungThuc> listhosochungthuc = ActionUtil.listhsct(str_loaihoso, tungay, denngay);

        String sochungthuc = ActionUtil.getLanguage(req, "vn.dtt.cmon.report.baocaochungthuc.sochungthuc");
        String nguoinop = ActionUtil.getLanguage(req, "vn.dtt.cmon.report.baocaochungthuc.nguoinop");
        String diachi = ActionUtil.getLanguage(req, "vn.dtt.cmon.report.baocaochungthuc.diachi");
        String noidungcongviec = ActionUtil.getLanguage(req,
                "vn.dtt.cmon.report.baocaochungthuc.noidungcongviec");
        String loaigiayto = ActionUtil.getLanguage(req, "vn.dtt.cmon.report.baocaochungthuc.loaigiayto");
        String sobo = ActionUtil.getLanguage(req, "vn.dtt.cmon.report.baocaochungthuc.sobo");
        String sotrang = ActionUtil.getLanguage(req, "vn.dtt.cmon.report.baocaochungthuc.sotrang");
        String ngaynhan = ActionUtil.getLanguage(req, "vn.dtt.cmon.report.baocaochungthuc.ngaynhan");
        String ngaytraketqua = ActionUtil.getLanguage(req, "vn.dtt.cmon.report.baocaochungthuc.ngaytraketqua");
        String lephi = ActionUtil.getLanguage(req, "vn.dtt.cmon.report.baocaochungthuc.lephi");
        String nguoithuchien = ActionUtil.getLanguage(req, "vn.dtt.cmon.report.baocaochungthuc.nguoithuchien");
        String nguoiky = ActionUtil.getLanguage(req, "vn.dtt.cmon.report.baocaochungthuc.nguoiky");
        String tieude = ActionUtil.getLanguage(req, "vn.dtt.cmon.report.baocaochungthuc.tieude");
        String[] header = { "STT", sochungthuc, nguoinop, diachi, noidungcongviec, loaigiayto, sobo, sotrang,
                ngaynhan, ngaytraketqua, lephi, nguoithuchien, nguoiky };

        rowNum++;
        int[] width = { 1500, 2000, 5000, 5000, 5000, 5000, 2000, 2000, 3000, 3000, 2000, 4000, 4000 };
        sheet.addMergedRegion(new Region(rowNum, (short) 0, rowNum + 1, (short) 12));
        HSSFRow titleRow = (HSSFRow) sheet.createRow(rowNum);
        HSSFCell titleCell = titleRow.createCell(0);
        titleCell.setCellStyle(styleTitle);
        titleCell.setCellValue(tieude);

        rowNum += 2;

        sheet.addMergedRegion(new Region(rowNum, (short) 0, rowNum, (short) 12));
        HSSFRow ngaythangRow = (HSSFRow) sheet.createRow(rowNum);
        HSSFCell ngaythangCell = ngaythangRow.createCell(0);
        ngaythangCell.setCellStyle(styleCellNgaythang);
        ngaythangCell.setCellValue(ActionUtil.getLanguage(req, "vn.dtt.cmon.report.excel.tungay") + " " + tungay
                + " " + ActionUtil.getLanguage(req, "vn.dtt.cmon.report.excel.denngay") + " " + denngay);

        rowNum = rowNum + 3;
        HSSFRow headerRow = (HSSFRow) sheet.createRow(rowNum);
        int colNum = 0;
        for (int i = 0; i < header.length; i++) {
            HSSFCell cell = headerRow.createCell(colNum);
            cell.setCellStyle(styleHeader);
            cell.setCellValue(header[i]);
            sheet.setColumnWidth(i, width[i]);
            colNum++;
        }
        rowNum++;
        long tong = 0;
        int stt = 0;
        for (int a = 0; a < listhosochungthuc.size(); a++) {
            // khai bao dong va cac cell
            stt++;
            HSSFRow row = (HSSFRow) sheet.createRow(rowNum);
            HSSFCell cell = row.createCell(0);
            HSSFCell cell1 = row.createCell(1);
            HSSFCell cell2 = row.createCell(2);
            HSSFCell cell3 = row.createCell(3);
            HSSFCell cell4 = row.createCell(4);
            HSSFCell cell5 = row.createCell(5);
            HSSFCell cell6 = row.createCell(6);
            HSSFCell cell7 = row.createCell(7);
            HSSFCell cell8 = row.createCell(8);
            HSSFCell cell9 = row.createCell(9);
            HSSFCell cell10 = row.createCell(10);
            HSSFCell cell11 = row.createCell(11);
            HSSFCell cell12 = row.createCell(12);
            // set style cho cac cell
            cell.setCellStyle(styleCellCenter);
            cell1.setCellStyle(styleCellLeft);
            cell2.setCellStyle(styleCellCenter);
            cell3.setCellStyle(styleCellCenter);
            cell4.setCellStyle(styleCellCenter);
            cell5.setCellStyle(styleCellCenter);
            cell6.setCellStyle(styleCellCenter);
            cell7.setCellStyle(styleCellCenter);
            cell8.setCellStyle(styleCellCenter);
            cell9.setCellStyle(styleCellCenter);
            cell10.setCellStyle(styleCellCenter);
            cell11.setCellStyle(styleCellCenter);
            cell12.setCellStyle(styleCellCenter);

            // set gia tri cho cac cell
            String diachithuongtru = listhosochungthuc.get(a).getDiaChiThuongTruNguoiNop() != null
                    ? listhosochungthuc.get(a).getDiaChiThuongTruNguoiNop()
                    : "";
            String dienthoaididong = listhosochungthuc.get(a).getSoDienThoaiDiDongNguoiNop() != null
                    ? "T:" + listhosochungthuc.get(a).getSoDienThoaiDiDongNguoiNop()
                    : "";
            cell.setCellValue(stt);
            cell1.setCellValue(listhosochungthuc.get(a).getSoChungThuc() != null
                    ? listhosochungthuc.get(a).getSoChungThuc()
                    : "");
            cell2.setCellValue(listhosochungthuc.get(a).getHoTenNguoiNopHoSo() != null
                    ? listhosochungthuc.get(a).getHoTenNguoiNopHoSo()
                    : "");
            cell3.setCellValue(diachithuongtru + "\n" + dienthoaididong);
            cell4.setCellValue(listhosochungthuc.get(a).getTenChungThuc());
            cell5.setCellValue(
                    listhosochungthuc.get(a).getThuTucHanhChinhId() != null ? ThuTucHanhChinhLocalServiceUtil
                            .getThuTucHanhChinh(listhosochungthuc.get(a).getThuTucHanhChinhId()).getTen() : "");
            cell6.setCellValue(listhosochungthuc.get(a).getSoBoHoSo());
            cell7.setCellValue(listhosochungthuc.get(a).getSoTo());
            cell8.setCellValue(listhosochungthuc.get(a).getNgayNopHoSo() != null
                    ? df.format(listhosochungthuc.get(a).getNgayNopHoSo())
                    : "");
            cell9.setCellValue(listhosochungthuc.get(a).getNgayTraKetQua() != null
                    ? df.format(listhosochungthuc.get(a).getNgayTraKetQua())
                    : "");
            cell10.setCellValue(listhosochungthuc.get(a).getLePhi());
            cell11.setCellValue(
                    listhosochungthuc.get(a).getCanBoTiepNhanId() != null
                            ? CongChucLocalServiceUtil
                                    .fetchCongChuc(listhosochungthuc.get(a).getCanBoTiepNhanId()).getHoVaTen()
                            : "");
            cell12.setCellValue(
                    listhosochungthuc.get(a).getTenCanBoKy() != null ? listhosochungthuc.get(a).getTenCanBoKy()
                            : "");
            rowNum++;
        }

        res.setContentType("application/vnd.ms-excel");
        res.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");
        res.addProperty(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=Baocaohosochungthuc.xls");
        OutputStream out = res.getPortletOutputStream();
        workbook.write(out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.oep.cmon.report.portlet.util.ActionUtil.java

License:Apache License

/**
 * This is function excel report detail//from  www . j av  a  2  s  . c  o  m
 * Version: 1.0
 *  
 * History: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  ------------------------------------------------- 
 *  3-March-2013  Nam Dinh    Create new
 * @param resourceRequest
 * @param resourceResponse
 */
public static void excelbaocaochitiet(ResourceRequest resourceRequest, ResourceResponse resourceResponse) {
    try {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("new sheet");
        HSSFFont Row2_font = workbook.createFont();
        Row2_font.setFontName(HSSFFont.FONT_ARIAL);
        Row2_font.setFontHeightInPoints((short) 13);
        Row2_font.setUnderline(HSSFFont.U_SINGLE);

        HSSFFont ngaythang_font = workbook.createFont();
        ngaythang_font.setFontName(HSSFFont.FONT_ARIAL);
        ngaythang_font.setFontHeightInPoints((short) 13);
        ngaythang_font.setItalic(true);

        HSSFFont style_font = workbook.createFont();
        style_font.setFontHeightInPoints((short) 13);

        HSSFFont trangthai_font = workbook.createFont();
        trangthai_font.setFontName(HSSFFont.FONT_ARIAL);
        trangthai_font.setFontHeightInPoints((short) 11);
        trangthai_font.setItalic(true);

        /*define a cell style*/
        HSSFFont tableHeaderFont = (HSSFFont) workbook.createFont();
        tableHeaderFont.setBoldweight((short) 3500);
        HSSFFont titleFont = (HSSFFont) workbook.createFont();
        titleFont.setBoldweight((short) 3500);
        titleFont.setFontHeightInPoints((short) 13);

        HSSFFont titleTongStyle_font = (HSSFFont) workbook.createFont();
        titleTongStyle_font.setFontHeightInPoints((short) 13);
        titleTongStyle_font.setBoldweight((short) 3500);

        HSSFCellStyle styleCellCenter = (HSSFCellStyle) workbook.createCellStyle();
        styleCellCenter.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        styleCellCenter.setBorderTop(HSSFCellStyle.BORDER_THIN);
        styleCellCenter.setBorderRight(HSSFCellStyle.BORDER_THIN);
        styleCellCenter.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        styleCellCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        styleCellCenter.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        styleCellCenter.setWrapText(true);

        HSSFCellStyle styleCellLeft = (HSSFCellStyle) workbook.createCellStyle();
        styleCellLeft.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        styleCellLeft.setBorderTop(HSSFCellStyle.BORDER_THIN);
        styleCellLeft.setBorderRight(HSSFCellStyle.BORDER_THIN);
        styleCellLeft.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        styleCellLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        styleCellLeft.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        styleCellLeft.setWrapText(true);

        HSSFCellStyle styleCellRight = (HSSFCellStyle) workbook.createCellStyle();
        styleCellRight.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        styleCellRight.setBorderTop(HSSFCellStyle.BORDER_THIN);
        styleCellRight.setBorderRight(HSSFCellStyle.BORDER_THIN);
        styleCellRight.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        styleCellRight.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
        styleCellRight.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        styleCellRight.setWrapText(true);

        HSSFCellStyle titleTongStyle = (HSSFCellStyle) workbook.createCellStyle();
        titleTongStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        titleTongStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        titleTongStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        titleTongStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        titleTongStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
        titleTongStyle.setFont(titleTongStyle_font);

        HSSFCellStyle titleLinhvucStyle = (HSSFCellStyle) workbook.createCellStyle();
        titleLinhvucStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        titleLinhvucStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        titleLinhvucStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        titleLinhvucStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        titleLinhvucStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        titleLinhvucStyle.setFont(titleTongStyle_font);

        HSSFCellStyle styleQuochieu = (HSSFCellStyle) workbook.createCellStyle();
        styleQuochieu.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        styleQuochieu.setFont(style_font);

        HSSFCellStyle styleRow2 = (HSSFCellStyle) workbook.createCellStyle();
        styleRow2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        styleRow2.setFont(Row2_font);

        HSSFCellStyle ngaythang_style = (HSSFCellStyle) workbook.createCellStyle();
        ngaythang_style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
        ngaythang_style.setFont(ngaythang_font);

        HSSFCellStyle style_trangthai = (HSSFCellStyle) workbook.createCellStyle();
        style_trangthai.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style_trangthai.setFont(trangthai_font);

        HSSFCellStyle style_tungay_denngay = (HSSFCellStyle) workbook.createCellStyle();
        style_tungay_denngay.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style_tungay_denngay.setFont(trangthai_font);

        HSSFCellStyle styleHeader = (HSSFCellStyle) workbook.createCellStyle();
        styleHeader.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        styleHeader.setBorderTop(HSSFCellStyle.BORDER_THIN);
        styleHeader.setBorderRight(HSSFCellStyle.BORDER_THIN);
        styleHeader.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        styleHeader.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        styleHeader.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        styleHeader.setFont(tableHeaderFont);
        styleHeader.setWrapText(true);

        HSSFCellStyle styleTitle = (HSSFCellStyle) workbook.createCellStyle();
        styleTitle.setFont(titleFont);
        styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        styleTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        /*end style*/
        int rowNum = 0;
        String[] header = { "STT", "Lnh vc",
                LanguageUtil.get(
                        (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                        resourceRequest.getLocale(), "vn.dtt.cmon.report.baocaochitiet.tenhoso"),
                LanguageUtil.get(
                        (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                        resourceRequest.getLocale(), "vn.dtt.cmon.report.baocaochitiet.sohoso"),
                LanguageUtil.get(
                        (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                        resourceRequest.getLocale(), "vn.dtt.cmon.report.baocaochitiet.trangthai"),
                LanguageUtil.get(
                        (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                        resourceRequest.getLocale(), "vn.dtt.cmon.report.baocaochitiet.diachi"),
                LanguageUtil.get(
                        (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                        resourceRequest.getLocale(), "vn.dtt.cmon.report.baocaochitiet.dienthoai"),
                LanguageUtil.get(
                        (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                        resourceRequest.getLocale(), "vn.dtt.cmon.report.baocaochitiet.nguoinophoso"),
                LanguageUtil.get(
                        (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                        resourceRequest.getLocale(), "vn.dtt.cmon.report.baocaochitiet.chuhoso"),
                LanguageUtil.get(
                        (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                        resourceRequest.getLocale(), "vn.dtt.cmon.report.baocaochitiet.ngaynhan"),
                LanguageUtil.get(
                        (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                        resourceRequest.getLocale(), "vn.dtt.cmon.report.baocaochitiet.ngaytra"),
                "Tnh trng\n h s",
                LanguageUtil.get(
                        (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                        resourceRequest.getLocale(), "vn.dtt.cmon.report.baocaochitiet.ghichu") };
        int[] width = { 1200, 3750, 3750, 4200, 3650, 3650, 3650, 3650, 3750, 3750, 3750, 3750, 3750 };

        String[] listloaihoso = null;
        if (Validator.isNotNull(ParamUtil.getParameterValues(resourceRequest, "chon_hoso"))) {
            listloaihoso = ParamUtil.getParameterValues(resourceRequest, "chon_hoso");
        }
        String ngaynhan_tungay = "";
        if (Validator.isNotNull(ParamUtil.getString(resourceRequest, "ngaynhan_tungay"))) {
            ngaynhan_tungay = ParamUtil.getString(resourceRequest, "ngaynhan_tungay");
        }
        String ngaynhan_denngay = "";
        if (Validator.isNotNull(ParamUtil.getString(resourceRequest, "ngaynhan_denngay"))) {
            ngaynhan_denngay = ParamUtil.getString(resourceRequest, "ngaynhan_denngay");
        }
        String trangthai_hoso = "";
        if (Validator.isNotNull(ParamUtil.getString(resourceRequest, "trangthai_hoso"))) {
            trangthai_hoso = ParamUtil.getString(resourceRequest, "trangthai_hoso");
        }
        String tinhtrang = "";
        if (Validator.isNotNull(ParamUtil.getString(resourceRequest, "tinhtrang_hoso"))) {
            tinhtrang = ParamUtil.getString(resourceRequest, "tinhtrang_hoso");
        }
        String loaidangky = "";
        if (Validator.isNotNull(ParamUtil.getString(resourceRequest, "loaidangky_hoso"))) {
            loaidangky = ParamUtil.getString(resourceRequest, "loaidangky_hoso");
        }
        String ten_donvi = "";
        if (Validator.isNotNull(ParamUtil.getString(resourceRequest, "chon_donvi"))) {
            ten_donvi = ParamUtil.getString(resourceRequest, "chon_donvi");
        }
        List<Baocaochitiet> list = BaocaochitietLocalServiceUtil.hienthiDanhsachBaocao(listloaihoso,
                ngaynhan_tungay, ngaynhan_denngay, trangthai_hoso, tinhtrang, loaidangky, QueryUtil.ALL_POS,
                QueryUtil.ALL_POS);

        sheet.addMergedRegion(new Region(rowNum, (short) 0, (short) 0, (short) 2));
        HSSFRow Quochieu = sheet.createRow(rowNum);
        HSSFCell Quochieucell_1 = Quochieu.createCell(0);
        HSSFCell Quochieucell_2 = Quochieu.createCell(3);
        Quochieucell_1.setCellStyle(styleQuochieu);
        Quochieucell_2.setCellStyle(styleQuochieu);
        Quochieucell_1.setCellValue(LanguageUtil.get(
                (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                resourceRequest.getLocale(), "vn.dtt.cmon.report.excel.qlc"));
        sheet.addMergedRegion(new Region(rowNum, (short) 3, (short) 0, (short) 12));
        Quochieucell_2.setCellValue(LanguageUtil.get(
                (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                resourceRequest.getLocale(), "vn.dtt.cmon.report.excel.chxhcnvn"));
        rowNum = rowNum + 1;

        sheet.addMergedRegion(new Region(rowNum, (short) 0, (short) 1, (short) 2));
        HSSFRow Row_2 = sheet.createRow(rowNum);
        //HSSFCell Quan = Row_2.createCell(0);
        //Quan.setCellValue(LanguageUtil.get((PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG) ,resourceRequest.getLocale(),"vn.dtt.cmon.report.excel.qlc"));
        //Quan.setCellStyle(styleRow2);

        sheet.addMergedRegion(new Region(rowNum, (short) 3, (short) 1, (short) 12));
        HSSFCell Tieungu = Row_2.createCell(3);
        Tieungu.setCellValue(LanguageUtil.get(
                (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                resourceRequest.getLocale(), "vn.dtt.cmon.report.excel.dltdhp"));
        Tieungu.setCellStyle(styleRow2);
        rowNum = rowNum + 1;

        Calendar dateTime = Calendar.getInstance();
        SimpleDateFormat ngay_format = new SimpleDateFormat("dd");
        SimpleDateFormat thang_format = new SimpleDateFormat("MM");
        SimpleDateFormat nam_format = new SimpleDateFormat("yyyy");
        Date time = dateTime.getTime();
        String day = ngay_format.format(time);
        String month = thang_format.format(time);
        String year = nam_format.format(time);
        sheet.addMergedRegion(new Region(rowNum, (short) 0, (short) 2, (short) 10));
        HSSFRow Row_3 = sheet.createRow(rowNum);
        HSSFCell ngaythang = Row_3.createCell(0);
        ngaythang.setCellValue(ten_donvi + ", "
                + LanguageUtil.get(
                        (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                        resourceRequest.getLocale(), "vn.dtt.cmon.report.excel.ngay")
                + " " + day + " "
                + LanguageUtil
                        .get((PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                                resourceRequest.getLocale(), "vn.dtt.cmon.report.excel.thang")
                + " " + month + " "
                + LanguageUtil.get(
                        (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                        resourceRequest.getLocale(), "vn.dtt.cmon.report.excel.nam")
                + " " + year);
        ngaythang.setCellStyle(ngaythang_style);
        rowNum = rowNum + 1;

        sheet.addMergedRegion(new Region(rowNum, (short) 0, rowNum, (short) 10));
        HSSFRow titleRow = sheet.createRow(rowNum);
        HSSFCell titleCell = titleRow.createCell(0);
        titleCell.setCellValue(LanguageUtil.get(
                (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                resourceRequest.getLocale(), "vn.dtt.cmon.report.baocaochitiet.tieude"));
        titleCell.setCellStyle(styleTitle);
        if (ngaynhan_tungay != "" && ngaynhan_denngay != "") {
            rowNum = rowNum + 1;
            sheet.addMergedRegion(new Region(rowNum, (short) 0, rowNum, (short) 10));
            HSSFRow TimeReportRow = sheet.createRow(rowNum);
            HSSFCell TimeReportCell = TimeReportRow.createCell(0);
            TimeReportCell.setCellValue(LanguageUtil.get(
                    (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                    resourceRequest.getLocale(), "vn.dtt.cmon.report.excel.tungay") + ": " + ngaynhan_tungay
                    + " "
                    + LanguageUtil.get(
                            (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                            resourceRequest.getLocale(), "vn.dtt.cmon.report.excel.denngay")
                    + ": " + ngaynhan_denngay);
            TimeReportCell.setCellStyle(style_tungay_denngay);
        } else if (ngaynhan_tungay != "" && ngaynhan_denngay == "") {
            rowNum = rowNum + 1;
            ngaynhan_denngay = "01/12/" + ngaynhan_tungay.substring(6, 10);
            sheet.addMergedRegion(new Region(rowNum, (short) 0, rowNum, (short) 10));
            HSSFRow TimeReportRow = sheet.createRow(rowNum);
            HSSFCell TimeReportCell = TimeReportRow.createCell(0);
            TimeReportCell.setCellValue(LanguageUtil.get(
                    (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                    resourceRequest.getLocale(), "vn.dtt.cmon.report.excel.tungay") + ": " + ngaynhan_tungay
                    + " "
                    + LanguageUtil.get(
                            (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                            resourceRequest.getLocale(), "vn.dtt.cmon.report.excel.denngay")
                    + ": " + ngaynhan_denngay);
            TimeReportCell.setCellStyle(style_tungay_denngay);
        } else if (ngaynhan_tungay == "" && ngaynhan_denngay != "") {
            rowNum = rowNum + 1;
            ngaynhan_tungay = "01/01/" + ngaynhan_denngay.substring(6, 10);
            sheet.addMergedRegion(new Region(rowNum, (short) 0, rowNum, (short) 10));
            HSSFRow TimeReportRow = sheet.createRow(rowNum);
            HSSFCell TimeReportCell = TimeReportRow.createCell(0);
            TimeReportCell.setCellValue(LanguageUtil.get(
                    (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                    resourceRequest.getLocale(), "vn.dtt.cmon.report.excel.tungay") + ": " + ngaynhan_tungay
                    + " "
                    + LanguageUtil.get(
                            (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                            resourceRequest.getLocale(), "vn.dtt.cmon.report.excel.denngay")
                    + ": " + ngaynhan_denngay);
            TimeReportCell.setCellStyle(style_tungay_denngay);
        } else {
            rowNum = rowNum + 1;
            ngaynhan_tungay = "01/01/" + year;
            ngaynhan_denngay = "01/12/" + year;
            sheet.addMergedRegion(new Region(rowNum, (short) 0, rowNum, (short) 10));
            HSSFRow TimeReportRow = sheet.createRow(rowNum);
            HSSFCell TimeReportCell = TimeReportRow.createCell(0);
            TimeReportCell.setCellValue(LanguageUtil.get(
                    (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                    resourceRequest.getLocale(), "vn.dtt.cmon.report.excel.tungay") + ": " + ngaynhan_tungay
                    + " "
                    + LanguageUtil.get(
                            (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                            resourceRequest.getLocale(), "vn.dtt.cmon.report.excel.denngay")
                    + ": " + ngaynhan_denngay);
            TimeReportCell.setCellStyle(style_tungay_denngay);
        }
        if (!trangthai_hoso.equals("")) {
            rowNum = rowNum + 1;
            sheet.addMergedRegion(new Region(rowNum, (short) 0, rowNum, (short) 10));
            HSSFRow TimeReportRow = sheet.createRow(rowNum);
            HSSFCell TimeReportCell = TimeReportRow.createCell(0);
            TimeReportCell
                    .setCellValue("("
                            + LanguageUtil.get(
                                    (PortletConfig) resourceRequest
                                            .getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                                    resourceRequest.getLocale(), "vn.dtt.cmon.report.baocaochitiet.trangthai")
                            + ": " + TrangThaiHoSoLocalServiceUtil
                                    .getTrangThaiHoSo(Long.parseLong(trangthai_hoso)).getTrangThai()
                            + ")");
            TimeReportCell.setCellStyle(style_trangthai);
        }
        rowNum = rowNum + 3;
        HSSFRow headerRow = (HSSFRow) sheet.createRow(rowNum);
        int colNum = 0;
        for (int i = 0; i < header.length; i++) {
            HSSFCell cell = headerRow.createCell(colNum);
            cell.setCellStyle(styleHeader);
            cell.setCellValue(header[i]);
            sheet.setColumnWidth(i, width[i]);
            colNum++;
        }
        rowNum++;
        long count = 0;
        if (list != null && list.size() > 0) {
            long linhvucId = 0L;
            for (int a = 0; a < list.size(); a++) {
                String tinh_trang = "";
                if (Validator.isNotNull(list.get(a).getNGAYTRAKETQUA())
                        && Validator.isNotNull(list.get(a).getNGAYHENTRAKETQUA())) {
                    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                    Date ngaytra = df.parse(list.get(a).getNGAYTRAKETQUA());
                    Date ngayhentra = df.parse(list.get(a).getNGAYHENTRAKETQUA());
                    if (ngaytra.before(ngayhentra)) {
                        tinh_trang = "Sm hn";
                    } else if (ngaytra.equals(ngayhentra)) {
                        tinh_trang = "ng hn";
                    } else if (ngaytra.after(ngayhentra)) {
                        tinh_trang = "Tr hn";
                    }
                }
                //khai bao dong va cac cell
                HSSFRow row = (HSSFRow) sheet.createRow(rowNum);
                HSSFCell cell = row.createCell(0);
                HSSFCell cell1 = row.createCell(1);
                HSSFCell cell2 = row.createCell(2);
                HSSFCell cell3 = row.createCell(3);
                HSSFCell cell4 = row.createCell(4);
                HSSFCell cell5 = row.createCell(5);
                HSSFCell cell6 = row.createCell(6);
                HSSFCell cell7 = row.createCell(7);
                HSSFCell cell8 = row.createCell(8);
                HSSFCell cell9 = row.createCell(9);
                HSSFCell cell10 = row.createCell(10);
                HSSFCell cell11 = row.createCell(11);
                HSSFCell cell12 = row.createCell(12);
                //set style cho cac cell
                cell.setCellStyle(styleCellCenter);
                cell1.setCellStyle(styleCellLeft);
                cell2.setCellStyle(styleCellCenter);
                cell3.setCellStyle(styleCellCenter);
                cell4.setCellStyle(styleCellCenter);
                cell5.setCellStyle(styleCellCenter);
                cell6.setCellStyle(styleCellCenter);
                cell7.setCellStyle(styleCellCenter);
                cell8.setCellStyle(styleCellCenter);
                cell9.setCellStyle(styleCellCenter);
                cell10.setCellStyle(styleCellCenter);
                cell11.setCellStyle(styleCellCenter);
                cell12.setCellStyle(styleCellCenter);
                //set gia tri cho cac cell
                cell.setCellValue((a + 1));
                cell1.setCellValue(NhomThuTucHanhChinhLocalServiceUtil
                        .getNhomThuTucHanhChinh(list.get(a).getLINHVUCID()).getTen());
                cell2.setCellValue(list.get(a).getTEN());
                cell3.setCellValue(list.get(a).getMASOHOSO());
                cell4.setCellValue(list.get(a).getTRANGTHAI());
                cell5.setCellValue(list.get(a).getDIACHIHIENNAY());
                cell6.setCellValue(list.get(a).getDIENTHOAIDIDONG());
                cell7.setCellValue(list.get(a).getHOTENNGUOINOPHOSO());
                cell8.setCellValue(list.get(a).getNGUOIDAIDIENPHAPLUAT());
                cell9.setCellValue(list.get(a).getNGAYNHANHOSO());
                cell10.setCellValue(list.get(a).getNGAYTRAKETQUA());
                cell11.setCellValue(tinh_trang);
                cell12.setCellValue(list.get(a).getGHICHU());
                count = a;
                rowNum++;
            }
            count = count + 1;
        } else {
            //khai bao dong va cac cell
            HSSFRow row = (HSSFRow) sheet.createRow(rowNum);
            HSSFCell cell = row.createCell(0);
            HSSFCell cell1 = row.createCell(1);
            HSSFCell cell2 = row.createCell(2);
            HSSFCell cell3 = row.createCell(3);
            HSSFCell cell4 = row.createCell(4);
            HSSFCell cell5 = row.createCell(5);
            HSSFCell cell6 = row.createCell(6);
            HSSFCell cell7 = row.createCell(7);
            HSSFCell cell8 = row.createCell(8);
            HSSFCell cell9 = row.createCell(9);
            HSSFCell cell10 = row.createCell(10);
            HSSFCell cell11 = row.createCell(11);
            HSSFCell cell12 = row.createCell(12);
            //set style cho cac cell
            cell.setCellStyle(styleCellCenter);
            cell1.setCellStyle(styleCellLeft);
            cell2.setCellStyle(styleCellCenter);
            cell3.setCellStyle(styleCellCenter);
            cell4.setCellStyle(styleCellCenter);
            cell5.setCellStyle(styleCellCenter);
            cell6.setCellStyle(styleCellCenter);
            cell7.setCellStyle(styleCellCenter);
            cell8.setCellStyle(styleCellCenter);
            cell9.setCellStyle(styleCellCenter);
            cell10.setCellStyle(styleCellCenter);
            cell11.setCellStyle(styleCellCenter);
            cell12.setCellStyle(styleCellCenter);
            //set gia tri cho cac cell
            cell.setCellValue("");
            cell1.setCellValue("");
            cell2.setCellValue("");
            cell3.setCellValue("");
            cell4.setCellValue("");
            cell5.setCellValue("");
            cell6.setCellValue("");
            cell7.setCellValue("");
            cell8.setCellValue("");
            cell9.setCellValue("");
            cell10.setCellValue("");
            cell11.setCellValue("");
            cell12.setCellValue("");
            count = 0;
            rowNum++;
        }
        sheet.addMergedRegion(new Region(rowNum, (short) 0, rowNum, (short) 11));
        HSSFRow rowTong = (HSSFRow) sheet.createRow(rowNum);
        HSSFCell cell = rowTong.createCell(0);
        HSSFCell cell1 = rowTong.createCell(1);
        HSSFCell cell2 = rowTong.createCell(2);
        HSSFCell cell3 = rowTong.createCell(3);
        HSSFCell cell4 = rowTong.createCell(4);
        HSSFCell cell5 = rowTong.createCell(5);
        HSSFCell cell6 = rowTong.createCell(6);
        HSSFCell cell7 = rowTong.createCell(7);
        HSSFCell cell8 = rowTong.createCell(8);
        HSSFCell cell9 = rowTong.createCell(9);
        HSSFCell cell10 = rowTong.createCell(10);
        HSSFCell cell11 = rowTong.createCell(11);
        HSSFCell cell12 = rowTong.createCell(12);
        //set style cho cac cell
        cell.setCellStyle(titleTongStyle);
        cell1.setCellStyle(styleCellCenter);
        cell2.setCellStyle(styleCellCenter);
        cell3.setCellStyle(styleCellCenter);
        cell4.setCellStyle(styleCellCenter);
        cell5.setCellStyle(styleCellCenter);
        cell6.setCellStyle(styleCellCenter);
        cell7.setCellStyle(styleCellCenter);
        cell8.setCellStyle(styleCellCenter);
        cell9.setCellStyle(styleCellCenter);
        cell10.setCellStyle(styleCellCenter);
        cell11.setCellStyle(styleCellCenter);
        cell12.setCellStyle(styleCellCenter);
        //set value for cells of rowtong 
        cell.setCellValue(LanguageUtil.get(
                (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                resourceRequest.getLocale(), "vn.dtt.cmon.report.baocaochitiet.tonghoso"));
        cell12.setCellValue(count);

        rowNum = rowNum + 2;
        sheet.addMergedRegion(new Region(rowNum, (short) 1, rowNum, (short) 2));
        HSSFRow nguoilap_baocaoRow = sheet.createRow(rowNum);
        HSSFCell nguoilap_Cell = nguoilap_baocaoRow.createCell(1);
        nguoilap_Cell.setCellValue(LanguageUtil.get(
                (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                resourceRequest.getLocale(), "vn.dtt.cmon.report.excel.nguoilap"));
        nguoilap_Cell.setCellStyle(styleTitle);

        sheet.addMergedRegion(new Region(rowNum, (short) 7, rowNum, (short) 10));
        HSSFCell title_nguoibaocao_Cell = nguoilap_baocaoRow.createCell(7);
        title_nguoibaocao_Cell.setCellValue(LanguageUtil.get(
                (PortletConfig) resourceRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG),
                resourceRequest.getLocale(), "vn.dtt.cmon.report.excel.nguoibaocao"));
        title_nguoibaocao_Cell.setCellStyle(styleTitle);

        rowNum = rowNum + 4;

        if (Validator.isNotNull(resourceRequest.getAttribute("THEME_DISPLAY"))) {
            CongChuc congchuc = new CongChucImpl();
            ThemeDisplay themdisplay = (ThemeDisplay) resourceRequest.getAttribute("THEME_DISPLAY");
            User user = themdisplay.getUser();
            TaiKhoanNguoiDung taikhoan = new TaiKhoanNguoiDungImpl();
            taikhoan = TaiKhoanNguoiDungLocalServiceUtil.findByTaiKhoanNguoiDungId(user.getUserId());
            congchuc = ActionUtil.getCongchucByTaiKhoanNguoiDungId(taikhoan.getId());

            sheet.addMergedRegion(new Region(rowNum, (short) 1, rowNum, (short) 2));
            HSSFRow nguoi_baocaoRow = sheet.createRow(rowNum);
            HSSFCell nguoibaocao_Cell = nguoi_baocaoRow.createCell(1);
            nguoibaocao_Cell.setCellValue(congchuc.getHoVaTen());
            nguoibaocao_Cell.setCellStyle(styleTitle);
        }

        resourceResponse.setContentType("application/vnd.ms-excel;charset=utf-8");
        resourceResponse.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");
        resourceResponse.addProperty(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=Baocaochitiet.xls");
        OutputStream out = resourceResponse.getPortletOutputStream();
        workbook.write(out);
        out.flush();
        out.close();
    } catch (Exception e) {
    }
}

From source file:org.openmicroscopy.shoola.util.file.ExcelWriter.java

License:Open Source License

/** Creates the fonts that are going to be used in the styles. */
private void createFonts() {
    HSSFFont font;
    /* Hyperlink font. */
    font = workbook.createFont();//w  ww  . j a v  a2  s . c o  m
    font.setUnderline(HSSFFont.U_SINGLE);
    font.setColor(HSSFColor.BLUE.index);
    fontMap.put(HYPERLINK, font);

    /* Default Font. */
    font = workbook.createFont();
    fontMap.put(DEFAULT, font);

    /* Bold Font. */
    font = workbook.createFont();
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    fontMap.put(BOLD_DEFAULT, font);

    /* Underline Font. */
    font = workbook.createFont();
    font.setUnderline(HSSFFont.U_SINGLE);
    fontMap.put(UNDERLINE_DEFAULT, font);

    /* Italic Font. */
    font = workbook.createFont();
    font.setItalic(true);
    fontMap.put(ITALIC_DEFAULT, font);

    /* Italic, underline Font. */
    font = workbook.createFont();
    font.setItalic(true);
    font.setUnderline(HSSFFont.U_SINGLE);
    fontMap.put(ITALIC_UNDERLINE_DEFAULT, font);

    /* Italic, bold Font. */
    font = workbook.createFont();
    font.setItalic(true);
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    fontMap.put(BOLD_ITALIC_DEFAULT, font);

    /* Italic, bold, underline Font. */
    font = workbook.createFont();
    font.setItalic(true);
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    font.setUnderline(HSSFFont.U_SINGLE);
    fontMap.put(BOLD_ITALIC_UNDERLINE_DEFAULT, font);

    /* Italic, bold, underline Font. */
    font = workbook.createFont();
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    font.setUnderline(HSSFFont.U_SINGLE);
    fontMap.put(BOLD_UNDERLINE_DEFAULT, font);

    /* 12 point font. */
    font = workbook.createFont();
    font.setFontHeightInPoints((short) 12);
    fontMap.put(PLAIN_12, font);

    /* 14 point font. */
    font = workbook.createFont();
    font.setFontHeightInPoints((short) 14);
    fontMap.put(PLAIN_14, font);

    /* 14 point font. */
    font = workbook.createFont();
    font.setFontHeightInPoints((short) 14);
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    fontMap.put(BOLD_14, font);

    /* 18 point font. */
    font = workbook.createFont();
    font.setFontHeightInPoints((short) 18);
    fontMap.put(PLAIN_18, font);

    /* 18 point font. */
    font = workbook.createFont();
    font.setFontHeightInPoints((short) 18);
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    fontMap.put(BOLD_18, font);
}

From source file:org.orbeon.oxf.util.XLSUtils.java

License:Open Source License

public static void copyFont(HSSFFont destination, HSSFFont source) {
    destination.setBoldweight(source.getBoldweight());
    destination.setColor(source.getColor());
    destination.setFontHeight(source.getFontHeight());
    destination.setFontHeightInPoints(source.getFontHeightInPoints());
    destination.setFontName(source.getFontName());
    destination.setItalic(source.getItalic());
    destination.setStrikeout(source.getStrikeout());
    destination.setTypeOffset(source.getTypeOffset());
    destination.setUnderline(source.getUnderline());
}

From source file:org.tentackle.ui.FormTableUtilityPopup.java

License:Open Source License

/**
 * Converts the table to an excel spreadsheet.
 * @param file the output file/*from   w  w w  .  j  av a2 s . c om*/
 * @param onlySelected true if export only selected rows
 * @throws IOException if export failed
 */
public void excel(File file, boolean onlySelected) throws IOException {

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();

    TableModel model = table.getModel();
    TableColumnModel columnModel = table.getColumnModel();

    int[] selectedRows = onlySelected ? table.getSelectedRows() : new int[] {};

    int rows = onlySelected ? selectedRows.length : model.getRowCount(); // number of data rows
    int cols = columnModel.getColumnCount(); // number of data columns

    short srow = 0; // current spreadsheet row

    // local copies cause might be changed
    String xTitle = this.title;
    String xIntro = this.intro;

    if (xTitle == null) {
        // get default from window title
        Window parent = FormHelper.getParentWindow(table);
        try {
            // paint page-title
            xTitle = ((FormWindow) parent).getTitle();
        } catch (Exception e) {
            xTitle = null;
        }
    }
    if (xTitle != null) {
        HSSFRow row = sheet.createRow(srow);
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        HSSFCellStyle cs = wb.createCellStyle();
        cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cs.setFont(font);
        HSSFCell cell = row.createCell(0);
        cell.setCellStyle(cs);
        cell.setCellValue(new HSSFRichTextString(xTitle));
        // region rowFrom, colFrom, rowTo, colTo
        sheet.addMergedRegion(new CellRangeAddress(0, srow, 0, cols - 1));
        srow++;
    }

    if (xIntro != null || onlySelected) {
        HSSFRow row = sheet.createRow(srow);
        HSSFCell cell = row.createCell(0);
        HSSFCellStyle cs = wb.createCellStyle();
        cs.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        cs.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        cs.setWrapText(true);
        cell.setCellStyle(cs);
        if (onlySelected) {
            if (xIntro == null) {
                xIntro = "";
            } else {
                xIntro += ", ";
            }
            xIntro += Locales.bundle.getString("<nur_selektierte_Zeilen>");
        }
        cell.setCellValue(new HSSFRichTextString(xIntro));
        sheet.addMergedRegion(new CellRangeAddress(srow, srow + 2, 0, cols - 1));
        srow += 3;
    }

    // column headers
    boolean isAbstractFormTableModel = model instanceof AbstractFormTableModel;
    srow++; // always skip one line
    HSSFRow row = sheet.createRow(srow);
    HSSFFont font = wb.createFont();
    font.setItalic(true);
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    HSSFCellStyle cs = wb.createCellStyle();
    cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    cs.setFont(font);
    for (int c = 0; c < cols; c++) {
        HSSFCell cell = row.createCell(c);
        cell.setCellValue(new HSSFRichTextString(isAbstractFormTableModel
                ? ((AbstractFormTableModel) model)
                        .getDisplayedColumnName(columnModel.getColumn(c).getModelIndex())
                : model.getColumnName(columnModel.getColumn(c).getModelIndex())));
        cell.setCellStyle(cs);
    }
    srow++;

    // default cell-style for date
    HSSFCellStyle dateStyle = wb.createCellStyle();
    dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));

    // cellstyles for numbers
    List<HSSFCellStyle> numberStyles = new ArrayList<HSSFCellStyle>();
    HSSFDataFormat format = wb.createDataFormat();

    for (int r = 0; r < rows; r++) {

        int modelRow = onlySelected ? selectedRows[r] : r;

        row = sheet.createRow(srow + (short) r);

        for (int i = 0; i < cols; i++) {

            int c = columnModel.getColumn(i).getModelIndex();

            Object value = model.getValueAt(modelRow, c);

            HSSFCell cell = row.createCell(i);

            if (value instanceof Boolean) {
                cell.setCellValue(((Boolean) value).booleanValue());
            } else if (value instanceof BMoney) {
                BMoney money = (BMoney) value;
                cell.setCellValue(money.doubleValue());

                String fmt = "#,##0";
                if (money.scale() > 0) {
                    fmt += ".";
                    for (int j = 0; j < money.scale(); j++) {
                        fmt += "0";
                    }
                }
                // create format
                short fmtIndex = format.getFormat(fmt);

                // check if there is already a cellstyle with this scale
                Iterator<HSSFCellStyle> iter = numberStyles.iterator();
                boolean found = false;
                while (iter.hasNext()) {
                    cs = iter.next();
                    if (cs.getDataFormat() == fmtIndex) {
                        // reuse that
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    // create a new style
                    cs = wb.createCellStyle();
                    cs.setDataFormat(fmtIndex);
                    numberStyles.add(cs);
                }
                cell.setCellStyle(cs);
            } else if (value instanceof Number) {
                cell.setCellValue(((Number) value).doubleValue());
            } else if (value instanceof Date) {
                cell.setCellValue((Date) value);
                cell.setCellStyle(dateStyle);
            } else if (value instanceof GregorianCalendar) {
                cell.setCellValue((GregorianCalendar) value);
                cell.setCellStyle(dateStyle);
            } else if (value != null) {
                cell.setCellValue(new HSSFRichTextString(value.toString()));
            }
        }
    }

    // set the width for each column
    for (int c = 0; c < cols; c++) {
        short width = (short) (columnModel.getColumn(c).getWidth() * 45); // is a reasonable value
        sheet.setColumnWidth(c, width);
    }

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream(file);
    wb.write(fileOut);
    fileOut.close();

    // open Excel
    URLHelper.openURL(file.getPath());
}

From source file:org.waterforpeople.mapping.dataexport.SurveyFormExporter.java

License:Open Source License

/**
 * Writes the survey as an XLS document// w  w w  .ja v a  2 s .c  o  m
 */
private void writeSurvey(String title, File fileName, List<QuestionGroupDto> groupList,
        Map<QuestionGroupDto, List<QuestionDto>> questions) throws Exception {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();

    sheet.setColumnWidth(0, COL_WIDTH);
    sheet.setColumnWidth(1, COL_WIDTH);
    HSSFCellStyle headerStyle = wb.createCellStyle();
    headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    HSSFFont headerFont = wb.createFont();
    headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    headerStyle.setFont(headerFont);

    HSSFCellStyle questionStyle = wb.createCellStyle();
    questionStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
    questionStyle.setWrapText(true);

    HSSFCellStyle depStyle = wb.createCellStyle();
    depStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    HSSFFont depFont = wb.createFont();
    depFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    depFont.setItalic(true);
    depStyle.setFont(depFont);

    int curRow = 0;
    HSSFRow row = sheet.createRow(curRow++);
    sheet.addMergedRegion(new CellRangeAddress(curRow - 1, curRow - 1, 0, 1));
    createCell(row, 0, title, headerStyle);
    row = sheet.createRow(curRow++);
    createCell(row, 0, QUESTION_HEADER, headerStyle);
    createCell(row, 1, RESPONSE_HEADER, headerStyle);

    Long count = 1L;
    if (questions != null) {
        for (int i = 0; i < groupList.size(); i++) {
            HSSFRow groupHeaderRow = sheet.createRow(curRow++);
            sheet.addMergedRegion(new CellRangeAddress(curRow - 1, curRow - 1, 0, 1));
            createCell(groupHeaderRow, 0, groupList.get(i).getDisplayName(), headerStyle);

            for (QuestionDto q : questions.get(groupList.get(i))) {
                int questionStartRow = curRow;
                HSSFRow tempRow = sheet.createRow(curRow++);
                if (q.getQuestionDependency() != null) {
                    // if there is a dependency, add a row about not
                    // answering unless the dependency is satisfied
                    sheet.addMergedRegion(new CellRangeAddress(curRow - 1, curRow - 1, 0, 1));
                    Long qNum = idToNumberMap.get(q.getQuestionDependency().getQuestionId());
                    createCell(tempRow, 0,
                            DEP_HEAD + q.getQuestionDependency().getAnswerValue() + DEP_HEAD_TO + "Q" + qNum,
                            depStyle);
                    tempRow = sheet.createRow(curRow++);
                    questionStartRow = curRow;
                }
                createCell(tempRow, 0, (count++) + ". " + formText(q.getText(), q.getTranslationMap()),
                        questionStyle);
                if (q.getOptionContainerDto() != null && q.getOptionContainerDto().getOptionsList() != null) {
                    for (QuestionOptionDto opt : q.getOptionContainerDto().getOptionsList()) {
                        tempRow = sheet.createRow(curRow++);
                        createCell(tempRow, 1, formText(opt.getText(), opt.getTranslationMap()) + SMALL_BLANK,
                                null);
                    }
                    sheet.addMergedRegion(new CellRangeAddress(questionStartRow, curRow - 1, 0, 0));
                } else {
                    createCell(tempRow, 1, BLANK, null);
                }
            }
        }
    }

    FileOutputStream fileOut = new FileOutputStream(fileName);
    wb.write(fileOut);
    fileOut.close();
}

From source file:poi.hssf.usermodel.examples.OfficeDrawing.java

License:Apache License

private static void drawSheet4(HSSFSheet sheet4, HSSFWorkbook wb) {
    // Create the drawing patriarch.  This is the top level container for
    // all shapes. This will clear out any existing shapes for that sheet.
    HSSFPatriarch patriarch = sheet4.createDrawingPatriarch();

    // Create a couple of textboxes
    HSSFTextbox textbox1 = patriarch/*from   w w w  . j a va  2 s .  c o m*/
            .createTextbox(new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 2, 2));
    textbox1.setString(new HSSFRichTextString("This is a test"));
    HSSFTextbox textbox2 = patriarch
            .createTextbox(new HSSFClientAnchor(0, 0, 900, 100, (short) 3, 3, (short) 3, 4));
    textbox2.setString(new HSSFRichTextString("Woo"));
    textbox2.setFillColor(200, 0, 0);
    textbox2.setLineStyle(HSSFSimpleShape.LINESTYLE_DOTGEL);

    // Create third one with some fancy font styling.
    HSSFTextbox textbox3 = patriarch
            .createTextbox(new HSSFClientAnchor(0, 0, 900, 100, (short) 4, 4, (short) 5, 4 + 1));
    HSSFFont font = wb.createFont();
    font.setItalic(true);
    font.setUnderline(HSSFFont.U_DOUBLE);
    HSSFRichTextString string = new HSSFRichTextString("Woo!!!");
    string.applyFont(2, 5, font);
    textbox3.setString(string);
    textbox3.setFillColor(0x08000030);
    textbox3.setLineStyle(HSSFSimpleShape.LINESTYLE_NONE); // no line around the textbox.
    textbox3.setNoFill(true); // make it transparent
}

From source file:poi.hssf.usermodel.examples.WorkingWithFonts.java

License:Apache 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(1);//  w  ww.  j av  a2s  .  c  o m

    // Create a new font and alter it.
    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short) 24);
    font.setFontName("Courier New");
    font.setItalic(true);
    font.setStrikeout(true);

    // Fonts are set into a style so create a new one to use.
    HSSFCellStyle style = wb.createCellStyle();
    style.setFont(font);

    // Create a cell and put a value in it.
    HSSFCell cell = row.createCell(1);
    cell.setCellValue("This is a test of fonts");
    cell.setCellStyle(style);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
}

From source file:reports.barCharts.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from  ww w .j  av a 2  s .co m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {
        dbConn conn = new dbConn();

        wb = new HSSFWorkbook();

        HSSFSheet shet2 = null;

        String year = "";
        String site = "";
        String period = "";
        String cbo = "";
        String startdate = "2015-01-01";
        String enddate = "2015-03-30";

        startdate = request.getParameter("startdate");
        enddate = request.getParameter("enddate");

        //            year=request.getParameter("year");
        //site=request.getParameter("sitecbo");
        //period=request.getParameter("period");
        //cbo=request.getParameter("staffcbo");

        String sitename = "";
        String cboname = "";

        //begin a loop that will create as many reports as possible

        HSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short) 12);
        font.setFontName("Cambria");
        //    font.setItalic(true);
        font.setBoldweight((short) 02);
        font.setColor(HSSFColor.BLACK.index);
        CellStyle style = wb.createCellStyle();
        style.setFont(font);
        style.setWrapText(true);
        style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);

        style.setAlignment(style.ALIGN_CENTER);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        //%%%%%%%%%%%%%%%%HEADER FONTS AND COLORATION%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        HSSFFont font_header = wb.createFont();
        font_header.setFontHeightInPoints((short) 10);
        font_header.setFontName("Eras Bold ITC");
        //    font.setItalic(true);
        font_header.setBoldweight((short) 05);
        font_header.setColor(HSSFColor.BLACK.index);
        CellStyle style_header = wb.createCellStyle();
        style_header.setFont(font_header);
        style_header.setWrapText(true);
        style_header.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
        style_header.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style_header.setAlignment(style_header.ALIGN_CENTER);

        //            style_header.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //            style_header.setBorderTop(HSSFCellStyle.BORDER_THIN);
        //            style_header.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //            style_header.setBorderLeft(HSSFCellStyle.BORDER_THIN);

        //%%%%%%%%%%%%%%%%%%%%%%%%%DATA FONT%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        //font data
        HSSFFont datafont = wb.createFont();
        datafont.setBoldweight((short) 03);
        datafont.setColor(HSSFColor.BLACK.index);
        datafont.setFontHeightInPoints((short) 10);
        datafont.setFontName("Cambria");
        datafont.setItalic(true);

        //bold font 
        HSSFFont bolfont = wb.createFont();

        bolfont.setBoldweight((short) 05);
        bolfont.setColor(HSSFColor.BLACK.index);
        bolfont.setFontHeightInPoints((short) 12);
        bolfont.setFontName("Cambria");

        //=========================ROW STYLE===============================

        HSSFCellStyle rowstyle = wb.createCellStyle();
        rowstyle.setWrapText(true);

        //=======INNER DATA STYLING===========================

        CellStyle innerdata_style = wb.createCellStyle();
        innerdata_style.setFont(datafont);
        innerdata_style.setWrapText(true);
        innerdata_style.setAlignment(innerdata_style.ALIGN_CENTER);
        innerdata_style.setFillForegroundColor(HSSFColor.WHITE.index);
        innerdata_style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        //            innerdata_style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //            innerdata_style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        //            innerdata_style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //            innerdata_style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        innerdata_style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        CellStyle lastcellrighborder = wb.createCellStyle();
        lastcellrighborder.setFont(datafont);
        lastcellrighborder.setWrapText(true);
        lastcellrighborder.setAlignment(lastcellrighborder.ALIGN_CENTER);
        lastcellrighborder.setFillForegroundColor(HSSFColor.WHITE.index);
        lastcellrighborder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        //            lastcellrighborder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //            lastcellrighborder.setBorderTop(HSSFCellStyle.BORDER_THIN);
        lastcellrighborder.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //            lastcellrighborder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        lastcellrighborder.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        CellStyle innerdata_style2 = wb.createCellStyle();
        innerdata_style2.setFont(bolfont);
        innerdata_style2.setWrapText(true);
        innerdata_style2.setAlignment(innerdata_style.ALIGN_LEFT);
        innerdata_style2.setFillForegroundColor(HSSFColor.WHITE.index);
        innerdata_style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        //            innerdata_style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //            innerdata_style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        //            innerdata_style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //            innerdata_style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        innerdata_style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        CellStyle orangestyle = wb.createCellStyle();
        orangestyle.setFont(bolfont);
        orangestyle.setWrapText(true);
        orangestyle.setAlignment(orangestyle.ALIGN_CENTER);
        orangestyle.setFillForegroundColor(HSSFColor.ORANGE.index);
        orangestyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        //          innerdata_style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //          innerdata_style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        //          innerdata_style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //          innerdata_style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        orangestyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        //Code colors

        CellStyle lg = wb.createCellStyle();
        lg.setFont(bolfont);
        lg.setWrapText(true);
        lg.setAlignment(lg.ALIGN_CENTER);
        lg.setFillForegroundColor(HSSFColor.GREEN.index);
        lg.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        lg.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        lg.setBorderTop(HSSFCellStyle.BORDER_THIN);
        lg.setBorderRight(HSSFCellStyle.BORDER_THIN);
        lg.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        lg.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        CellStyle Y = wb.createCellStyle();
        Y.setFont(bolfont);
        Y.setWrapText(true);
        Y.setAlignment(Y.ALIGN_CENTER);
        Y.setFillForegroundColor(HSSFColor.WHITE.index);
        Y.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        Y.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        Y.setBorderTop(HSSFCellStyle.BORDER_THIN);
        Y.setBorderRight(HSSFCellStyle.BORDER_THIN);
        Y.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        Y.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        CellStyle R = wb.createCellStyle();
        R.setFont(bolfont);
        R.setWrapText(true);
        R.setAlignment(R.ALIGN_CENTER);
        R.setFillForegroundColor(HSSFColor.RED.index);
        R.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        R.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        R.setBorderTop(HSSFCellStyle.BORDER_THIN);
        R.setBorderRight(HSSFCellStyle.BORDER_THIN);
        R.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        R.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        //=======INNER LEFT DATA STYLING===========================

        CellStyle binnerdata_style2 = wb.createCellStyle();
        binnerdata_style2.setFont(datafont);
        binnerdata_style2.setWrapText(true);
        binnerdata_style2.setAlignment(binnerdata_style2.ALIGN_LEFT);
        binnerdata_style2.setFillForegroundColor(HSSFColor.WHITE.index);
        binnerdata_style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        binnerdata_style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        binnerdata_style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
        binnerdata_style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
        binnerdata_style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        binnerdata_style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        //create a header

        //=======================Domainname styles
        CellStyle dnamestyle = wb.createCellStyle();
        dnamestyle.setFont(bolfont);
        dnamestyle.setWrapText(true);
        dnamestyle.setAlignment(dnamestyle.ALIGN_LEFT);
        dnamestyle.setFillForegroundColor(HSSFColor.WHITE.index);
        dnamestyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        dnamestyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        dnamestyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        dnamestyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        dnamestyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        dnamestyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        shet2 = wb.createSheet("Column Charts Per Cbo");
        shet2.setColumnWidth(0, 12000);
        shet2.setColumnWidth(1, 12000);
        shet2.setColumnWidth(2, 4000);
        shet2.setColumnWidth(3, 10000);
        shet2.setColumnWidth(4, 5000);
        shet2.setColumnWidth(5, 5000);
        shet2.setColumnWidth(6, 5000);
        shet2.setColumnWidth(7, 5000);
        shet2.setColumnWidth(8, 5000);
        shet2.setColumnWidth(9, 5000);
        shet2.setColumnWidth(10, 5000);
        shet2.setColumnWidth(11, 5000);
        shet2.setColumnWidth(12, 5000);
        shet2.setColumnWidth(13, 5000);

        //11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111           
        //11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111           
        //11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111           
        //11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111  

        String gettables = "SELECT avg(value) as domainvalue,domain_totals.domainid as domainid,domain_name, section_name,cbo,avg(aggregate_sum) as aggregate_sum FROM ovc_lip.domain_totals join (sites join cbo on sites.cbo_id=cbo.cboid) on domain_totals.site=sites.site_id join ( domains join sections on domains.section_id=sections.section_id ) on domain_totals.domainid=domains.domain_id where  date between '"
                + startdate + "' and '" + enddate + "' group by cbo.cboid,domainid order by cbo,domainid";

        System.out.println(gettables);
        conn.rs = conn.st.executeQuery(gettables);
        int rwcount = 0;
        int rowcountcopy = 0;
        String tableheaders[] = { "Section", "Domain", "% Overall Achievement", "Column chart" };

        HSSFRow rwx = null;
        int monitorrows = 0;
        int secAcopy = 0;
        int secBcopy = 0;

        HSSFCell celx = null;
        int noofcols = 3;
        boolean isrow1 = true;
        while (conn.rs.next()) {
            //if the section has changed
            monitorrows++;
            String domainid = conn.rs.getString("domainid");
            float domainvalue = conn.rs.getFloat("domainvalue");
            domainvalue = domainvalue * 100;
            domainvalue = Math.round(domainvalue);
            float totalsum = conn.rs.getFloat("aggregate_sum");
            int dmn = (int) domainvalue;
            totalsum = Math.round(totalsum);
            //determine the cell to print data on
            int ttlsm = (int) totalsum;
            int hearderheight = 40;
            //if its the first row in each 
            if (isrow1) {
                isrow1 = false;
                rwx = shet2.createRow(rwcount);

                HSSFCell headercel = rwx.createCell(0);
                headercel.setCellValue(conn.rs.getString("cbo"));
                headercel.setCellStyle(style);
                rwx.setHeightInPoints(hearderheight);

                //create a blank
                HSSFCell cel = null;

                for (int b = 1; b < tableheaders.length; b++) {
                    cel = rwx.createCell(b);
                    cel.setCellValue("");
                    cel.setCellStyle(style);
                }
                //now merge the header cell
                shet2.addMergedRegion(new CellRangeAddress(rwcount, rwcount, 0, tableheaders.length - 1));
                rwcount++;

                //now create the header part

                HSSFRow headerrw = shet2.createRow(rwcount);
                rwx.setHeightInPoints(hearderheight);
                for (int b = 0; b < tableheaders.length; b++) {
                    HSSFCell cel1 = headerrw.createCell(b);
                    cel1.setCellValue(tableheaders[b]);
                    cel1.setCellStyle(style);
                }

                rwcount++;
            }

            //create the section part

            HSSFRow rw = shet2.createRow(rwcount);

            rw.setHeightInPoints(25);
            //column one --- section

            HSSFCell seccell = rw.createCell(0);
            seccell.setCellValue(conn.rs.getString("section_name"));
            seccell.setCellStyle(dnamestyle);

            HSSFCell domcell = rw.createCell(1);
            domcell.setCellValue(conn.rs.getString("domain_name"));
            domcell.setCellStyle(dnamestyle);

            //values only
            HSSFCell domval = rw.createCell(2);
            domval.setCellValue(dmn);
            domval.setCellStyle(dnamestyle);

            HSSFCell blank = rw.createCell(3);
            blank.setCellValue("");
            blank.setCellStyle(dnamestyle);

            //now, draw the chart
            HSSFPatriarch patriarch = shet2.createDrawingPatriarch();
            HSSFTextbox textbox1 = patriarch.createTextbox(
                    new HSSFClientAnchor(0, 0, (dmn * 10), 255, (short) 3, rwcount, (short) 3, rwcount));
            textbox1.setString(new HSSFRichTextString("" + dmn));
            textbox1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            //green 18,174,55
            //red 250 32 32
            //yellow 248 255 9
            if (dmn >= 75) {
                textbox1.setFillColor(18, 174, 55);
            } else if (dmn > 59 && dmn < 75) {

                textbox1.setFillColor(248, 255, 9);

            }

            else {

                textbox1.setFillColor(250, 32, 32);

            }
            rwcount++;

            if (monitorrows == 4) {
                shet2.addMergedRegion(new CellRangeAddress(rwcount - 4, rwcount - 1, 0, 0));
            }

            if (monitorrows == 12) {

                shet2.addMergedRegion(new CellRangeAddress(rwcount - 8, rwcount - 1, 0, 0));

                HSSFRow lastrw = shet2.createRow(rwcount);
                lastrw.setHeightInPoints(25);
                //now create a row with average
                HSSFCell avcell0 = lastrw.createCell(0);
                avcell0.setCellValue("Average");
                avcell0.setCellStyle(dnamestyle);

                HSSFCell avcell = lastrw.createCell(1);
                avcell.setCellValue("Average");
                avcell.setCellStyle(dnamestyle);

                HSSFCell avcell1 = lastrw.createCell(2);
                avcell1.setCellValue(ttlsm);
                avcell1.setCellStyle(dnamestyle);

                HSSFCell blank1 = lastrw.createCell(3);
                blank1.setCellValue("");
                blank1.setCellStyle(dnamestyle);
                HSSFTextbox textbox = patriarch.createTextbox(
                        new HSSFClientAnchor(0, 0, (dmn * (10)), 255, (short) 3, rwcount, (short) 3, rwcount));
                textbox.setString(new HSSFRichTextString("" + ttlsm));
                if (dmn >= 75) {

                    textbox.setFillColor(18, 174, 55);

                } else if (dmn > 59 && dmn < 75) {

                    textbox.setFillColor(248, 255, 9);

                }

                else {

                    textbox.setFillColor(250, 32, 32);

                }
                textbox.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

                isrow1 = true;
                monitorrows = 0;
                //dont print anything 
                rwcount++;

                //last blank cell

                HSSFRow blankrow = shet2.createRow(rwcount);
                blankrow.setHeightInPoints(30);
                for (int b = 0; b < tableheaders.length; b++) {
                    HSSFCell cel1 = blankrow.createCell(b);
                    cel1.setCellValue("");
                    cel1.setCellStyle(innerdata_style);
                }

                rwcount++;
            }

        }

        ///=========================end of while loop 

        //write it as an excel attachment

        ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
        wb.write(outByteStream);
        byte[] outArray = outByteStream.toByteArray();
        response.setContentType("application/ms-excel");
        response.setContentLength(outArray.length);
        response.setHeader("Expires:", "0"); // eliminates browser caching
        response.setHeader("Content-Disposition",
                "attachment; filename=OVC_CBO_CHARTS_FROM_" + startdate + "_TO_" + enddate + ".xls");
        OutputStream outStream = response.getOutputStream();
        outStream.write(outArray);
        outStream.flush();
    } catch (SQLException ex) {
        Logger.getLogger(resultspercbo.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:reports.basicreports.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from   w  w w .  ja  v  a 2  s. com
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {
        dbConn conn = new dbConn();

        wb = new HSSFWorkbook();

        HSSFSheet shet2 = null;

        String year = "";
        String site = "";
        String period = "";
        String cbo = "";

        year = request.getParameter("year");
        site = request.getParameter("sitecbo");
        period = request.getParameter("period");
        cbo = request.getParameter("staffcbo");

        String sitename = "";
        String cboname = "";

        conn.rs = conn.st.executeQuery("select cbo from cbo where cboid='" + cbo + "'");
        if (conn.rs.next()) {
            cboname = conn.rs.getString(1);
        }

        conn.rs = conn.st.executeQuery("select site_name from sites where site_id='" + site + "'");
        if (conn.rs.next()) {
            sitename = conn.rs.getString(1);
        }

        HSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short) 12);
        font.setFontName("Cambria");
        //    font.setItalic(true);
        font.setBoldweight((short) 02);
        font.setColor(HSSFColor.BLACK.index);
        CellStyle style = wb.createCellStyle();
        style.setFont(font);
        style.setWrapText(true);
        style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);

        style.setAlignment(style.ALIGN_CENTER);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        //%%%%%%%%%%%%%%%%HEADER FONTS AND COLORATION%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        HSSFFont font_header = wb.createFont();
        font_header.setFontHeightInPoints((short) 10);
        font_header.setFontName("Eras Bold ITC");
        //    font.setItalic(true);
        font_header.setBoldweight((short) 05);
        font_header.setColor(HSSFColor.BLACK.index);
        CellStyle style_header = wb.createCellStyle();
        style_header.setFont(font_header);
        style_header.setWrapText(true);
        style_header.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
        style_header.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style_header.setAlignment(style_header.ALIGN_CENTER);

        //            style_header.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //            style_header.setBorderTop(HSSFCellStyle.BORDER_THIN);
        //            style_header.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //            style_header.setBorderLeft(HSSFCellStyle.BORDER_THIN);

        //%%%%%%%%%%%%%%%%%%%%%%%%%DATA FONT%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        //font data
        HSSFFont datafont = wb.createFont();
        datafont.setBoldweight((short) 03);
        datafont.setColor(HSSFColor.BLACK.index);
        datafont.setFontHeightInPoints((short) 10);
        datafont.setFontName("Cambria");
        datafont.setItalic(true);

        //bold font 
        HSSFFont bolfont = wb.createFont();

        bolfont.setBoldweight((short) 05);
        bolfont.setColor(HSSFColor.BLACK.index);
        bolfont.setFontHeightInPoints((short) 12);
        bolfont.setFontName("Cambria");

        //=======INNER DATA STYLING===========================

        CellStyle innerdata_style = wb.createCellStyle();
        innerdata_style.setFont(datafont);
        innerdata_style.setWrapText(true);
        innerdata_style.setAlignment(innerdata_style.ALIGN_CENTER);
        innerdata_style.setFillForegroundColor(HSSFColor.WHITE.index);
        innerdata_style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        //            innerdata_style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //            innerdata_style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        //            innerdata_style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //            innerdata_style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        innerdata_style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        CellStyle lastcellrighborder = wb.createCellStyle();
        lastcellrighborder.setFont(datafont);
        lastcellrighborder.setWrapText(true);
        lastcellrighborder.setAlignment(lastcellrighborder.ALIGN_CENTER);
        lastcellrighborder.setFillForegroundColor(HSSFColor.WHITE.index);
        lastcellrighborder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        //            lastcellrighborder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //            lastcellrighborder.setBorderTop(HSSFCellStyle.BORDER_THIN);
        lastcellrighborder.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //            lastcellrighborder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        lastcellrighborder.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        CellStyle innerdata_style2 = wb.createCellStyle();
        innerdata_style2.setFont(bolfont);
        innerdata_style2.setWrapText(true);
        innerdata_style2.setAlignment(innerdata_style.ALIGN_LEFT);
        innerdata_style2.setFillForegroundColor(HSSFColor.WHITE.index);
        innerdata_style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        //            innerdata_style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //            innerdata_style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        //            innerdata_style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //            innerdata_style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        innerdata_style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        //Code colors

        CellStyle lg = wb.createCellStyle();
        lg.setFont(bolfont);
        lg.setWrapText(true);
        lg.setAlignment(lg.ALIGN_CENTER);
        lg.setFillForegroundColor(HSSFColor.GREEN.index);
        lg.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        lg.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        lg.setBorderTop(HSSFCellStyle.BORDER_THIN);
        lg.setBorderRight(HSSFCellStyle.BORDER_THIN);
        lg.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        lg.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        CellStyle Y = wb.createCellStyle();
        Y.setFont(bolfont);
        Y.setWrapText(true);
        Y.setAlignment(Y.ALIGN_CENTER);
        Y.setFillForegroundColor(HSSFColor.YELLOW.index);
        Y.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        Y.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        Y.setBorderTop(HSSFCellStyle.BORDER_THIN);
        Y.setBorderRight(HSSFCellStyle.BORDER_THIN);
        Y.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        Y.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        CellStyle R = wb.createCellStyle();
        R.setFont(bolfont);
        R.setWrapText(true);
        R.setAlignment(R.ALIGN_CENTER);
        R.setFillForegroundColor(HSSFColor.RED.index);
        R.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        R.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        R.setBorderTop(HSSFCellStyle.BORDER_THIN);
        R.setBorderRight(HSSFCellStyle.BORDER_THIN);
        R.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        R.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        //=======INNER LEFT DATA STYLING===========================

        CellStyle binnerdata_style2 = wb.createCellStyle();
        binnerdata_style2.setFont(datafont);
        binnerdata_style2.setWrapText(true);
        binnerdata_style2.setAlignment(binnerdata_style2.ALIGN_LEFT);
        binnerdata_style2.setFillForegroundColor(HSSFColor.WHITE.index);
        binnerdata_style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        binnerdata_style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        binnerdata_style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
        binnerdata_style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
        binnerdata_style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        binnerdata_style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        //create a header

        //=======================Domainname styles
        CellStyle dnamestyle = wb.createCellStyle();
        dnamestyle.setFont(bolfont);
        dnamestyle.setWrapText(true);
        dnamestyle.setAlignment(dnamestyle.ALIGN_LEFT);
        dnamestyle.setFillForegroundColor(HSSFColor.WHITE.index);
        dnamestyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        dnamestyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        dnamestyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        dnamestyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        dnamestyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        dnamestyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        shet2 = wb.createSheet("Report");
        shet2.setColumnWidth(0, 10000);
        shet2.setColumnWidth(1, 5000);
        shet2.setColumnWidth(2, 5000);
        shet2.setColumnWidth(3, 5000);
        shet2.setColumnWidth(4, 8000);
        shet2.setColumnWidth(5, 8000);
        //create header one
        HSSFRow header = shet2.createRow(0);
        header.setHeightInPoints(30);
        HSSFCell cel1 = header.createCell(0);
        cel1.setCellValue("APHIAplus NURU YA BONDE");
        cel1.setCellStyle(style);
        for (int b = 1; b <= 5; b++) {
            cel1 = header.createCell(b);
            cel1.setCellValue("");
            cel1.setCellStyle(style);
        }

        shet2.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));

        //create header two
        HSSFRow header2 = shet2.createRow(1);
        header2.setHeightInPoints(28);
        HSSFCell cel2 = null;
        for (int b = 1; b <= 5; b++) {
            cel2 = header2.createCell(b);
            cel2.setCellValue("");
            cel2.setCellStyle(style);
        }

        cel2 = header2.createCell(0);
        cel2.setCellValue("OVC LIP SUPPORT SUPERVISION DASH BOARD");
        cel2.setCellStyle(style);

        shet2.addMergedRegion(new CellRangeAddress(1, 1, 0, 5));

        //cbo name
        //create header three
        HSSFRow header3 = shet2.createRow(2);
        HSSFCell cel3 = header3.createCell(0);
        cel3.setCellValue("Name of LIP/CBO");
        cel3.setCellStyle(innerdata_style2);

        HSSFCell cel4 = header3.createCell(1);
        cel4.setCellValue("" + cboname);
        cel4.setCellStyle(innerdata_style);
        //blank cells for purpose of clear worksheet only
        for (int x = 2; x <= 3; x++) {
            HSSFCell cel = header3.createCell(x);
            cel.setCellValue("");
            cel.setCellStyle(innerdata_style);
        }

        HSSFCell cel5 = header3.createCell(4);
        cel5.setCellValue("Site Visited:");
        cel5.setCellStyle(innerdata_style2);

        HSSFCell cel6 = header3.createCell(5);
        cel6.setCellValue("" + sitename);
        cel6.setCellStyle(lastcellrighborder);

        //add the width of this column

        //create a blank row whose last cell has a border

        HSSFRow blankrw = shet2.createRow(3);
        for (int z = 0; z < 5; z++) {

            HSSFCell cl = blankrw.createCell(z);
            cl.setCellValue("");
            cl.setCellStyle(innerdata_style);
        }
        HSSFCell cl = blankrw.createCell(5);
        cl.setCellValue("");
        cl.setCellStyle(lastcellrighborder);
        //==========DATE OF VISIT

        String mywhere = "site='" + site + "' and period='" + period + "' and year='" + year + "' ";

        String supervisor = "";
        String dateofvisit = "";

        String strengths = "";
        String constraints = "";

        String loadbasicdetails = "select * from backgroundinfor join staff on backgroundinfor.supervisor=staff.staff_id where "
                + mywhere + " ";
        System.out.println(loadbasicdetails);
        conn.rs = conn.st.executeQuery(loadbasicdetails);
        while (conn.rs.next()) {
            supervisor = conn.rs.getString("fname") + " " + conn.rs.getString("mname");
            dateofvisit = conn.rs.getString("ass_date");
            strengths = conn.rs.getString("strengths");
            constraints = conn.rs.getString("constraints");

        }

        //================Create the second header=================
        //create header three
        HSSFRow header4 = shet2.createRow(4);
        HSSFCell cel = header4.createCell(0);
        cel.setCellValue("Date of Visit");
        cel.setCellStyle(innerdata_style2);

        HSSFCell cell = header4.createCell(1);
        cell.setCellValue("" + dateofvisit);
        cell.setCellStyle(innerdata_style);

        //blank cells for purpose of clear worksheet only
        for (int x = 2; x <= 3; x++) {
            HSSFCell ceel = header4.createCell(x);
            ceel.setCellValue("");
            ceel.setCellStyle(innerdata_style);
        }

        HSSFCell cell5 = header4.createCell(4);
        cell5.setCellValue("Supervision Team Lead:");
        cell5.setCellStyle(innerdata_style2);

        HSSFCell cell6 = header4.createCell(5);
        cell6.setCellValue("" + supervisor);
        cell6.setCellStyle(lastcellrighborder);

        //another blank row
        HSSFRow blankrw2 = shet2.createRow(5);
        for (int z = 0; z < 5; z++) {

            HSSFCell cl2 = blankrw2.createCell(z);
            cl2.setCellValue("");
            cl2.setCellStyle(innerdata_style);
        }
        HSSFCell cl2 = blankrw2.createCell(5);
        cl2.setCellValue("");
        cl2.setCellStyle(lastcellrighborder);

        //create a header

        String theaderar[] = { "Assesment Domain", "LG", "Y", "R", "Comments/Action", "" };

        HSSFRow theader = shet2.createRow(6);

        for (int x = 0; x < theaderar.length; x++) {
            HSSFCell tcel = theader.createCell(x);
            tcel.setCellValue(theaderar[x]);
            if (theaderar[x].equalsIgnoreCase("LG")) {
                tcel.setCellStyle(lg);
            } else if (theaderar[x].equalsIgnoreCase("Y")) {
                tcel.setCellStyle(Y);
            } else if (theaderar[x].equalsIgnoreCase("R")) {
                tcel.setCellStyle(R);
            } else {
                tcel.setCellStyle(style);
            }

        }
        shet2.addMergedRegion(new CellRangeAddress(6, 6, 4, 5));
        //SECTION A HEADER
        HSSFRow seca = shet2.createRow(7);
        HSSFCell tcel1 = seca.createCell(0);
        tcel1.setCellValue("Section A: Data management and Reporting Systems");
        tcel1.setCellStyle(style);
        for (int b = 1; b <= 5; b++) {
            cel1 = seca.createCell(b);
            cel1.setCellValue("");
            cel1.setCellStyle(style);
        }

        shet2.addMergedRegion(new CellRangeAddress(7, 7, 0, 5));

        String gettables = "SELECT domain_name,domains.domain_id as domainid,section_name,domains.section_id as secid,value as domainvalue,aggregate_sum,period,year,site FROM domains join sections on domains.section_id=sections.section_id join domain_totals on domains.domain_id=domain_totals.domainid where "
                + mywhere + " order by domainid";
        System.out.println(gettables);
        conn.rs = conn.st.executeQuery(gettables);
        int rwcount = 8;
        HSSFRow rwx = null;
        HSSFCell celx = null;
        String sectioncopy = "";
        while (conn.rs.next()) {
            if (sectioncopy.equals("")) {
                sectioncopy = conn.rs.getString("section_name");
            }
            //if the section has changed
            if (!sectioncopy.equals(conn.rs.getString("section_name"))) {
                //create a section header
                //``````````````````````````````INNER SECTION HEADERS``````````````````            
                //``````````````````````````````INNER SECTION HEADERS``````````````````            
                //``````````````````````````````INNER SECTION HEADERS``````````````````            
                HSSFRow secb = shet2.createRow(rwcount);
                HSSFCell t = secb.createCell(0);
                t.setCellValue("Section " + conn.rs.getString("section_name"));
                t.setCellStyle(style);
                //for purpose of merging
                for (int b = 1; b <= 5; b++) {
                    cel1 = secb.createCell(b);
                    cel1.setCellValue("");
                    cel1.setCellStyle(style);
                }
                //equalize copy and current value       
                sectioncopy = conn.rs.getString("section_name");
                shet2.addMergedRegion(new CellRangeAddress(rwcount, rwcount, 0, 5));

                //increment rowcount to skip the current row 
                rwcount++;
            }

            String valu[] = { conn.rs.getString("domain_name"), "", "", "", "", "" };

            rwx = shet2.createRow(rwcount);
            for (int t = 0; t < valu.length; t++) {
                celx = rwx.createCell(t);
                celx.setCellValue("" + valu[t]);
                celx.setCellStyle(dnamestyle);
            }
            shet2.addMergedRegion(new CellRangeAddress(rwcount, rwcount, 4, 5));
            //get the value of percentange achievement per domian
            //multiply by 100
            //round off
            float domainvalue = conn.rs.getFloat("domainvalue");

            domainvalue = domainvalue * 100;
            //BigDecimal bd=new BigDecimal(domainvalue).setScale(0,RoundingMode.HALF_EVEN);
            //domainval=bd.doubleValue();
            domainvalue = Math.round(domainvalue);

            //determine the cell to print data on
            if (domainvalue >= 75) {

                celx = rwx.createCell(1);
                celx.setCellValue("" + domainvalue + "%");
                celx.setCellStyle(lg);

            } else if (domainvalue >= 60 && domainvalue < 75) {
                celx = rwx.createCell(2);
                celx.setCellValue("" + domainvalue + "%");
                celx.setCellStyle(Y);

            } else if (domainvalue < 60) {

                celx = rwx.createCell(3);
                celx.setCellValue("" + domainvalue + "%");
                celx.setCellStyle(R);
            }

            rwcount++;
        }

        ///=========================end of while loop 

        //====================STRENGTHS=========================== 
        HSSFRow secb = shet2.createRow(rwcount);

        //for purpose of merging
        for (int b = 1; b <= 5; b++) {
            cel1 = secb.createCell(b);
            cel1.setCellValue("");
            cel1.setCellStyle(style);
        }

        HSSFCell t = secb.createCell(0);
        t.setCellValue("What has worked well and key areas of strengths observed");
        t.setCellStyle(style);
        shet2.addMergedRegion(new CellRangeAddress(rwcount, rwcount, 0, 5));

        rwcount++;

        HSSFRow str = shet2.createRow(rwcount);

        for (int b = 1; b <= 5; b++) {
            cel1 = str.createCell(b);
            cel1.setCellValue("");
            cel1.setCellStyle(dnamestyle);
        }

        HSSFCell t1 = str.createCell(0);
        t1.setCellValue("" + strengths);
        t1.setCellStyle(dnamestyle);
        shet2.addMergedRegion(new CellRangeAddress(rwcount, rwcount, 0, 5));
        //for purpose of merging

        str.setHeightInPoints(50);
        rwcount++;

        //=======Contraints
        HSSFRow sec3 = shet2.createRow(rwcount);

        //for purpose of merging
        for (int b = 1; b <= 5; b++) {
            cel1 = sec3.createCell(b);
            cel1.setCellValue("");
            cel1.setCellStyle(style);
        }

        HSSFCell t2 = sec3.createCell(0);
        t2.setCellValue("Critical consraints affecting quality programming and data management");
        t2.setCellStyle(style);
        shet2.addMergedRegion(new CellRangeAddress(rwcount, rwcount, 0, 5));

        rwcount++;

        HSSFRow str2 = shet2.createRow(rwcount);

        for (int b = 1; b <= 5; b++) {
            cel1 = str2.createCell(b);
            cel1.setCellValue("");
            cel1.setCellStyle(dnamestyle);
        }

        HSSFCell t4 = str2.createCell(0);
        t4.setCellValue("" + constraints);
        t4.setCellStyle(dnamestyle);
        shet2.addMergedRegion(new CellRangeAddress(rwcount, rwcount, 0, 5));
        str2.setHeightInPoints(50);

        rwcount++;
        //a line of codes
        String codes[] = { "LG - Meets Expectations (>=75%); ", " Y- Needs Improvement (60%- 74%);",
                "R - Needs Urgent Attention (<=59%);" };
        HSSFRow rwl = shet2.createRow(rwcount);
        HSSFCell ce = rwl.createCell(0);
        ce.setCellValue("CODES");
        ce.setCellStyle(dnamestyle);
        for (int b = 0; b < codes.length; b++) {
            ce = rwl.createCell(b + 1);
            ce.setCellValue("" + codes[b]);
            if (b == 0) {
                ce.setCellStyle(lg);
            } else if (b == 1) {

                ce.setCellStyle(Y);
            } else {
                ce.setCellStyle(R);
            }
        }
        ce = rwl.createCell(4);
        ce.setCellValue("");
        ce.setCellStyle(dnamestyle);
        ce = rwl.createCell(5);
        ce.setCellValue("");
        ce.setCellStyle(dnamestyle);
        shet2.addMergedRegion(new CellRangeAddress(rwcount, rwcount, 4, 5));

        //write it as an excel attachment
        sitename = sitename.replace(" ", "_");
        sitename = sitename.replace("'", "");
        cboname = cboname.replace(" ", "_");
        cboname = cboname.replace("'", "_");
        ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
        wb.write(outByteStream);
        byte[] outArray = outByteStream.toByteArray();
        response.setContentType("application/ms-excel");
        response.setContentLength(outArray.length);
        response.setHeader("Expires:", "0"); // eliminates browser caching
        response.setHeader("Content-Disposition",
                "attachment; filename=OVC_LIP_REPORT_" + cboname + "_" + sitename + ".xls");
        OutputStream outStream = response.getOutputStream();
        outStream.write(outArray);
        outStream.flush();
    } catch (SQLException ex) {
        Logger.getLogger(basicreports.class.getName()).log(Level.SEVERE, null, ex);
    }

}