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

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

Introduction

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

Prototype

@Override
public void setFillPattern(FillPatternType fp) 

Source Link

Document

setting to one fills the cell with the foreground color...

Usage

From source file:reports.kePMSFormated.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, SQLException {
    session = request.getSession();//from  w  w w.  j  a  v a  2s. c  o m
    dbConn conn = new dbConn();
    pos = 0;

    pepfaryear = 2014;
    //    pepfaryear=Integer.parseInt(request.getParameter("year"));
    prevyear = pepfaryear - 1;
    String enddate = pepfaryear + "09";
    String startdate = prevyear + "10";

    start = Integer.parseInt(startdate);
    end = Integer.parseInt(enddate);
    System.out.println("start date : " + start + " end date  : " + end);
    String allpath = getServletContext().getRealPath("/TEMPLATE.xlsm");

    //            ^^^^^^^^^^^^^CREATE STATIC AND WRITE STATIC DATA TO THE EXCELL^^^^^^^^^^^^
    HSSFWorkbook wb;
    wb = new HSSFWorkbook();

    //        HSSFWorkbook wb=new HSSFWorkbook();
    HSSFSheet shet1 = wb.createSheet("sheet0");
    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short) 18);
    font.setFontName("Arial Black");
    font.setColor((short) 0000);
    CellStyle style = wb.createCellStyle();
    style.setFont(font);
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    HSSFFont font2 = wb.createFont();
    font2.setFontName("Arial Black");
    font2.setColor((short) 0000);
    CellStyle style2 = wb.createCellStyle();
    style2.setFont(font2);

    HSSFCellStyle stborder = wb.createCellStyle();
    stborder.setBorderTop(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderRight(HSSFCellStyle.BORDER_THIN);
    stborder.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    HSSFCellStyle stylex = wb.createCellStyle();
    stylex.setFillForegroundColor(HSSFColor.LIME.index);
    stylex.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    stylex.setBorderTop(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderRight(HSSFCellStyle.BORDER_THIN);
    stylex.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    HSSFFont fontx = wb.createFont();
    fontx.setColor(HSSFColor.DARK_BLUE.index);
    stylex.setFont(fontx);
    stylex.setWrapText(true);

    //  HSSFSheet sheet1 = wb.getSheetAt(0);
    shet1.setColumnWidth(0, 4000);
    shet1.setColumnWidth(1, 4000);
    shet1.setColumnWidth(2, 4000);
    shet1.setColumnWidth(3, 4000);
    shet1.setColumnWidth(4, 4000);

    HSSFRow rw4 = shet1.createRow(0);
    rw4.setHeightInPoints(45);
    rw4.setRowStyle(style2);
    // rw4.createCell(1).setCellValue("Number");
    HSSFCell cell0, cell1, cell2, cell3, cell4;

    cell0 = rw4.createCell(0);
    cell1 = rw4.createCell(1);
    cell2 = rw4.createCell(2);
    cell3 = rw4.createCell(3);
    cell4 = rw4.createCell(4);

    cell0.setCellValue("PARTNER NAME");
    cell1.setCellValue("AGE BRACKET");
    cell2.setCellValue("GENDER");
    cell3.setCellValue("MONTH");
    cell4.setCellValue("ACHIEVED");

    String getClients = "SELECT partner.partner_name," + "CASE " + "when clients.completionmonth =01 THEN '"
            + pepfaryear + "-01(JAN)' " + "when clients.completionmonth =02 THEN '" + pepfaryear + "-02 (FEB)' "
            + "when clients.completionmonth =03 THEN '" + pepfaryear + "-03 (MAR)' "
            + "when clients.completionmonth=04 THEN '" + pepfaryear + "-04 (APR)' "
            + "when clients.completionmonth=05 THEN '" + pepfaryear + "-05 (MAY)' "
            + "when clients.completionmonth=06 THEN '" + pepfaryear + "-06 (JUN)' "
            + "when clients.completionmonth=07 THEN '" + pepfaryear + "-07 (JUL)' "
            + "when clients.completionmonth=08 THEN '" + pepfaryear + "-08 (AUG)' "
            + "when clients.completionmonth=09 THEN '" + pepfaryear + "-09 (SEPT)' "
            + "when clients.completionmonth=10 THEN '" + prevyear + "-10 (OCT)' "
            + "when clients.completionmonth=11 THEN '" + prevyear + "-11 (NOV)'"
            + "when clients.completionmonth=12 THEn '" + prevyear + "-12 (DEC)'"
            + "END AS MONTHS,clients.completionyear," + "CASE"
            + "      WHEN clients.age BETWEEN 0 AND 14 THEN '0-14'"
            + "      WHEN clients.age BETWEEN 15 AND 19 THEN '15-19'"
            + "      WHEN clients.age BETWEEN 20 AND 24 THEN '20-24'" + "      WHEN clients.age >24 THEN '>25'"
            + " ELSE 'NOT SELECTED'" + "   END AS AGEBRACKET," + "CASE "
            + "when clients.gender LIKE 'Female' THEN 'F' " + "when clients.gender LIKE 'Male' THEN 'M' "
            + "ELSE 'NO SEX' " + "END AS SEX" + ",completionmonth,COUNT(clients.client_id) FROM clients "
            + "JOIN partner ON clients.partner_id=partner.partner_id "
            + " WHERE clients.completionmonth>0 && clients.completionyear>0 GROUP BY partner.partner_name,SEX,MONTHS,AGEBRACKET ORDER BY clients.partner_id";
    conn.rs = conn.st.executeQuery(getClients);
    while (conn.rs.next()) {
        partnername = clientid = gender = "";
        age = 0;

        partnername = conn.rs.getString(1);
        month = conn.rs.getString(2);
        year = conn.rs.getInt(3);
        agebracket = conn.rs.getString(4);
        gender = conn.rs.getString(5);
        String dkey = year + "" + conn.rs.getString(6);
        datekey = Integer.parseInt(dkey);
        achieved = conn.rs.getInt(7);

        if (datekey >= start && datekey <= end && year >= 2014) {

            pos++;
            HSSFRow rw4x = shet1.createRow(pos);
            rw4x.setHeightInPoints(25);
            rw4x.setRowStyle(style2);
            HSSFCell cell0x, cell1x, cell2x, cell3x, cell4x;
            cell0x = rw4x.createCell(0);
            cell1x = rw4x.createCell(1);
            cell2x = rw4x.createCell(2);
            cell3x = rw4x.createCell(3);
            cell4x = rw4x.createCell(4);

            //  OUTPUT SERVICES PROVIDED================================     
            cell0x.setCellValue(partnername);
            cell1x.setCellValue(agebracket);
            cell2x.setCellValue(gender);
            cell3x.setCellValue(month);
            cell4x.setCellValue(achieved);

            System.out.println(pos + "-----partner :" + partnername + " age bracket :" + agebracket
                    + " gender :" + gender + " completion month : " + month);
        }
    }

    if (conn.rs != null) {
        conn.rs.close();
    }
    if (conn.st != null) {
        conn.st.close();
    }
    if (conn.rs1 != null) {
        conn.rs1.close();
    }
    if (conn.st1 != null) {
        conn.st1.close();
    }
    if (conn.rs2 != null) {
        conn.rs2.close();
    }
    if (conn.st2 != null) {
        conn.st2.close();
    }
    if (conn.st3 != null) {
        conn.st3.close();
    }
    if (conn.pst != null) {
        conn.pst.close();
    }
    if (conn.pst != null) {
        conn.pst.close();
    }
    if (conn.pst1 != null) {
        conn.pst1.close();
    }
    if (conn.pst1 != null) {
        conn.pst1.close();
    }
    if (conn.conn != null) {
        conn.conn.close();
    }

    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=PWP_kePMS_REPORT.xls");
    OutputStream outStream = response.getOutputStream();
    outStream.write(outArray);
    outStream.flush();

}

From source file:reports.kmmpexcel.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {// ww w.j a  v a 2s .  c  o  m
        response.setContentType("text/html;charset=UTF-8");
        session = request.getSession();

        dbConn conn = new dbConn();
        //get the existing data for the month, year and facility that is already on session

        String month = "";
        String year = "";
        String facil = "361";

        String form = "kmmp";

        String reportType = "";
        if (request.getParameter("reportType") != null) {
            reportType = request.getParameter("reportType");
        }
        String reportDuration = "";
        if (request.getParameter("reportDuration") != null) {
            reportDuration = request.getParameter("reportDuration");
        }

        //=====================================================================================================

        year = "2015";
        month = "5";
        String county = "";

        String header = "";

        String reporttype = "";

        if (request.getParameter("year") != null) {
            year = request.getParameter("year");
        }

        if (request.getParameter("facility") != null && reportType.equals("2")) {
            facil = request.getParameter("facility");

            String getfacil = "select SubPartnerNom,CentreSanteId as mflcode from subpartnera where SubPartnerID='"
                    + facil + "'";
            conn.rs = conn.st.executeQuery(getfacil);

            while (conn.rs.next()) {

                header += " FACILITY : " + conn.rs.getString(1).toUpperCase() + "     MFL CODE  :  "
                        + conn.rs.getString(2) + "  ";

            }

        }

        if (request.getParameter("county") != null && reportType.equals("2")) {
            county = request.getParameter("county");

            String getcounty = "select County from county where CountyID='" + county + "'";
            conn.rs = conn.st.executeQuery(getcounty);

            while (conn.rs.next()) {

                header += " COUNTY : " + conn.rs.getString(1).toUpperCase() + " ";

            }

        }

        if (request.getParameter("month") != null && reportDuration.equals("4")) {
            month = request.getParameter("month");

            String getmonth = "select name as monthname from month where id='" + month + "'";
            conn.rs = conn.st.executeQuery(getmonth);

            while (conn.rs.next()) {

                header += " MONTH : " + conn.rs.getString(1).toUpperCase() + " ";

            }

        }

        header += " YEAR : " + year + "";

        String facilitywhere = "";
        String yearwhere = "";
        String monthwhere = "";
        String countywhere = "";
        String duration = "";
        String semi_annual = "";
        String quarter = "";

        //==================================================================================================
        //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

        int yearcopy = Integer.parseInt(year);

        //        reportType="2";
        //        year=2015;
        //        reportDuration="3";
        String yearmonth = "" + year;
        int prevYear = yearcopy - 1;
        int maxYearMonth = 0;
        int monthcopy = 0;
        //        GET REPORT DURATION============================================

        if (reportDuration.equals("1")) {
            yearmonth += "_AnnualReport";
            duration = " " + form + ".yearmonth BETWEEN " + prevYear + "10 AND " + year + "09";
        } else if (reportDuration.equals("2")) {
            semi_annual = request.getParameter("semi_annual");
            //        semi_annual="2";
            if (semi_annual.equals("1")) {
                yearmonth = prevYear + "_Oct_" + year + "_Mar";
                duration = " " + form + ".yearmonth BETWEEN " + prevYear + "10 AND " + year + "03";
            } else {
                yearmonth += "_Apr_Sep";
                duration = " " + form + ".yearmonth BETWEEN " + year + "04 AND " + year + "09";
            }
        }

        else if (reportDuration.equals("3")) {
            String startMonth, endMonth;
            quarter = request.getParameter("quarter");
            //       quarter="3";
            String getMonths = "SELECT months,name FROM quarter WHERE id='" + quarter + "'";
            conn.rs = conn.st.executeQuery(getMonths);
            if (conn.rs.next() == true) {

                String months[] = conn.rs.getString(1).split(",");
                startMonth = months[0];
                endMonth = months[2];
                if (quarter.equals("1")) {
                    duration = " " + form + ".yearmonth BETWEEN " + prevYear + "" + startMonth + " AND "
                            + prevYear + "" + endMonth;
                    yearmonth = prevYear + "_" + conn.rs.getString(2);
                } else {
                    yearmonth = year + "_" + conn.rs.getString(2);
                    duration = " " + form + ".yearmonth BETWEEN " + year + "" + startMonth + " AND " + year + ""
                            + endMonth;
                }
            }
        }

        else if (reportDuration.equals("4")) {
            monthcopy = Integer.parseInt(request.getParameter("month"));

            //     month=5;
            if (monthcopy >= 10) {
                yearmonth = prevYear + "_" + month;
                duration = " " + form + ".yearmonth=" + prevYear + "" + month;
            } else {
                duration = " " + form + ".yearmonth=" + year + "0" + month;
                yearmonth = year + "_(" + month + ")";
            }
        } else {
            duration = "";
        }

        //======================================================================   

        //==================================================================================================
        //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

        String getexistingdata = "";

        if (!county.equals("")) {

            countywhere = " and countyid = '" + county + "'";

        }

        if (!facil.equals("") && reportType.equalsIgnoreCase("2")) {

            facilitywhere = " and kmmp.SubPartnerID = '" + facil + "'";

        }

        //String joinedwhwere=" where 1=1 "+yearwhere+" && "+duration;  

        String joinedwhwere = " where 1=1 " + facilitywhere + "  " + yearwhere + " && " + duration;

        //=====================================================================================================    

        //______________________________________________________________________________________
        //                       NOW CREATE THE WORKSHEETS          
        //______________________________________________________________________________________  

        HSSFWorkbook wb = new HSSFWorkbook();

        //______________________________________________________________________________________
        //______________________________________________________________________________________

        HSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short) 18);
        font.setFontName("Cambria");
        font.setColor((short) 0000);
        CellStyle style = wb.createCellStyle();
        style.setFont(font);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFFont font2 = wb.createFont();
        font2.setFontName("Cambria");
        font2.setColor((short) 0000);
        CellStyle style2 = wb.createCellStyle();
        style2.setFont(font2);
        style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style2.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFCellStyle stborder = wb.createCellStyle();
        stborder.setBorderTop(HSSFCellStyle.BORDER_THIN);
        stborder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        stborder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        stborder.setBorderRight(HSSFCellStyle.BORDER_THIN);
        stborder.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        HSSFCellStyle stylex = wb.createCellStyle();
        stylex.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
        stylex.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        stylex.setBorderTop(HSSFCellStyle.BORDER_THIN);
        stylex.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        stylex.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        stylex.setBorderRight(HSSFCellStyle.BORDER_THIN);
        stylex.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        HSSFFont fontx = wb.createFont();
        fontx.setColor(HSSFColor.BLACK.index);
        fontx.setFontName("Cambria");
        stylex.setFont(fontx);
        stylex.setWrapText(true);

        HSSFSheet shet = wb.createSheet(form);

        //create headers for that worksheet

        HSSFRow rw = shet.createRow(0);
        rw.setHeightInPoints(25);
        HSSFCell cl0 = rw.createCell(0);
        cl0.setCellValue("KMMP HEALTH FACILITY REPORTING FORM");
        cl0.setCellStyle(stylex);

        for (int a = 1; a < 4; a++) {
            HSSFCell clx = rw.createCell(a);
            clx.setCellValue("");
            clx.setCellStyle(stylex);
        }

        HSSFRow rw1 = shet.createRow(1);
        rw1.setHeightInPoints(23);
        HSSFCell cl = rw1.createCell(0);
        cl.setCellValue(header);
        cl.setCellStyle(stylex);

        for (int a = 1; a < 4; a++) {
            HSSFCell clx = rw1.createCell(a);
            clx.setCellValue("");
            clx.setCellStyle(stylex);
        }

        HSSFRow rw2 = shet.createRow(2);
        rw2.setHeightInPoints(23);
        HSSFCell cl3 = rw2.createCell(0);
        cl3.setCellValue("KMMP OUTPUT DATA");
        cl3.setCellStyle(stylex);

        for (int a = 1; a < 4; a++) {
            HSSFCell clx = rw2.createCell(a);
            clx.setCellValue("");
            clx.setCellStyle(stylex);
        }

        shet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
        shet.addMergedRegion(new CellRangeAddress(1, 1, 0, 3));
        shet.addMergedRegion(new CellRangeAddress(2, 2, 0, 3));
        shet.addMergedRegion(new CellRangeAddress(3, 3, 1, 2));
        shet.addMergedRegion(new CellRangeAddress(4, 4, 1, 2));
        shet.addMergedRegion(new CellRangeAddress(5, 5, 1, 2));
        shet.addMergedRegion(new CellRangeAddress(5, 7, 0, 0));
        shet.addMergedRegion(new CellRangeAddress(6, 6, 1, 2));
        shet.addMergedRegion(new CellRangeAddress(7, 7, 1, 2));
        shet.addMergedRegion(new CellRangeAddress(8, 8, 1, 2));
        shet.addMergedRegion(new CellRangeAddress(9, 11, 0, 0));
        shet.addMergedRegion(new CellRangeAddress(9, 11, 1, 1));
        shet.addMergedRegion(new CellRangeAddress(12, 12, 1, 2));
        shet.addMergedRegion(new CellRangeAddress(13, 13, 1, 2));
        shet.setColumnWidth(0, 2000);
        shet.setColumnWidth(1, 17000);
        shet.setColumnWidth(2, 5000);

        getexistingdata = "select  sum(KMMP1) as KMMP1,   sum(KMMP2) as KMMP2,  sum(KMMP3a) as KMMP3a,   sum(KMMP3b) as KMMP3b,   avg(KMMP3c) as KMMP3c ,   sum(KMMP4) as KMMP4 ,   sum(KMMP5a) as KMMP5a,    sum(KMMP5b) as KMMP5b,    sum(KMMP5c) as KMMP5c,    sum(HV0205) as HV0205,    sum(HV0206) as HV0206 from kmmp join ( subpartnera join (district join county on county.CountyID=district.CountyID ) on district.DistrictID = subpartnera.DistrictID )  on kmmp.SubPartnerID = subpartnera.SubPartnerID   "
                + joinedwhwere + "  ";

        System.out.println(getexistingdata);
        String formtype = "<b><font color='#4b8df8'>New Entry</font></b>";
        String KMMP1 = "";
        String KMMP2 = "";
        String KMMP3a = "";
        String KMMP3b = "";
        String KMMP3c = "";
        String KMMP4 = "";
        String KMMP5a = "";
        String KMMP5b = "";
        String KMMP5c = "";
        String HV0205 = "";
        String HV0206 = "";

        String distid = "";

        if (session.getAttribute("subcountyid") != null) {
            distid = session.getAttribute("subcountyid").toString();
        }

        int counter = 0;

        conn.rs = conn.st.executeQuery(getexistingdata);
        while (conn.rs.next()) {
            //now check if form was updated and if its one month after data entry
            //now load the column values here

            KMMP1 = conn.rs.getString("KMMP1");
            if (KMMP1 == null) {
                KMMP1 = "";
            }

            KMMP2 = conn.rs.getString("KMMP2");
            if (KMMP2 == null) {
                KMMP2 = "";
            }

            KMMP3a = conn.rs.getString("KMMP3a");
            if (KMMP3a == null) {
                KMMP3a = "";
            }

            KMMP3b = conn.rs.getString("KMMP3b");
            if (KMMP3b == null) {
                KMMP3b = "";
            }

            KMMP3c = conn.rs.getString("KMMP3c");
            if (KMMP3c == null) {
                KMMP3c = "";
            }

            KMMP4 = conn.rs.getString("KMMP4");
            if (KMMP4 == null) {
                KMMP4 = "";
            }

            KMMP5a = conn.rs.getString("KMMP5a");
            if (KMMP5a == null) {
                KMMP5a = "";
            }

            KMMP5b = conn.rs.getString("KMMP5b");
            if (KMMP5b == null) {
                KMMP5b = "";
            }

            KMMP5c = conn.rs.getString("KMMP5c");
            if (KMMP5c == null) {
                KMMP5c = "";
            }

            HV0205 = conn.rs.getString("HV0205");
            if (HV0205 == null) {
                HV0205 = "";
            }

            HV0206 = conn.rs.getString("HV0206");
            if (HV0206 == null) {
                HV0206 = "";
            }

        }

        String createdtable = "";

        if (1 == 1) {

            if (1 == 1) {
                HSSFRow rw3 = shet.createRow(3);
                rw3.setHeightInPoints(23);
                HSSFCell cl4 = rw3.createCell(0);
                cl4.setCellValue("1");
                cl4.setCellStyle(style2);

                HSSFCell cl41 = rw3.createCell(1);
                cl41.setCellValue("No of New HIV positive clients enrolled in KMMP Services (ANC and PN) ");
                cl41.setCellStyle(style2);

                HSSFCell cl42 = rw3.createCell(2);
                cl42.setCellValue("");
                cl42.setCellStyle(style2);

                HSSFCell cl43 = rw3.createCell(3);
                cl43.setCellValue(KMMP1);
                cl43.setCellStyle(style2);
            }
            //================================================================================================  

            if (1 == 1) {
                HSSFRow rw3 = shet.createRow(4);
                rw3.setHeightInPoints(23);
                HSSFCell cl4 = rw3.createCell(0);
                cl4.setCellValue("2");
                cl4.setCellStyle(style2);

                HSSFCell cl41 = rw3.createCell(1);
                cl41.setCellValue("No of New HIV negative clients enrolled in KMMP Services (ANC Only) ");
                cl41.setCellStyle(style2);

                HSSFCell cl42 = rw3.createCell(2);
                cl42.setCellValue("");
                cl42.setCellStyle(style2);

                HSSFCell cl43 = rw3.createCell(3);
                cl43.setCellValue(KMMP2);
                cl43.setCellStyle(style2);
            }
            //================================================================================================

            if (1 == 1) {
                HSSFRow rw3 = shet.createRow(5);
                rw3.setHeightInPoints(23);
                HSSFCell cl4 = rw3.createCell(0);
                cl4.setCellValue("3");
                cl4.setCellStyle(style2);

                HSSFCell cl41 = rw3.createCell(1);
                cl41.setCellValue("a) No. of HIV-positive pregnant women enrolled in KMMP Services");
                cl41.setCellStyle(style2);

                HSSFCell cl42 = rw3.createCell(2);
                cl42.setCellValue("");
                cl42.setCellStyle(style2);

                HSSFCell cl43 = rw3.createCell(3);
                cl43.setCellValue(KMMP3a);
                cl43.setCellStyle(style2);
            }
            //================================================================================================

            if (1 == 1) {
                HSSFRow rw3 = shet.createRow(6);
                rw3.setHeightInPoints(23);

                HSSFCell cl41 = rw3.createCell(1);
                cl41.setCellValue(
                        " b) Total number of HIV-positive pregnant women in facility (New positive \n and Known Positive-MOH731)");
                cl41.setCellStyle(style2);

                HSSFCell cl42 = rw3.createCell(2);
                cl42.setCellValue("");
                cl42.setCellStyle(style2);

                HSSFCell cl43 = rw3.createCell(3);
                cl43.setCellValue(KMMP3b);
                cl43.setCellStyle(style2);
            }
            //================================================================================================

            if (1 == 1) {
                HSSFRow rw3 = shet.createRow(7);
                rw3.setHeightInPoints(23);

                HSSFCell cl41 = rw3.createCell(1);
                cl41.setCellValue(" Percentage of new IV-positive pregnant women enrolled in KMMP Services");
                cl41.setCellStyle(style2);

                HSSFCell cl42 = rw3.createCell(2);
                cl42.setCellValue("");
                cl42.setCellStyle(style2);

                HSSFCell cl43 = rw3.createCell(3);
                cl43.setCellValue(KMMP3c.substring(0, 2) + "%");
                cl43.setCellStyle(style2);
            }
            //================================================================================================

            if (1 == 1) {
                HSSFRow rw3 = shet.createRow(8);
                rw3.setHeightInPoints(23);
                HSSFCell cl4 = rw3.createCell(0);
                cl4.setCellValue("4");
                cl4.setCellStyle(style2);

                HSSFCell cl41 = rw3.createCell(1);
                cl41.setCellValue("No. of KMMP support group sessions held");
                cl41.setCellStyle(style2);

                HSSFCell cl42 = rw3.createCell(2);
                cl42.setCellValue("");
                cl42.setCellStyle(style2);

                HSSFCell cl43 = rw3.createCell(3);
                cl43.setCellValue(KMMP4);
                cl43.setCellStyle(style2);
            }
            //================================================================================================

            if (1 == 1) {
                HSSFRow rw3 = shet.createRow(9);
                rw3.setHeightInPoints(23);
                HSSFCell cl4 = rw3.createCell(0);
                cl4.setCellValue("");
                cl4.setCellStyle(style2);

                HSSFCell cl41 = rw3.createCell(1);
                cl41.setCellValue("Defaulter tracing");
                cl41.setCellStyle(style2);

                HSSFCell cl42 = rw3.createCell(2);
                cl42.setCellValue("New Defaulted Clients");
                cl42.setCellStyle(style2);

                HSSFCell cl43 = rw3.createCell(3);
                cl43.setCellValue(KMMP5a);
                cl43.setCellStyle(style2);
            }
            //================================================================================================

            if (1 == 1) {
                HSSFRow rw3 = shet.createRow(10);
                rw3.setHeightInPoints(23);
                HSSFCell cl4 = rw3.createCell(1);
                cl4.setCellValue("");
                cl4.setCellStyle(style2);
                HSSFCell cl42 = rw3.createCell(2);
                cl42.setCellValue("Clients Reached");
                cl42.setCellStyle(style2);

                HSSFCell cl43 = rw3.createCell(3);
                cl43.setCellValue(KMMP5b);
                cl43.setCellStyle(style2);
            }
            //================================================================================================
            if (1 == 1) {
                HSSFRow rw3 = shet.createRow(11);
                rw3.setHeightInPoints(23);
                HSSFCell cl4 = rw3.createCell(1);
                cl4.setCellValue("");
                cl4.setCellStyle(style2);

                HSSFCell cl42 = rw3.createCell(2);
                cl42.setCellValue("Successfully resolved");
                cl42.setCellStyle(style2);

                HSSFCell cl43 = rw3.createCell(3);
                cl43.setCellValue(KMMP5c);
                cl43.setCellStyle(style2);
            }

            //================================================================================================

            if (1 == 1) {
                HSSFRow rw3 = shet.createRow(12);
                rw3.setHeightInPoints(23);
                HSSFCell cl4 = rw3.createCell(0);
                cl4.setCellValue("");
                cl4.setCellStyle(style2);

                HSSFCell cl41 = rw3.createCell(1);
                cl41.setCellValue("MOH 731 HV02-05 Known positive status (at entry into ANC) ");
                cl41.setCellStyle(style2);

                HSSFCell cl42 = rw3.createCell(2);
                cl42.setCellValue("");
                cl42.setCellStyle(style2);

                HSSFCell cl43 = rw3.createCell(3);
                cl43.setCellValue(HV0205);
                cl43.setCellStyle(style2);
            }
            //================================================================================================  

            if (1 == 1) {
                HSSFRow rw3 = shet.createRow(13);
                rw3.setHeightInPoints(23);
                HSSFCell cl4 = rw3.createCell(0);
                cl4.setCellValue("");
                cl4.setCellStyle(style2);

                HSSFCell cl41 = rw3.createCell(1);
                cl41.setCellValue("MOH 731 HV02-06 Antenatal");
                cl41.setCellStyle(style2);

                HSSFCell cl42 = rw3.createCell(2);
                cl42.setCellValue("");
                cl42.setCellStyle(style2);

                HSSFCell cl43 = rw3.createCell(3);
                cl43.setCellValue(HV0206);
                cl43.setCellStyle(style2);
            }

            //================================================================================================     
            createdtable += header
                    + "<br/><br/><br/><table class='mytable'   border=\"1\" style=\"font-family:cambria; border-color: #e5e5e5;margin-bottom: 3px; width:500px;\"><tr class='form-actions'><th colspan='3'><b style=\"text-align:center;\"> KMMP OUTPUT DATA</b></th><th>Total</th></tr><tr><td><b> 1 </b></td><td colspan='2'>No of New HIV positive clients enrolled in KMMP Services (ANC and PN) </td><td>"
                    + KMMP1 + "</td></tr>";

            createdtable += "<tr><td>Successfully Resolved</td><td>" + KMMP5c + "</td></tr>";

            createdtable += "<tr><td></td><td colspan='2'>MOH 731 HV02-05 Known positive status (at entry into ANC) :</td><td>"
                    + HV0205 + "</td></tr>";

            createdtable += "<tr><td></td><td colspan='2'>MOH 731 HV02-06 Antenatal:</td><td>" + HV0206
                    + "</td></tr></table> <div class='form-actions'></div>";

        }

        //System.out.println(createdtable);

        if (conn.conn != null) {
            conn.conn.close();
        }
        if (conn.rs != null) {
            conn.rs.close();
        }
        if (conn.rs1 != null) {
            conn.rs1.close();
        }
        if (conn.rs2 != null) {
            conn.rs2.close();
        }
        if (conn.st != null) {
            conn.st.close();
        }
        if (conn.st2 != null) {
            conn.st2.close();
        }

        //#############################################################################################################

        IdGenerator IG = new IdGenerator();
        String createdOn = IG.CreatedOn();

        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=kmmp" + yearmonth + "_Generated_On_" + createdOn + ".xls");
        OutputStream outStream = response.getOutputStream();
        outStream.write(outArray);
        outStream.flush();
        outStream.close();

        //#############################################################################################################

    } catch (SQLException ex) {
        Logger.getLogger(kmmppdf.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:reports.nutritionexcel.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {//ww  w .  ja va  2 s .co  m
        response.setContentType("text/html;charset=UTF-8");
        session = request.getSession();

        dbConn conn = new dbConn();
        //get the existing data for the month, year and facility that is already on session

        String month = "";
        String year = "";
        String facil = "";

        String form = "nutrition";

        //=====================================================================================================

        year = "2015";
        month = "5";
        String county = "";

        String header = "";

        String reportType = "";
        if (request.getParameter("reportType") != null) {
            reportType = request.getParameter("reportType");
        }
        String reportDuration = "";
        if (request.getParameter("reportDuration") != null) {
            reportDuration = request.getParameter("reportDuration");
        }
        if (request.getParameter("year") != null) {
            year = request.getParameter("year");
        }

        if (request.getParameter("facility") != null && reportType.equals("2")) {
            facil = request.getParameter("facility");

            String getfacil = "select SubPartnerNom,CentreSanteId as mflcode from subpartnera where SubPartnerID='"
                    + facil + "'";
            conn.rs = conn.st.executeQuery(getfacil);

            while (conn.rs.next()) {

                header += " FACILITY : " + conn.rs.getString(1).toUpperCase() + "    MFL CODE  :  "
                        + conn.rs.getString(2) + "  ";

            }

        }

        if (request.getParameter("county") != null && reportType.equals("2")) {
            county = request.getParameter("county");

            String getcounty = "select County from county where CountyID='" + county + "'";
            conn.rs = conn.st.executeQuery(getcounty);

            while (conn.rs.next()) {

                header += " COUNTY : " + conn.rs.getString(1).toUpperCase() + " ";

            }

        }

        if (request.getParameter("month") != null && reportDuration.equals("4")) {
            month = request.getParameter("month");

            String getmonth = "select name as monthname from month where id='" + month + "'";
            conn.rs = conn.st.executeQuery(getmonth);

            while (conn.rs.next()) {

                header += " MONTH : " + conn.rs.getString(1).toUpperCase() + " ";

            }

        }

        header += " YEAR : " + year + "";

        String facilitywhere = "";
        String yearwhere = "";
        String monthwhere = "";
        String countywhere = "";
        String duration = "";
        String semi_annual = "";
        String quarter = "";

        //==================================================================================================
        //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

        int yearcopy = Integer.parseInt(year);

        //        reportType="2";
        //        year=2015;
        //        reportDuration="3";
        String yearmonth = "" + year;
        int prevYear = yearcopy - 1;
        int maxYearMonth = 0;
        int monthcopy = 0;
        //        GET REPORT DURATION============================================

        if (reportDuration.equals("1")) {
            yearmonth += "_AnnualReport";
            duration = " " + form + ".yearmonth BETWEEN " + prevYear + "10 AND " + year + "09";
        } else if (reportDuration.equals("2")) {
            semi_annual = request.getParameter("semi_annual");
            //        semi_annual="2";
            if (semi_annual.equals("1")) {
                yearmonth = prevYear + "_Oct_" + year + "_Mar";
                duration = " " + form + ".yearmonth BETWEEN " + prevYear + "10 AND " + year + "03";
            } else {
                yearmonth += "_Apr_Sep";
                duration = " " + form + ".yearmonth BETWEEN " + year + "04 AND " + year + "09";
            }
        }

        else if (reportDuration.equals("3")) {
            String startMonth, endMonth;
            quarter = request.getParameter("quarter");
            //       quarter="3";
            String getMonths = "SELECT months,name FROM quarter WHERE id='" + quarter + "'";
            conn.rs = conn.st.executeQuery(getMonths);
            if (conn.rs.next() == true) {

                String months[] = conn.rs.getString(1).split(",");
                startMonth = months[0];
                endMonth = months[2];
                if (quarter.equals("1")) {
                    duration = " " + form + ".yearmonth BETWEEN " + prevYear + "" + startMonth + " AND "
                            + prevYear + "" + endMonth;
                    yearmonth = prevYear + "_" + conn.rs.getString(2);
                } else {
                    yearmonth = year + "_" + conn.rs.getString(2);
                    duration = " " + form + ".yearmonth BETWEEN " + year + "" + startMonth + " AND " + year + ""
                            + endMonth;
                }
            }
        }

        else if (reportDuration.equals("4")) {
            monthcopy = Integer.parseInt(request.getParameter("month"));

            //     month=5;
            if (monthcopy >= 10) {
                yearmonth = prevYear + "_" + month;
                duration = " " + form + ".yearmonth=" + prevYear + "" + month;
            } else {
                duration = " " + form + ".yearmonth=" + year + "0" + month;
                yearmonth = year + "_(" + month + ")";
            }
        } else {
            duration = "";
        }

        //======================================================================   
        //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

        String getexistingdata = "";

        if (!county.equals("")) {

            countywhere = " and countyid = '" + county + "'";

        }

        if (!facil.equals("") && reportType.equalsIgnoreCase("2")) {

            facilitywhere = " and " + form + ".SubPartnerID = '" + facil + "'";

        }

        //String joinedwhwere=" where 1=1 "+yearwhere+" && "+duration;  

        String joinedwhwere = " where 1=1 " + facilitywhere + "  " + yearwhere + " && " + duration;

        //=====================================================================================================    

        //=====================================================================================================    

        //=====================================================================================================    

        //______________________________________________________________________________________
        //                       NOW CREATE THE WORKSHEETS          
        //______________________________________________________________________________________  

        HSSFWorkbook wb = new HSSFWorkbook();

        //______________________________________________________________________________________
        //______________________________________________________________________________________

        HSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short) 18);
        font.setFontName("Cambria");
        font.setColor((short) 0000);
        CellStyle style = wb.createCellStyle();
        style.setFont(font);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFFont font2 = wb.createFont();
        font2.setFontName("Cambria");
        font2.setColor((short) 0000);
        CellStyle style2 = wb.createCellStyle();
        style2.setFont(font2);
        style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style2.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFCellStyle stborder = wb.createCellStyle();
        stborder.setBorderTop(HSSFCellStyle.BORDER_THIN);
        stborder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        stborder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        stborder.setBorderRight(HSSFCellStyle.BORDER_THIN);
        stborder.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        HSSFCellStyle stylex = wb.createCellStyle();
        stylex.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
        stylex.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        stylex.setBorderTop(HSSFCellStyle.BORDER_THIN);
        stylex.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        stylex.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        stylex.setBorderRight(HSSFCellStyle.BORDER_THIN);
        stylex.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        stylex.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFFont fontx = wb.createFont();
        fontx.setColor(HSSFColor.BLACK.index);
        fontx.setFontName("Cambria");
        fontx.setColor((short) 0000);
        fontx.setBoldweight((short) 07);
        stylex.setFont(fontx);
        stylex.setWrapText(true);

        HSSFSheet shet = wb.createSheet(form);

        //create headers for that worksheet

        HSSFRow rw = shet.createRow(0);
        rw.setHeightInPoints(25);
        HSSFCell cl0 = rw.createCell(0);
        cl0.setCellValue("3.1.9: Nutrition");
        cl0.setCellStyle(stylex);

        for (int a = 1; a <= 6; a++) {
            HSSFCell clx = rw.createCell(a);
            clx.setCellValue("");
            clx.setCellStyle(stylex);
        }

        HSSFRow rw1 = shet.createRow(1);
        rw1.setHeightInPoints(23);
        HSSFCell cl = rw1.createCell(0);
        cl.setCellValue(header);
        cl.setCellStyle(stylex);

        for (int a = 1; a <= 6; a++) {
            HSSFCell clx = rw1.createCell(a);
            clx.setCellValue("");
            clx.setCellStyle(stylex);
        }

        HSSFRow rw2 = shet.createRow(2);
        rw2.setHeightInPoints(23);
        HSSFCell cl3 = rw2.createCell(0);
        cl3.setCellValue("3.1.9.2 Population Based Nutrition Service Delivery");
        cl3.setCellStyle(stylex);
        HSSFCell cl3a = rw2.createCell(1);
        cl3a.setCellValue("");
        cl3a.setCellStyle(stylex);
        HSSFCell cl31 = rw2.createCell(2);
        cl31.setCellValue(
                "Number of people trained in child health care and nutrition through USG-supported health area programs");
        cl31.setCellStyle(style2);

        for (int a = 3; a <= 5; a++) {
            HSSFCell clx = rw2.createCell(a);
            clx.setCellValue("");
            clx.setCellStyle(style2);
        }

        shet.addMergedRegion(new CellRangeAddress(2, 11, 0, 1));
        shet.addMergedRegion(new CellRangeAddress(2, 2, 2, 5));
        shet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
        shet.addMergedRegion(new CellRangeAddress(1, 1, 0, 6));
        shet.addMergedRegion(new CellRangeAddress(12, 20, 0, 0));

        shet.setColumnWidth(0, 3000);
        shet.setColumnWidth(1, 3000);
        shet.setColumnWidth(2, 16000);
        shet.setColumnWidth(3, 6900);
        shet.setColumnWidth(4, 3000);
        shet.setColumnWidth(5, 3000);

        getexistingdata = "select  sum(MCHCCNtrTM) as MCHCCNtrTM,    MCHCCNtrTMC,    sum(MCHCCNtrTF) as MCHCCNtrTF,    MCHCCNtrTFC,   sum(MCHCCNtrTT) as MCHCCNtrTT,    MCHCCNtrTTC,    sum(MCHNtrnCHWTrain) as MCHNtrnCHWTrain,   sum(MCHNutChRch) as MCHNutChRch,   sum(MCHNtrnWasted) as MCHNtrnWasted,   sum(MCHNtrnUnderweight) as MCHNtrnUnderweight,   sum(MCHChild5D) as MCHChild5D,   sum(MCHNtrnHealthFacility) as MCHNtrnHealthFacility,   sum(MCHVaccVitA) as MCHVaccVitA,   sum(MCHNtrnFoodOVC) as MCHNtrnFoodOVC,   sum(MCHNtrnFoodPLHIV) as MCHNtrnFoodPLHIV,   sum(MCHNtrnFood) as MCHNtrnFood,   sum(C51DCM) as C51DCM,   sum(C51DCF) as C51DCF,   sum(C51DC) as C51DC,    sum(C51DAM) as C51DAM,   sum(C51DAF) as C51DAF,   sum(C51DA) as C51DA,    sum(C51DP) as C51DP,    sum(C51DMT) as C51DMT,    sum(C51DFT) as C51DFT,    sum(C51DT) as C51DT  from "
                + form
                + " join ( subpartnera join (district join county on county.CountyID=district.CountyID ) on district.DistrictID = subpartnera.DistrictID )  on "
                + form + ".SubPartnerID = subpartnera.SubPartnerID   " + joinedwhwere + "  ";

        //System.out.println(getexistingdata);

        String MCHCCNtrTM = "";
        String MCHCCNtrTF = "";
        String MCHCCNtrTT = "";

        String MCHCCNtrTMC = "0";
        String MCHCCNtrTFC = "0";
        String MCHCCNtrTTC = "0";

        String MCHCCNtrTMCH = "0";
        String MCHCCNtrTFCH = "0";
        String MCHCCNtrTTCH = "0";

        String MCHNtrnCHWTrain = "";
        String MCHNutChRch = "";
        String MCHNtrnWasted = "";
        String MCHNtrnUnderweight = "";
        String MCHChild5D = "";
        String MCHNtrnHealthFacility = "";
        String MCHVaccVitA = "";
        String MCHNtrnFoodOVC = "";
        String MCHNtrnFoodPLHIV = "";
        String MCHNtrnFood = "";
        String C51DCM = "";
        String C51DCF = "";
        String C51DC = "";
        String C51DAM = "";
        String C51DAF = "";
        String C51DA = "";
        String C51DP = "";
        String C51DMT = "";
        String C51DFT = "";
        String C51DT = "";

        String distid = "";

        if (session.getAttribute("subcountyid") != null) {
            distid = session.getAttribute("subcountyid").toString();
        }

        int counter = 0;

        conn.rs = conn.st.executeQuery(getexistingdata);
        while (conn.rs.next()) {
            //now check if form was updated and if its one month after data entry
            //now load the column values here

            MCHCCNtrTM = conn.rs.getString("MCHCCNtrTM");
            if (MCHCCNtrTM == null) {
                MCHCCNtrTM = "";
            }

            MCHCCNtrTF = conn.rs.getString("MCHCCNtrTF");
            if (MCHCCNtrTF == null) {
                MCHCCNtrTF = "";
            }

            MCHCCNtrTT = conn.rs.getString("MCHCCNtrTT");
            if (MCHCCNtrTT == null) {
                MCHCCNtrTT = "";
            }

            MCHCCNtrTMC = conn.rs.getString("MCHCCNtrTMC");
            if (MCHCCNtrTMC == null) {
                MCHCCNtrTMC = "0";
            }

            MCHCCNtrTFC = conn.rs.getString("MCHCCNtrTFC");
            if (MCHCCNtrTFC == null) {
                MCHCCNtrTFC = "0";
            }

            MCHCCNtrTTC = conn.rs.getString("MCHCCNtrTTC");
            if (MCHCCNtrTTC == null) {
                MCHCCNtrTTC = "0";
            }

            MCHNtrnCHWTrain = conn.rs.getString("MCHNtrnCHWTrain");
            if (MCHNtrnCHWTrain == null) {
                MCHNtrnCHWTrain = "";
            }

            MCHNutChRch = conn.rs.getString("MCHNutChRch");
            if (MCHNutChRch == null) {
                MCHNutChRch = "";
            }

            MCHNtrnWasted = conn.rs.getString("MCHNtrnWasted");
            if (MCHNtrnWasted == null) {
                MCHNtrnWasted = "";
            }

            MCHNtrnUnderweight = conn.rs.getString("MCHNtrnUnderweight");
            if (MCHNtrnUnderweight == null) {
                MCHNtrnUnderweight = "";
            }

            MCHChild5D = conn.rs.getString("MCHChild5D");
            if (MCHChild5D == null) {
                MCHChild5D = "";
            }

            MCHNtrnHealthFacility = conn.rs.getString("MCHNtrnHealthFacility");
            if (MCHNtrnHealthFacility == null) {
                MCHNtrnHealthFacility = "";
            }

            MCHVaccVitA = conn.rs.getString("MCHVaccVitA");
            if (MCHVaccVitA == null) {
                MCHVaccVitA = "";
            }

            MCHNtrnFoodOVC = conn.rs.getString("MCHNtrnFoodOVC");
            if (MCHNtrnFoodOVC == null) {
                MCHNtrnFoodOVC = "";
            }

            MCHNtrnFoodPLHIV = conn.rs.getString("MCHNtrnFoodPLHIV");
            if (MCHNtrnFoodPLHIV == null) {
                MCHNtrnFoodPLHIV = "";
            }

            MCHNtrnFood = conn.rs.getString("MCHNtrnFood");
            if (MCHNtrnFood == null) {
                MCHNtrnFood = "";
            }

            C51DCM = conn.rs.getString("C51DCM");
            if (C51DCM == null) {
                C51DCM = "";
            }

            C51DCF = conn.rs.getString("C51DCF");
            if (C51DCF == null) {
                C51DCF = "";
            }

            C51DC = conn.rs.getString("C51DC");
            if (C51DC == null) {
                C51DC = "";
            }

            C51DAM = conn.rs.getString("C51DAM");
            if (C51DAM == null) {
                C51DAM = "";
            }

            C51DAF = conn.rs.getString("C51DAF");
            if (C51DAF == null) {
                C51DAF = "";
            }

            C51DA = conn.rs.getString("C51DA");
            if (C51DA == null) {
                C51DA = "";
            }

            C51DP = conn.rs.getString("C51DP");
            if (C51DP == null) {
                C51DP = "";
            }

            C51DMT = conn.rs.getString("C51DMT");
            if (C51DMT == null) {
                C51DMT = "";
            }

            C51DFT = conn.rs.getString("C51DFT");
            if (C51DFT == null) {
                C51DFT = "";
            }

            C51DT = conn.rs.getString("C51DT");
            if (C51DT == null) {
                C51DT = "";
            }

        }

        String createdtable = "";

        if (1 == 1) {

            int r = 3;

            HSSFCell cl3d = rw2.createCell(6);
            cl3d.setCellValue(MCHCCNtrTTC);
            cl3d.setCellStyle(style2);

            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(style2);
                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue("Number of Men");
                clx.setCellStyle(style2);

                HSSFCell clx1 = rwx.createCell(6);
                clx1.setCellValue(MCHCCNtrTM);
                clx1.setCellStyle(style2);

                for (int a = 3; a <= 5; a++) {
                    HSSFCell clx2 = rwx.createCell(a);
                    clx2.setCellValue("");
                    clx2.setCellStyle(style2);
                    shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));
                }
                r++;
            }

            //==================================================================================================================

            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(style2);
                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue("Number of Women");
                clx.setCellStyle(style2);

                HSSFCell clx1 = rwx.createCell(6);
                clx1.setCellValue(MCHCCNtrTF);
                clx1.setCellStyle(style2);

                for (int a = 3; a <= 5; a++) {
                    HSSFCell clx2 = rwx.createCell(a);
                    clx2.setCellValue("");
                    clx2.setCellStyle(style2);
                    shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));
                }
                r++;
            }

            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(style2);
                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue(
                        "Total Number of people trained in child health care and nutrition through USG-supported health area programs");
                clx.setCellStyle(style2);

                HSSFCell clx1 = rwx.createCell(6);
                clx1.setCellValue(MCHCCNtrTT);
                clx1.setCellStyle(style2);

                for (int a = 3; a <= 5; a++) {
                    HSSFCell clx2 = rwx.createCell(a);
                    clx2.setCellValue("");
                    clx2.setCellStyle(style2);
                    shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));
                }
                r++;
            }

            //============================================================================================   

            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(style2);
                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue("Number of Community health workers trained in child health and/or nutrition");
                clx.setCellStyle(style2);

                HSSFCell clx1 = rwx.createCell(6);
                clx1.setCellValue(MCHNtrnCHWTrain);
                clx1.setCellStyle(style2);

                for (int a = 3; a <= 5; a++) {
                    HSSFCell clx2 = rwx.createCell(a);
                    clx2.setCellValue("");
                    clx2.setCellStyle(style2);
                    shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));
                }
                r++;
            }

            //===========================================================================================================

            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(style2);
                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue("Number of children reached by USG-supported nutrition programs");
                clx.setCellStyle(style2);

                HSSFCell clx1 = rwx.createCell(6);
                clx1.setCellValue(MCHNutChRch);
                clx1.setCellStyle(style2);

                for (int a = 3; a <= 5; a++) {
                    HSSFCell clx2 = rwx.createCell(a);
                    clx2.setCellValue("");
                    clx2.setCellStyle(style2);
                    shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));
                }
                r++;
            }

            //===========================================================================================================

            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(style2);
                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue(
                        "Total number of children under five who are wasted (with weight for height Z score < - 2)");
                clx.setCellStyle(style2);

                HSSFCell clx1 = rwx.createCell(6);
                clx1.setCellValue(MCHNtrnWasted);
                clx1.setCellStyle(style2);

                for (int a = 3; a <= 5; a++) {
                    HSSFCell clx2 = rwx.createCell(a);
                    clx2.setCellValue("");
                    clx2.setCellStyle(style2);
                    shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));
                }
                r++;
            }

            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(style2);
                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue(
                        "Total number of children under five who are underweight (with weight for age Z score < - 2) (see Indicator");
                clx.setCellStyle(style2);

                HSSFCell clx1 = rwx.createCell(6);
                clx1.setCellValue(MCHNtrnUnderweight);
                clx1.setCellStyle(style2);

                for (int a = 3; a <= 5; a++) {
                    HSSFCell clx2 = rwx.createCell(a);
                    clx2.setCellValue("");
                    clx2.setCellStyle(style2);
                    shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));
                }
                r++;
            }
            //===========================================================================================================

            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(style2);
                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue("Total number of children under five years");
                clx.setCellStyle(style2);

                HSSFCell clx1 = rwx.createCell(6);
                clx1.setCellValue(MCHChild5D);
                clx1.setCellStyle(style2);

                for (int a = 3; a <= 5; a++) {
                    HSSFCell clx2 = rwx.createCell(a);
                    clx2.setCellValue("");
                    clx2.setCellStyle(style2);
                    shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));
                }
                r++;
            }
            //===========================================================================================================

            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(style2);
                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue(
                        "Number of health facilities with established capacity to manage acute under-nutrition");
                clx.setCellStyle(style2);

                HSSFCell clx1 = rwx.createCell(6);
                clx1.setCellValue(MCHNtrnHealthFacility);
                clx1.setCellStyle(style2);

                for (int a = 3; a <= 5; a++) {
                    HSSFCell clx2 = rwx.createCell(a);
                    clx2.setCellValue("");
                    clx2.setCellStyle(style2);
                    shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));
                }
                r++;
            }
            //===========================================================================================================

            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("HIV and Nutrition");
                clx0.setCellStyle(stylex);

                HSSFCell clx1a = rwx.createCell(1);
                clx1a.setCellValue("");
                clx1a.setCellStyle(stylex);
                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue(
                        "Number of children under 5 years of age who received Vitamin A from USG-supported programs");
                clx.setCellStyle(style2);

                HSSFCell clx1 = rwx.createCell(6);
                clx1.setCellValue(MCHVaccVitA);
                clx1.setCellStyle(style2);

                for (int a = 3; a <= 5; a++) {
                    HSSFCell clx2 = rwx.createCell(a);
                    clx2.setCellValue("");
                    clx2.setCellStyle(style2);
                    shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));

                    //shet.addMergedRegion(new CellRangeAddress(2,11,0,0));
                }
                r++;
            }
            //===========================================================================================================

            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(stylex);
                HSSFCell clx1a = rwx.createCell(1);
                clx1a.setCellValue("C2.3.D:");
                clx1a.setCellStyle(stylex);
                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue(
                        "Number of HIV  positive clinically malnourished clients who received therapeutic and/or supplementary food < 18");
                clx.setCellStyle(style2);

                HSSFCell clx1 = rwx.createCell(6);
                clx1.setCellValue(MCHNtrnFoodOVC);
                clx1.setCellStyle(style2);

                for (int a = 3; a <= 5; a++) {
                    HSSFCell clx2 = rwx.createCell(a);
                    clx2.setCellValue("");
                    clx2.setCellStyle(style2);
                    shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));

                    shet.addMergedRegion(new CellRangeAddress(r, r + 2, 1, 1));
                }
                r++;
            }
            //===========================================================================================================    

            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(stylex);
                HSSFCell clx1a = rwx.createCell(1);
                clx1a.setCellValue("");
                clx1a.setCellStyle(stylex);
                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue(
                        "Number of HIV  positive clinically malnourished clients who received therapeutic and/or supplementary food 18+ (PLHIV)");
                clx.setCellStyle(style2);

                HSSFCell clx1 = rwx.createCell(6);
                clx1.setCellValue(MCHNtrnFoodPLHIV);
                clx1.setCellStyle(style2);

                for (int a = 3; a <= 5; a++) {
                    HSSFCell clx2 = rwx.createCell(a);
                    clx2.setCellValue("");
                    clx2.setCellStyle(style2);
                    shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));

                    // shet.addMergedRegion(new CellRangeAddress(r,r+3,1,1));
                }
                r++;
            }

            //=========================================================================================================== 

            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(stylex);
                HSSFCell clx1a = rwx.createCell(1);
                clx1a.setCellValue("");
                clx1a.setCellStyle(stylex);
                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue(
                        "Number of HIV  positive clinically malnourished clients who received therapeutic and/or supplementary food - Total");
                clx.setCellStyle(style2);

                HSSFCell clx1 = rwx.createCell(6);
                clx1.setCellValue(MCHNtrnFood);
                clx1.setCellStyle(style2);

                for (int a = 3; a <= 5; a++) {
                    HSSFCell clx2 = rwx.createCell(a);
                    clx2.setCellValue("");
                    clx2.setCellStyle(style2);
                    shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));

                    // shet.addMergedRegion(new CellRangeAddress(r,r+3,1,1));
                }
                r++;
            }
            //===========================================================================================================      
            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(stylex);
                HSSFCell clx1a = rwx.createCell(1);
                clx1a.setCellValue("");
                clx1a.setCellStyle(stylex);
                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue("");
                clx.setCellStyle(style2);
                HSSFCell clx3 = rwx.createCell(3);
                clx3.setCellValue("");
                clx3.setCellStyle(style2);
                HSSFCell clx1 = rwx.createCell(6);
                clx1.setCellValue("");
                clx1.setCellStyle(style2);

                String haeade[] = { "Male", "Female", "Total" };

                for (int a = 4; a <= 6; a++) {
                    HSSFCell clx2 = rwx.createCell(a);
                    clx2.setCellValue(haeade[a - 4]);
                    clx2.setCellStyle(style2);
                    //shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));

                    // shet.addMergedRegion(new CellRangeAddress(r,r+3,1,1));
                }
                shet.addMergedRegion(new CellRangeAddress(r, r, 2, 3));
                r++;
            }
            //=========================================================================================================== 
            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(stylex);
                HSSFCell clx1a = rwx.createCell(1);
                clx1a.setCellValue("C5.1.D:");
                clx1a.setCellStyle(stylex);

                HSSFCell clx1b = rwx.createCell(3);
                clx1b.setCellValue("< 18");
                clx1b.setCellStyle(style2);

                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue(
                        "Number of eligible clients who received food and / or other nutrition Services");
                clx.setCellStyle(style2);

                String haeade[] = { C51DCM, C51DCF, C51DC };

                for (int a = 0; a < haeade.length; a++) {
                    HSSFCell clx2 = rwx.createCell(a + 4);
                    clx2.setCellValue(haeade[a]);
                    clx2.setCellStyle(style2);
                    //shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));

                    // shet.addMergedRegion(new CellRangeAddress(r,r+3,1,1));
                }
                //shet.addMergedRegion(new CellRangeAddress(r,r,2,3));       
                shet.addMergedRegion(new CellRangeAddress(r, r + 3, 1, 1));
                shet.addMergedRegion(new CellRangeAddress(r, r + 3, 2, 2));

                r++;
            }
            //===========================================================================================================    

            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(stylex);
                HSSFCell clx1a = rwx.createCell(1);
                clx1a.setCellValue("");
                clx1a.setCellStyle(stylex);

                HSSFCell clx1b = rwx.createCell(3);
                clx1b.setCellValue(">=18");
                clx1b.setCellStyle(style2);

                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue("");
                clx.setCellStyle(style2);

                String haeade[] = { C51DAM, C51DAF, C51DA };

                for (int a = 0; a < haeade.length; a++) {
                    HSSFCell clx2 = rwx.createCell(a + 4);
                    clx2.setCellValue(haeade[a]);
                    clx2.setCellStyle(style2);
                    //shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));

                    // shet.addMergedRegion(new CellRangeAddress(r,r+3,1,1));
                }
                //shet.addMergedRegion(new CellRangeAddress(r,r,2,3));       

                r++;
            }
            //===========================================================================================================    

            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(stylex);
                HSSFCell clx1a = rwx.createCell(1);
                clx1a.setCellValue("");
                clx1a.setCellStyle(stylex);

                HSSFCell clx1b = rwx.createCell(3);
                clx1b.setCellValue("Pregnant/Lactating (PMTCT 1.5)");
                clx1b.setCellStyle(style2);

                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue("");
                clx.setCellStyle(style2);

                String haeade[] = { "", "", C51DP };

                for (int a = 0; a < haeade.length; a++) {
                    HSSFCell clx2 = rwx.createCell(a + 4);
                    clx2.setCellValue(haeade[a]);
                    clx2.setCellStyle(style2);
                    //shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));

                    // shet.addMergedRegion(new CellRangeAddress(r,r+3,1,1));
                }
                shet.addMergedRegion(new CellRangeAddress(r, r, 4, 5));

                r++;
            }
            //===========================================================================================================    
            if (1 == 1) {

                HSSFRow rwx = shet.createRow(r);
                rwx.setHeightInPoints(23);
                HSSFCell clx0 = rwx.createCell(0);
                clx0.setCellValue("");
                clx0.setCellStyle(stylex);
                HSSFCell clx1a = rwx.createCell(1);
                clx1a.setCellValue("");
                clx1a.setCellStyle(stylex);

                HSSFCell clx1b = rwx.createCell(3);
                clx1b.setCellValue("Total");
                clx1b.setCellStyle(style2);

                HSSFCell clx = rwx.createCell(2);
                clx.setCellValue("");
                clx.setCellStyle(style2);

                String haeade[] = { C51DMT, C51DFT, C51DT };

                for (int a = 0; a < haeade.length; a++) {
                    HSSFCell clx2 = rwx.createCell(a + 4);
                    clx2.setCellValue(haeade[a]);
                    clx2.setCellStyle(style2);
                    //shet.addMergedRegion(new CellRangeAddress(r, r, 2, 5));

                    // shet.addMergedRegion(new CellRangeAddress(r,r+3,1,1));
                }
                //shet.addMergedRegion(new CellRangeAddress(r,r,2,3));       

                r++;
            }
            //===========================================================================================================    

            createdtable += header
                    + "<br/><table   border='1' style='border-color: #e5e5e5;margin-bottom: 3px;font-size:11;'><tr class='form-actions'>"
                    + "<th rowspan='10' colspan='2'><b style='text-align:center;'>3.1.9.2 <br/> population-based Nutrition Service Delivery</b></th>"
                    + "<td colspan='4'><b>Number of People trained in child health care and nutrition through USG-supported health area programs</b></td><td><b>"
                    + MCHCCNtrTTC + "</b></td></tr>";
            createdtable += "<tr><td colspan='4' style='text-align:left;'>No of Men </td><td>" + MCHCCNtrTM
                    + "</td></tr>";
            createdtable += "<tr><td colspan='4' style='text-align:left;'>No of Women </td><td>" + MCHCCNtrTF
                    + "</td></tr>";
            createdtable += "<tr><td colspan='4'><b>Total Number of people trained in child health care and nutrition through USG-supported health area programs</b></td><td>"
                    + MCHCCNtrTT + "</td></tr>";
            createdtable += "<tr><td colspan='4'><b>Number of Community health workers trained in child health and/or nutrition</b></td><td>"
                    + MCHNtrnCHWTrain + "</td></tr>";
            createdtable += "<tr><td colspan='4'><b>Number of children reached by USG-supported nutrition programs</b></td><td>"
                    + MCHNutChRch + "</td></tr>";
            createdtable += "<tr><td colspan='4'>Total number of children under five who are wasted (with weight for height Z score < - 2)</td><td>"
                    + MCHNtrnWasted + "</td></tr>";
            createdtable += "<tr><td colspan='4'>Total number of children under five who are underweight (with weight for age Z score < - 2) (SEE Indicator </td><td>"
                    + MCHNtrnUnderweight + "</td></tr>";
            createdtable += "<tr><td colspan='4'>Total number of children under five years</td><td>"
                    + MCHChild5D + "</td></tr>";
            createdtable += "<tr><td colspan='4'>Number of health facilities with established capacity to manage acute under-nutrition</td><td>"
                    + MCHNtrnHealthFacility + "</td></tr>";

            createdtable += "<tr><td colspan='1' rowspan='9'>HIV and Nutrition</td><td></td> <td colspan='4'> <b> Number of children under 5 years of age who received Vitamin A from USG-supported programs </b> </td><td>"
                    + MCHVaccVitA + "</td></tr>";
            createdtable += "<tr><td rowspan='3' colspan='1'> <b> C2.3.D </b> </td> <td colspan='4'>Number of HIV  positive clinically malnourished clients who received therapeutic and/or supplementary food < 18 </td><td>"
                    + MCHNtrnFoodOVC + "</td></tr>";
            createdtable += "<tr><td colspan='4'>Number of HIV  positive clinically malnourished clients who received therapeutic and/or supplementary food 18+ (PLHIV)</td><td>"
                    + MCHNtrnFoodPLHIV + "</td></tr>";
            createdtable += "<tr><td colspan='4'>Number of HIV  positive clinically malnourished clients who received therapeutic and/or supplementary food -<b> Total</b></td><td>"
                    + MCHNtrnFood + "</td></tr>";

            createdtable += "<tr> <td></td><td></td><td></td><td><b>Male</b></td><td><b>Female</b></td><td><b>Total</b></td></tr>";
            createdtable += "<tr><td rowspan='4' colspan='1'> <b>C5.1.D </b> </td> <td colspan='1' rowspan='4'>Number of eligible clients who received food and / or other nutrition Services</td><td> <b> less Than 18 </b>  </td> <td>"
                    + C51DCM + "</td><td>" + C51DCF + "</td><td>" + C51DC + "</td></tr>";
            createdtable += "<tr><td> <b> >=18 </b> </td> <td>" + C51DAM + "</td><td>" + C51DAF + "</td><td>"
                    + C51DA + "</td></tr>";
            createdtable += "<tr><td colspan='3'> <b> Pregnant/Lactating (PMTCT 1.5)</b> </td><td>" + C51DP
                    + "</td></tr>";
            createdtable += "<tr><td> <b> Total </b>  </td> <td> " + C51DMT + " </td> <td> " + C51DFT
                    + " </td> <td> " + C51DT + " </td></tr></table>";

        }

        System.out.println(createdtable);

        if (conn.conn != null) {
            conn.conn.close();
        }
        if (conn.rs != null) {
            conn.rs.close();
        }
        if (conn.rs1 != null) {
            conn.rs1.close();
        }
        if (conn.rs2 != null) {
            conn.rs2.close();
        }
        if (conn.st != null) {
            conn.st.close();
        }
        if (conn.st2 != null) {
            conn.st2.close();
        }

        IdGenerator IG = new IdGenerator();
        String createdOn = IG.CreatedOn();

        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=" + form + yearmonth + "_Generated_On_" + createdOn + ".xls");
        OutputStream outStream = response.getOutputStream();
        outStream.write(outArray);
        outStream.flush();
        outStream.close();

    } catch (SQLException ex) {
        Logger.getLogger(Vmmcpdf.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:reports.receivedMessageDIC.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, SQLException {
    session = request.getSession();//w ww  . ja  v a  2 s. c  om
    dbConn conn = new dbConn();
    i = 4;
    startDate = session.getAttribute("custstartDate").toString();
    endDate = session.getAttribute("custendDate").toString();

    //            ^^^^^^^^^^^^^CREATE STATIC AND WRITE STATIC DATA TO THE EXCELL^^^^^^^^^^^^
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet shet1 = wb.createSheet();
    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short) 18);
    font.setFontName("Arial Black");
    font.setColor((short) 0000);
    CellStyle style = wb.createCellStyle();
    style.setFont(font);
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    HSSFFont font2 = wb.createFont();
    font2.setFontName("Arial Black");
    font2.setColor((short) 0000);
    CellStyle style2 = wb.createCellStyle();
    style2.setFont(font2);

    HSSFCellStyle stborder = wb.createCellStyle();
    stborder.setBorderTop(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderRight(HSSFCellStyle.BORDER_THIN);
    stborder.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    shet1.setColumnWidth(0, 5000);
    shet1.setColumnWidth(1, 5000);
    shet1.setColumnWidth(2, 5000);
    shet1.setColumnWidth(3, 5500);

    shet1.setColumnWidth(4, 10);

    shet1.setColumnWidth(5, 10);
    shet1.setColumnWidth(6, 10);
    shet1.setColumnWidth(7, 10);
    shet1.setColumnWidth(8, 10);
    shet1.setColumnWidth(9, 10);
    shet1.setColumnWidth(10, 10);
    shet1.setColumnWidth(11, 10);
    shet1.setColumnWidth(12, 10);

    shet1.setColumnWidth(13, 5000);

    shet1.setColumnWidth(14, 5300);
    shet1.setColumnWidth(15, 5000);
    shet1.setColumnWidth(16, 5200);
    shet1.setColumnWidth(17, 5200);
    shet1.setColumnWidth(18, 5200);
    shet1.setColumnWidth(19, 5800);
    shet1.setColumnWidth(20, 5000);
    shet1.setColumnWidth(21, 5300);

    shet1.setColumnWidth(22, 5300);
    shet1.setColumnWidth(23, 5000);
    shet1.setColumnWidth(24, 5200);
    shet1.setColumnWidth(25, 5200);

    //    shet1.setColumnWidth(26, 5200); 
    //    shet1.setColumnWidth(27, 5800); 
    //    shet1.setColumnWidth(28, 5000); 
    //    shet1.setColumnWidth(29, 5300);
    //    shet1.setColumnWidth(30, 5800); 
    //    shet1.setColumnWidth(31, 5000); 
    //    shet1.setColumnWidth(32, 5300);

    //    shet1.setColumnWidth(20, 2000);
    HSSFCellStyle styleBorder = wb.createCellStyle();
    styleBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);
    styleBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    styleBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    styleBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);
    styleBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    HSSFRow rw1 = shet1.createRow(1);
    HSSFCell cell;
    HSSFRow rw4 = shet1.createRow(0);
    rw4.setHeightInPoints(45);
    rw4.setRowStyle(style2);
    // rw4.createCell(1).setCellValue("Number");
    HSSFCell cell0, cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9, cell10, cell11, cell12,
            cell13, cell14, cell15, cell16, cell17, cell18, cell19, cell20;
    HSSFCell cell21, cell22, cell23, cell24, cell25, cell26, cell27, cell28, cell29, cell30, cell31, cell32,
            cell33;

    cell0 = rw4.createCell(0);
    cell1 = rw4.createCell(1);
    cell2 = rw4.createCell(2);
    cell3 = rw4.createCell(3);
    //   cell4=rw4.createCell(4);
    //   cell5=rw4.createCell(5);
    cell13 = rw4.createCell(13);
    cell14 = rw4.createCell(14);
    cell15 = rw4.createCell(15);
    cell16 = rw4.createCell(16);
    cell17 = rw4.createCell(17);
    cell18 = rw4.createCell(18);
    cell19 = rw4.createCell(19);
    cell20 = rw4.createCell(20);
    cell21 = rw4.createCell(21);
    cell22 = rw4.createCell(22);
    cell23 = rw4.createCell(23);
    cell24 = rw4.createCell(24);
    cell25 = rw4.createCell(25);
    //   
    //   cell26=rw4.createCell(26);
    //   cell27=rw4.createCell(27);
    //   cell28=rw4.createCell(28);
    //   cell29=rw4.createCell(29);
    //   cell30=rw4.createCell(30);
    //   cell31=rw4.createCell(31);
    //   cell32=rw4.createCell(32);
    //   cell33=rw4.createCell(33);

    cell0.setCellValue("COUNTY NAME");
    cell1.setCellValue("PARTNER NAME");
    cell2.setCellValue("DISTRICT NAME");
    cell3.setCellValue("DIC");
    // cell4.setCellValue("MESSAGE NO");
    // cell5.setCellValue("ACHIEVED");

    cell13.setCellValue("Knowledge of HIV Status");
    cell14.setCellValue("Partner HIV Testing");
    cell15.setCellValue("Child HIV Testing");
    cell16.setCellValue("Discordance");
    cell17.setCellValue("HIV Disclosure");
    cell18.setCellValue("Risk Factor/Reduction");
    cell19.setCellValue("Condom Use");
    cell20.setCellValue("Alcohol and Substance Abuse");

    cell21.setCellValue("Adherence");
    cell22.setCellValue("STIs");
    cell23.setCellValue("Family Planning");
    cell24.setCellValue("PMTCT");
    cell25.setCellValue("TB");

    // cell26.setCellValue("Received Contraceptives");
    // cell27.setCellValue("Reffered To Service Point");
    // cell28.setCellValue("Given Condoms");
    // cell29 .setCellValue("Screened For TB");
    // cell30.setCellValue("Screened For STIs");
    // cell31.setCellValue("Partner Tested");
    // cell32.setCellValue("Children Tested");
    // cell33.setCellValue("Disclosed Status");

    HSSFCellStyle stylex = wb.createCellStyle();
    stylex.setFillForegroundColor(HSSFColor.LIME.index);
    stylex.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    stylex.setBorderTop(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderRight(HSSFCellStyle.BORDER_THIN);
    stylex.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    HSSFFont fontx = wb.createFont();
    fontx.setColor(HSSFColor.DARK_BLUE.index);
    stylex.setFont(fontx);
    stylex.setWrapText(true);

    cell0.setCellStyle(stylex);
    cell1.setCellStyle(stylex);
    cell2.setCellStyle(stylex);
    cell3.setCellStyle(stylex);
    //cell4.setCellStyle(stylex);
    //cell5.setCellStyle(stylex);
    //cell6.setCellStyle(stylex);
    //cell7.setCellStyle(stylex);
    //cell8.setCellStyle(stylex);
    //cell9.setCellStyle(stylex);
    //cell10.setCellStyle(stylex);
    //cell11.setCellStyle(stylex);
    //cell12.setCellStyle(stylex);
    cell13.setCellStyle(stylex);
    cell14.setCellStyle(stylex);
    cell15.setCellStyle(stylex);
    cell16.setCellStyle(stylex);
    cell17.setCellStyle(stylex);
    cell18.setCellStyle(stylex);
    cell19.setCellStyle(stylex);
    cell20.setCellStyle(stylex);
    cell21.setCellStyle(stylex);
    cell22.setCellStyle(stylex);
    cell23.setCellStyle(stylex);
    cell24.setCellStyle(stylex);
    cell25.setCellStyle(stylex);
    //cell26.setCellStyle(stylex);
    //cell27.setCellStyle(stylex);
    //cell28.setCellStyle(stylex);
    //cell29.setCellStyle(stylex);
    //cell30.setCellStyle(stylex);
    //cell31.setCellStyle(stylex);
    //cell32.setCellStyle(stylex); 
    //cell33.setCellStyle(stylex);

    i = 1;
    current_group = "";
    String getClients = "SELECT "
            + "DATE_FORMAT( NOW( ) , '%Y' ) - DATE_FORMAT( personal_information.dob, '%Y' )-( DATE_FORMAT( NOW( ),'YYYY-%mm-%dd' )< DATE_FORMAT( personal_information.dob, 'YYYY-%mm-%dd' ) )"
            + ",personal_information.gender,district.district_name,partner.partner_name,county.county_name,"
            + "register2.session_no,SUM(register2.value),dic.dic_name " + " FROM personal_information "
            + "LEFT JOIN dic ON personal_information.dic_id=dic.dic_id "
            + "LEFT JOIN district ON personal_information.district_id=district.district_id "
            + "LEFT JOIN county ON district.county_id=county.county_id "
            + "LEFT JOIN partner ON personal_information.partner_id=partner.partner_id "
            + "LEFT JOIN register2 ON personal_information.client_id=register2.client_id "
            + " WHERE register2.value<2 && STR_TO_DATE(register2.date,'%m/%d/%Y') BETWEEN STR_TO_DATE('"
            + startDate + "','%m/%d/%Y') AND STR_TO_DATE('" + endDate + "','%m/%d/%Y')  "
            + " GROUP BY county.county_name,partner.partner_name,district.district_name,dic.dic_name,register2.session_no ORDER BY partner.partner_name,district.district_name,dic.dic_name";
    conn.rs = conn.st.executeQuery(getClients);
    while (conn.rs.next()) {
        county = district = hf = partner = groupname = serviceprovider = clientname = age = gender = groupings = year = providerid = "";
        countyid = districtid = hfid = partnerid = groupid = serviceproviderid = clientid = "";
        s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = s9 = s10 = s11 = s12 = s13 = "";
        cm = rsp = cd = tb = sti = testedpartner = testedchild = session_no = value = status;
        sess = val = cds = 0;
        hf_id = lessons_attended = national_id = ccc_no = mobile_no = dob = "";
        added = 0;
        message_no = achieved = 0;
        dic_name = "";
        age = conn.rs.getString(1);
        gender = conn.rs.getString(2);
        district = conn.rs.getString(3);
        partner = conn.rs.getString(4);

        county = conn.rs.getString(5);
        message_no = conn.rs.getInt(6);
        achieved = conn.rs.getInt(7);
        dic_name = conn.rs.getString(8);
        //      OUTPUT ATTENDED-------------------------------- 
        current_group = district;
        cm = rsp = tb = sti = testedpartner = testedchild = session_no = value = status = "NO";
        if (current_group.equals("")) {
            previous_group = current_group;
            //  OUTPUT SERVICES PROVIDED================================     
            HSSFRow rw4x = shet1.createRow(i);
            rw4.setHeightInPoints(45);
            rw4.setRowStyle(style2);
            // rw4.createCell(1).setCellValue("Number");
            HSSFCell cell0x, cell1x, cell2x, cell3x, cell4x, cell5x, cell6x, cell7x, cell8x, cell9x, cell10x,
                    cell11x, cell12x, cell13x, cell14x, cell15x, cell16x, cell17x, cell18x, cell19x, cell20x;
            HSSFCell cell21x, cell22x, cell23x, cell24x, cell25x, cell26x, cell27x, cell28x, cell29x, cell30x,
                    cell31x, cell32x, cell33x;
            cell0x = rw4x.createCell(0);
            cell1x = rw4x.createCell(1);
            cell2x = rw4x.createCell(2);
            cell3x = rw4x.createCell(3);
            //   cell4x=rw4x.createCell(4);
            //   cell5x=rw4x.createCell(5);
            cell13x = rw4x.createCell(13);
            cell14x = rw4x.createCell(14);
            cell15x = rw4x.createCell(15);
            cell16x = rw4x.createCell(16);
            cell17x = rw4x.createCell(17);
            cell18x = rw4x.createCell(18);
            cell19x = rw4x.createCell(19);
            cell20x = rw4x.createCell(20);
            cell21x = rw4x.createCell(21);
            cell22x = rw4x.createCell(22);
            cell23x = rw4x.createCell(23);
            cell24x = rw4x.createCell(24);
            cell25x = rw4x.createCell(25);
            //   cell26x=rw4x.createCell(26);
            //   cell27x=rw4x.createCell(27);
            //   cell28x=rw4x.createCell(28);
            //   cell29x=rw4x.createCell(29);
            //   cell30x=rw4x.createCell(30);
            //   cell31x=rw4x.createCell(31);
            //   cell32x=rw4x.createCell(32);
            //   cell33x=rw4x.createCell(33);

            cell0x.setCellValue(county);
            cell1x.setCellValue(partner);
            cell2x.setCellValue(district);
            cell3x.setCellValue(dic_name);
            // cell4x.setCellValue(message_no);
            // cell5x.setCellValue(achieved);
            if (message_no == 1) {
                cell13x.setCellValue(achieved);
            }

            if (message_no == 2) {
                cell14x.setCellValue(achieved);
            }

            if (message_no == 3) {
                cell15x.setCellValue(achieved);
            }

            if (message_no == 4) {
                cell16x.setCellValue(achieved);
            }

            if (message_no == 5) {
                cell17x.setCellValue(achieved);
            }

            if (message_no == 6) {
                cell18x.setCellValue(achieved);
            }

            if (message_no == 7) {
                cell19x.setCellValue(achieved);
            }

            if (message_no == 8) {
                cell20x.setCellValue(achieved);
            }

            if (message_no == 9) {
                cell21x.setCellValue(achieved);
            }

            if (message_no == 10) {
                cell22x.setCellValue(achieved);
            }

            if (message_no == 11) {
                cell23x.setCellValue(achieved);
            }

            if (message_no == 12) {
                cell24x.setCellValue(achieved);
            }

            if (message_no == 13) {
                cell25x.setCellValue(achieved);
            }

            // cell26x .setCellValue(cm);
            // cell27x.setCellValue(rsp);
            // cell28x.setCellValue(cds);
            // cell29x.setCellValue(tb);
            // cell30x.setCellValue(sti);
            // cell31x.setCellValue(testedpartner);
            // cell32x.setCellValue(testedchild);
            // cell33x.setCellValue(status);

            cell0x.setCellStyle(styleBorder);
            cell1x.setCellStyle(styleBorder);
            cell2x.setCellStyle(styleBorder);
            cell3x.setCellStyle(styleBorder);
            //cell4x.setCellStyle(styleBorder);
            //cell5x.setCellStyle(styleBorder);
            cell13x.setCellStyle(styleBorder);
            cell14x.setCellStyle(styleBorder);
            cell15x.setCellStyle(styleBorder);
            cell16x.setCellStyle(styleBorder);
            cell17x.setCellStyle(styleBorder);
            cell18x.setCellStyle(styleBorder);
            cell19x.setCellStyle(styleBorder);
            cell20x.setCellStyle(styleBorder);
            cell21x.setCellStyle(styleBorder);
            cell22x.setCellStyle(styleBorder);
            cell23x.setCellStyle(styleBorder);
            cell24x.setCellStyle(styleBorder);
            cell25x.setCellStyle(styleBorder);
            //cell26x.setCellStyle(styleBorder);
            //cell27x.setCellStyle(styleBorder);
            //cell28x.setCellStyle(styleBorder);
            //cell29x.setCellStyle(styleBorder);
            //cell30x.setCellStyle(styleBorder);
            //cell31x.setCellStyle(styleBorder);
            //cell32x.setCellStyle(styleBorder);
            //cell33x.setCellStyle(styleBorder); 
            i++;
        } else if (!current_group.equals(previous_group)) {
            //      update attended sessions;  
            previous_group = current_group;
            //  OUTPUT SERVICES PROVIDED================================     
            HSSFRow rw4x = shet1.createRow(i);
            rw4.setHeightInPoints(45);
            rw4.setRowStyle(style2);
            // rw4.createCell(1).setCellValue("Number");
            HSSFCell cell0x, cell1x, cell2x, cell3x, cell4x, cell5x, cell6x, cell7x, cell8x, cell9x, cell10x,
                    cell11x, cell12x, cell13x, cell14x, cell15x, cell16x, cell17x, cell18x, cell19x, cell20x;
            HSSFCell cell21x, cell22x, cell23x, cell24x, cell25x, cell26x, cell27x, cell28x, cell29x, cell30x,
                    cell31x, cell32x, cell33x;
            cell0x = rw4x.createCell(0);
            cell1x = rw4x.createCell(1);
            cell2x = rw4x.createCell(2);
            cell3x = rw4x.createCell(3);
            //   cell4x=rw4x.createCell(4);
            //   cell5x=rw4x.createCell(5);
            cell13x = rw4x.createCell(13);
            cell14x = rw4x.createCell(14);
            cell15x = rw4x.createCell(15);
            cell16x = rw4x.createCell(16);
            cell17x = rw4x.createCell(17);
            cell18x = rw4x.createCell(18);
            cell19x = rw4x.createCell(19);
            cell20x = rw4x.createCell(20);
            cell21x = rw4x.createCell(21);
            cell22x = rw4x.createCell(22);
            cell23x = rw4x.createCell(23);
            cell24x = rw4x.createCell(24);
            cell25x = rw4x.createCell(25);

            //   cell26x=rw4x.createCell(26);
            //   cell27x=rw4x.createCell(27);
            //   cell28x=rw4x.createCell(28);
            //   cell29x=rw4x.createCell(29);
            //   cell30x=rw4x.createCell(30);
            //   cell31x=rw4x.createCell(31);
            //   cell32x=rw4x.createCell(32);
            //   cell33x=rw4x.createCell(33);

            cell0x.setCellValue(county);
            cell1x.setCellValue(partner);
            cell2x.setCellValue(district);
            cell3x.setCellValue(dic_name);
            // cell4x.setCellValue(message_no);
            // cell5x.setCellValue(achieved);
            if (message_no == 1) {
                cell13x.setCellValue(achieved);
            }

            if (message_no == 2) {
                cell14x.setCellValue(achieved);
            }

            if (message_no == 3) {
                cell15x.setCellValue(achieved);
            }

            if (message_no == 4) {
                cell16x.setCellValue(achieved);
            }

            if (message_no == 5) {
                cell17x.setCellValue(achieved);
            }

            if (message_no == 6) {
                cell18x.setCellValue(achieved);
            }

            if (message_no == 7) {
                cell19x.setCellValue(achieved);
            }

            if (message_no == 8) {
                cell20x.setCellValue(achieved);
            }

            if (message_no == 9) {
                cell21x.setCellValue(achieved);
            }

            if (message_no == 10) {
                cell22x.setCellValue(achieved);
            }

            if (message_no == 11) {
                cell23x.setCellValue(achieved);
            }

            if (message_no == 12) {
                cell24x.setCellValue(achieved);
            }

            if (message_no == 13) {
                cell25x.setCellValue(achieved);
            }

            // cell26x .setCellValue(cm);
            // cell27x.setCellValue(rsp);
            // cell28x.setCellValue(cds);
            // cell29x.setCellValue(tb);
            // cell30x.setCellValue(sti);
            // cell31x.setCellValue(testedpartner);
            // cell32x.setCellValue(testedchild);
            // cell33x.setCellValue(status);

            cell0x.setCellStyle(styleBorder);
            cell1x.setCellStyle(styleBorder);
            cell2x.setCellStyle(styleBorder);
            cell3x.setCellStyle(styleBorder);
            //cell4x.setCellStyle(styleBorder);
            //cell5x.setCellStyle(styleBorder);

            cell13x.setCellStyle(styleBorder);
            cell14x.setCellStyle(styleBorder);
            cell15x.setCellStyle(styleBorder);
            cell16x.setCellStyle(styleBorder);
            cell17x.setCellStyle(styleBorder);
            cell18x.setCellStyle(styleBorder);
            cell19x.setCellStyle(styleBorder);
            cell20x.setCellStyle(styleBorder);
            cell21x.setCellStyle(styleBorder);
            cell22x.setCellStyle(styleBorder);
            cell23x.setCellStyle(styleBorder);
            cell24x.setCellStyle(styleBorder);
            cell25x.setCellStyle(styleBorder);

            //cell26x.setCellStyle(styleBorder);
            //cell27x.setCellStyle(styleBorder);
            //cell28x.setCellStyle(styleBorder);
            //cell29x.setCellStyle(styleBorder);
            //cell30x.setCellStyle(styleBorder);
            //cell31x.setCellStyle(styleBorder);
            //cell32x.setCellStyle(styleBorder);
            //cell33x.setCellStyle(styleBorder);     
            i++;
        }

        else if (current_group.equals(previous_group)) {
            HSSFRow rw4x = shet1.getRow(i - 1);
            if (message_no == 1) {
                HSSFCell cell13x = rw4x.createCell(13);
                cell13x.setCellValue(achieved);
                cell13x.setCellStyle(styleBorder);
            }
            if (message_no == 2) {
                HSSFCell cell14x = rw4x.createCell(14);
                cell14x.setCellValue(achieved);
                cell14x.setCellStyle(styleBorder);
            }
            if (message_no == 3) {
                HSSFCell cell15x = rw4x.createCell(15);
                cell15x.setCellValue(achieved);
                cell15x.setCellStyle(styleBorder);
            }
            if (message_no == 4) {
                HSSFCell cell16x = rw4x.createCell(16);
                cell16x.setCellValue(achieved);
                cell16x.setCellStyle(styleBorder);
            }
            if (message_no == 5) {
                HSSFCell cell17x = rw4x.createCell(17);
                cell17x.setCellValue(achieved);
                cell17x.setCellStyle(styleBorder);
            }
            if (message_no == 6) {
                HSSFCell cell18x = rw4x.createCell(18);
                cell18x.setCellValue(achieved);
                cell18x.setCellStyle(styleBorder);
            }
            if (message_no == 7) {
                HSSFCell cell19x = rw4x.createCell(19);
                cell19x.setCellValue(achieved);
                cell19x.setCellStyle(styleBorder);
            }
            if (message_no == 8) {
                HSSFCell cell20x = rw4x.createCell(20);
                cell20x.setCellValue(achieved);
                cell20x.setCellStyle(styleBorder);
            }
            if (message_no == 9) {
                HSSFCell cell21x = rw4x.createCell(21);
                cell21x.setCellValue(achieved);
                cell21x.setCellStyle(styleBorder);
            }
            if (message_no == 10) {
                HSSFCell cell22x = rw4x.createCell(22);
                cell22x.setCellValue(achieved);
                cell22x.setCellStyle(styleBorder);
            }
            if (message_no == 11) {
                HSSFCell cell23x = rw4x.createCell(23);
                cell23x.setCellValue(achieved);
                cell23x.setCellStyle(styleBorder);
            }
            if (message_no == 12) {
                HSSFCell cell24x = rw4x.createCell(24);
                cell24x.setCellValue(achieved);
                cell24x.setCellStyle(styleBorder);
            }
            if (message_no == 13) {
                HSSFCell cell25x = rw4x.createCell(25);
                cell25x.setCellValue(achieved);
                cell25x.setCellStyle(styleBorder);
            }

        } else {
            System.out.println("here-------------nothing seen");
        }
    }
    System.out.println("here : " + i);

    if (conn.rs != null) {
        conn.rs.close();
    }
    if (conn.st != null) {
        conn.st.close();
    }
    if (conn.rs1 != null) {
        conn.rs1.close();
    }
    if (conn.st1 != null) {
        conn.st1.close();
    }
    if (conn.rs2 != null) {
        conn.rs2.close();
    }
    if (conn.st2 != null) {
        conn.st2.close();
    }
    if (conn.st3 != null) {
        conn.st3.close();
    }
    if (conn.pst != null) {
        conn.pst.close();
    }
    if (conn.pst != null) {
        conn.pst.close();
    }
    if (conn.pst1 != null) {
        conn.pst1.close();
    }
    if (conn.pst1 != null) {
        conn.pst1.close();
    }
    if (conn.conn != null) {
        conn.conn.close();
    }

    // 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=PWP_Raw_Data_DIC.xls");
    OutputStream outStream = response.getOutputStream();
    outStream.write(outArray);
    outStream.flush();
}

From source file:reports.receivedMessageDistrict.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, SQLException {
    session = request.getSession();//  w  w w  . j a v a2  s  . c  o m
    dbConn conn = new dbConn();

    startDate = session.getAttribute("custstartDate").toString();
    endDate = session.getAttribute("custendDate").toString();

    total = 0;

    i = 4;

    previousPartner = currentPartner = "";

    //            ^^^^^^^^^^^^^CREATE STATIC AND WRITE STATIC DATA TO THE EXCELL^^^^^^^^^^^^
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet shet1 = wb.createSheet();
    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short) 18);
    font.setFontName("Arial Black");
    font.setColor((short) 0000);
    CellStyle style = wb.createCellStyle();
    style.setFont(font);
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    HSSFFont font2 = wb.createFont();
    font2.setFontName("Arial Black");
    font2.setColor((short) 0000);
    CellStyle style2 = wb.createCellStyle();
    style2.setFont(font2);

    HSSFCellStyle stborder = wb.createCellStyle();
    stborder.setBorderTop(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderRight(HSSFCellStyle.BORDER_THIN);
    stborder.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    shet1.setColumnWidth(0, 5000);
    shet1.setColumnWidth(1, 5000);
    shet1.setColumnWidth(2, 5000);
    shet1.setColumnWidth(3, 10);

    shet1.setColumnWidth(4, 10);

    shet1.setColumnWidth(5, 10);
    shet1.setColumnWidth(6, 10);
    shet1.setColumnWidth(7, 10);
    shet1.setColumnWidth(8, 10);
    shet1.setColumnWidth(9, 10);
    shet1.setColumnWidth(10, 10);
    shet1.setColumnWidth(11, 10);
    shet1.setColumnWidth(12, 10);

    shet1.setColumnWidth(13, 5000);

    shet1.setColumnWidth(14, 5300);
    shet1.setColumnWidth(15, 5000);
    shet1.setColumnWidth(16, 5200);
    shet1.setColumnWidth(17, 5200);
    shet1.setColumnWidth(18, 5200);
    shet1.setColumnWidth(19, 5800);
    shet1.setColumnWidth(20, 5000);
    shet1.setColumnWidth(21, 5300);

    shet1.setColumnWidth(22, 5300);
    shet1.setColumnWidth(23, 5000);
    shet1.setColumnWidth(24, 5200);
    shet1.setColumnWidth(25, 5200);

    HSSFCellStyle styleBorder = wb.createCellStyle();
    styleBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);
    styleBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    styleBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    styleBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);
    styleBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    HSSFRow rw1 = shet1.createRow(1);
    HSSFCell cell;
    HSSFRow rw4 = shet1.createRow(0);
    rw4.setHeightInPoints(45);
    rw4.setRowStyle(style2);
    // rw4.createCell(1).setCellValue("Number");
    HSSFCell cell0, cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9, cell10, cell11, cell12,
            cell13, cell14, cell15, cell16, cell17, cell18, cell19, cell20;
    HSSFCell cell21, cell22, cell23, cell24, cell25, cell26, cell27, cell28, cell29, cell30, cell31, cell32,
            cell33;

    cell0 = rw4.createCell(0);
    cell1 = rw4.createCell(1);
    cell2 = rw4.createCell(2);
    cell13 = rw4.createCell(13);
    cell14 = rw4.createCell(14);
    cell15 = rw4.createCell(15);
    cell16 = rw4.createCell(16);
    cell17 = rw4.createCell(17);
    cell18 = rw4.createCell(18);
    cell19 = rw4.createCell(19);
    cell20 = rw4.createCell(20);
    cell21 = rw4.createCell(21);
    cell22 = rw4.createCell(22);
    cell23 = rw4.createCell(23);
    cell24 = rw4.createCell(24);
    cell25 = rw4.createCell(25);

    cell0.setCellValue("COUNTY NAME");
    cell1.setCellValue("PARTNER NAME");
    cell2.setCellValue("DISTRICT NAME");

    cell13.setCellValue("Knowledge of HIV Status");
    cell14.setCellValue("Partner HIV Testing");
    cell15.setCellValue("Child HIV Testing");
    cell16.setCellValue("Discordance");
    cell17.setCellValue("HIV Disclosure");
    cell18.setCellValue("Risk Factor/Reduction");
    cell19.setCellValue("Condom Use");
    cell20.setCellValue("Alcohol and Substance Abuse");

    cell21.setCellValue("Adherence");
    cell22.setCellValue("STIs");
    cell23.setCellValue("Family Planning");
    cell24.setCellValue("PMTCT");
    cell25.setCellValue("TB");

    HSSFCellStyle stylex = wb.createCellStyle();
    stylex.setFillForegroundColor(HSSFColor.LIME.index);
    stylex.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    stylex.setBorderTop(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderRight(HSSFCellStyle.BORDER_THIN);
    stylex.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    HSSFFont fontx = wb.createFont();
    fontx.setColor(HSSFColor.DARK_BLUE.index);
    stylex.setFont(fontx);
    stylex.setWrapText(true);

    cell0.setCellStyle(stylex);
    cell1.setCellStyle(stylex);
    cell2.setCellStyle(stylex);

    cell13.setCellStyle(stylex);
    cell14.setCellStyle(stylex);
    cell15.setCellStyle(stylex);
    cell16.setCellStyle(stylex);
    cell17.setCellStyle(stylex);
    cell18.setCellStyle(stylex);
    cell19.setCellStyle(stylex);
    cell20.setCellStyle(stylex);
    cell21.setCellStyle(stylex);
    cell22.setCellStyle(stylex);
    cell23.setCellStyle(stylex);
    cell24.setCellStyle(stylex);
    cell25.setCellStyle(stylex);

    i = 1;
    previous_group = "";
    previousPartner = "";
    current_group = "";
    String getClients = "SELECT district.district_name,partner.partner_name,county.county_name,"
            + "register2.session_no,SUM(register2.value) " + " FROM personal_information "
            + "LEFT JOIN district ON personal_information.district_id=district.district_id "
            + "LEFT JOIN county ON district.county_id=county.county_id "
            + "LEFT JOIN partner ON personal_information.partner_id=partner.partner_id "
            + "LEFT JOIN register2 ON personal_information.client_id=register2.client_id "
            + " WHERE register2.value<2 && STR_TO_DATE(register2.date,'%m/%d/%Y') BETWEEN STR_TO_DATE('"
            + startDate + "','%m/%d/%Y') AND STR_TO_DATE('" + endDate + "','%m/%d/%Y')  "
            + " GROUP BY county.county_name,district.district_name,register2.session_no ORDER BY county.county_name,partner.partner_name,district.district_name";
    conn.rs = conn.st.executeQuery(getClients);
    while (conn.rs.next()) {
        county = district = hf = partner = groupname = serviceprovider = clientname = age = gender = groupings = year = providerid = "";
        countyid = districtid = hfid = partnerid = groupid = serviceproviderid = clientid = "";
        s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = s9 = s10 = s11 = s12 = s13 = "";
        cm = rsp = cd = tb = sti = testedpartner = testedchild = session_no = value = status;
        sess = val = cds = 0;
        hf_id = lessons_attended = national_id = ccc_no = mobile_no = dob = "";
        added = 0;
        message_no = achieved = 0;

        //          age=conn.rs.getString(1);
        //          gender=conn.rs.getString(2);
        district = conn.rs.getString(1);
        partner = conn.rs.getString(2);
        System.out.println("ppartner is : " + partner);
        if (partner == null) {
            partner = "NO PARTNER";
        }
        county = conn.rs.getString(3);
        message_no = conn.rs.getInt(4);
        achieved = conn.rs.getInt(5);
        if (message_no == 5) {
            total += achieved;
        }
        //      OUTPUT ATTENDED-------------------------------- 
        current_group = district;
        currentPartner = partner;
        System.out.println("current partner : " + currentPartner + " previous partner : " + previousPartner);
        cm = rsp = tb = sti = testedpartner = testedchild = session_no = value = status = "NO";
        System.out.println("current partner : " + currentPartner + "    previous partner : " + previousPartner
                + " achieved : " + achieved);
        System.out.println("current district : " + current_group + "    previous distrcit : " + previous_group
                + "mesage no : " + message_no);
        System.out.println(
                "------------------------------------------------------------------------------------------------------");
        if (previous_group.equals("")) {
            previous_group = current_group;
            previousPartner = currentPartner;
            //  OUTPUT SERVICES PROVIDED================================     
            HSSFRow rw4x = shet1.createRow(i);
            rw4.setHeightInPoints(45);
            rw4.setRowStyle(style2);
            // rw4.createCell(1).setCellValue("Number");
            HSSFCell cell0x, cell1x, cell2x, cell3x, cell4x, cell5x, cell6x, cell7x, cell8x, cell9x, cell10x,
                    cell11x, cell12x, cell13x, cell14x, cell15x, cell16x, cell17x, cell18x, cell19x, cell20x;
            HSSFCell cell21x, cell22x, cell23x, cell24x, cell25x, cell26x, cell27x, cell28x, cell29x, cell30x,
                    cell31x, cell32x, cell33x;
            cell0x = rw4x.createCell(0);
            cell1x = rw4x.createCell(1);
            cell2x = rw4x.createCell(2);

            cell13x = rw4x.createCell(13);
            cell14x = rw4x.createCell(14);
            cell15x = rw4x.createCell(15);
            cell16x = rw4x.createCell(16);
            cell17x = rw4x.createCell(17);
            cell18x = rw4x.createCell(18);
            cell19x = rw4x.createCell(19);
            cell20x = rw4x.createCell(20);
            cell21x = rw4x.createCell(21);
            cell22x = rw4x.createCell(22);
            cell23x = rw4x.createCell(23);
            cell24x = rw4x.createCell(24);
            cell25x = rw4x.createCell(25);

            cell0x.setCellValue(county);
            cell1x.setCellValue(partner);
            cell2x.setCellValue(district);

            if (message_no == 1) {
                cell13x.setCellValue(achieved);
            }

            if (message_no == 2) {
                cell14x.setCellValue(achieved);
            }

            if (message_no == 3) {
                cell15x.setCellValue(achieved);
            }

            if (message_no == 4) {
                cell16x.setCellValue(achieved);
            }

            if (message_no == 5) {
                cell17x.setCellValue(achieved);
            }

            if (message_no == 6) {
                cell18x.setCellValue(achieved);
            }

            if (message_no == 7) {
                cell19x.setCellValue(achieved);
            }

            if (message_no == 8) {
                cell20x.setCellValue(achieved);
            }

            if (message_no == 9) {
                cell21x.setCellValue(achieved);
            }

            if (message_no == 10) {
                cell22x.setCellValue(achieved);
            }

            if (message_no == 11) {
                cell23x.setCellValue(achieved);
            }

            if (message_no == 12) {
                cell24x.setCellValue(achieved);
            }

            if (message_no == 13) {
                cell25x.setCellValue(achieved);
            }

            cell0x.setCellStyle(styleBorder);
            cell1x.setCellStyle(styleBorder);
            cell2x.setCellStyle(styleBorder);

            cell13x.setCellStyle(styleBorder);
            cell14x.setCellStyle(styleBorder);
            cell15x.setCellStyle(styleBorder);
            cell16x.setCellStyle(styleBorder);
            cell17x.setCellStyle(styleBorder);
            cell18x.setCellStyle(styleBorder);
            cell19x.setCellStyle(styleBorder);
            cell20x.setCellStyle(styleBorder);
            cell21x.setCellStyle(styleBorder);
            cell22x.setCellStyle(styleBorder);
            cell23x.setCellStyle(styleBorder);
            cell24x.setCellStyle(styleBorder);
            cell25x.setCellStyle(styleBorder);

            i++;
        } else if (!currentPartner.equals(previousPartner)) {
            previous_group = current_group;
            previousPartner = currentPartner;
            //  OUTPUT SERVICES PROVIDED================================     
            HSSFRow rw4x = shet1.createRow(i);
            rw4.setHeightInPoints(45);
            rw4.setRowStyle(style2);
            // rw4.createCell(1).setCellValue("Number");
            HSSFCell cell0x, cell1x, cell2x, cell3x, cell4x, cell5x, cell6x, cell7x, cell8x, cell9x, cell10x,
                    cell11x, cell12x, cell13x, cell14x, cell15x, cell16x, cell17x, cell18x, cell19x, cell20x;
            HSSFCell cell21x, cell22x, cell23x, cell24x, cell25x, cell26x, cell27x, cell28x, cell29x, cell30x,
                    cell31x, cell32x, cell33x;
            cell0x = rw4x.createCell(0);
            cell1x = rw4x.createCell(1);
            cell2x = rw4x.createCell(2);

            cell13x = rw4x.createCell(13);
            cell14x = rw4x.createCell(14);
            cell15x = rw4x.createCell(15);
            cell16x = rw4x.createCell(16);
            cell17x = rw4x.createCell(17);
            cell18x = rw4x.createCell(18);
            cell19x = rw4x.createCell(19);
            cell20x = rw4x.createCell(20);
            cell21x = rw4x.createCell(21);
            cell22x = rw4x.createCell(22);
            cell23x = rw4x.createCell(23);
            cell24x = rw4x.createCell(24);
            cell25x = rw4x.createCell(25);

            cell0x.setCellValue(county);
            cell1x.setCellValue(partner);
            cell2x.setCellValue(district);

            if (message_no == 1) {
                cell13x.setCellValue(achieved);
            }

            if (message_no == 2) {
                cell14x.setCellValue(achieved);
            }

            if (message_no == 3) {
                cell15x.setCellValue(achieved);
            }

            if (message_no == 4) {
                cell16x.setCellValue(achieved);
            }

            if (message_no == 5) {
                cell17x.setCellValue(achieved);
            }

            if (message_no == 6) {
                cell18x.setCellValue(achieved);
            }

            if (message_no == 7) {
                cell19x.setCellValue(achieved);
            }

            if (message_no == 8) {
                cell20x.setCellValue(achieved);
            }

            if (message_no == 9) {
                cell21x.setCellValue(achieved);
            }

            if (message_no == 10) {
                cell22x.setCellValue(achieved);
            }

            if (message_no == 11) {
                cell23x.setCellValue(achieved);
            }

            if (message_no == 12) {
                cell24x.setCellValue(achieved);
            }

            if (message_no == 13) {
                cell25x.setCellValue(achieved);
            }

            cell0x.setCellStyle(styleBorder);
            cell1x.setCellStyle(styleBorder);
            cell2x.setCellStyle(styleBorder);

            cell13x.setCellStyle(styleBorder);
            cell14x.setCellStyle(styleBorder);
            cell15x.setCellStyle(styleBorder);
            cell16x.setCellStyle(styleBorder);
            cell17x.setCellStyle(styleBorder);
            cell18x.setCellStyle(styleBorder);
            cell19x.setCellStyle(styleBorder);
            cell20x.setCellStyle(styleBorder);
            cell21x.setCellStyle(styleBorder);
            cell22x.setCellStyle(styleBorder);
            cell23x.setCellStyle(styleBorder);
            cell24x.setCellStyle(styleBorder);
            cell25x.setCellStyle(styleBorder);

            i++;
        } else if (!current_group.equals(previous_group)) {
            previous_group = current_group;
            previousPartner = currentPartner;
            //      update attended sessions;  
            //             previous_group=current_group;
            //  OUTPUT SERVICES PROVIDED================================     
            HSSFRow rw4x = shet1.createRow(i);
            rw4.setHeightInPoints(45);
            rw4.setRowStyle(style2);
            // rw4.createCell(1).setCellValue("Number");
            HSSFCell cell0x, cell1x, cell2x, cell3x, cell4x, cell5x, cell6x, cell7x, cell8x, cell9x, cell10x,
                    cell11x, cell12x, cell13x, cell14x, cell15x, cell16x, cell17x, cell18x, cell19x, cell20x;
            HSSFCell cell21x, cell22x, cell23x, cell24x, cell25x, cell26x, cell27x, cell28x, cell29x, cell30x,
                    cell31x, cell32x, cell33x;
            cell0x = rw4x.createCell(0);
            cell1x = rw4x.createCell(1);
            cell2x = rw4x.createCell(2);

            cell13x = rw4x.createCell(13);
            cell14x = rw4x.createCell(14);
            cell15x = rw4x.createCell(15);
            cell16x = rw4x.createCell(16);
            cell17x = rw4x.createCell(17);
            cell18x = rw4x.createCell(18);
            cell19x = rw4x.createCell(19);
            cell20x = rw4x.createCell(20);
            cell21x = rw4x.createCell(21);
            cell22x = rw4x.createCell(22);
            cell23x = rw4x.createCell(23);
            cell24x = rw4x.createCell(24);
            cell25x = rw4x.createCell(25);

            cell0x.setCellValue(county);
            cell1x.setCellValue(partner);
            cell2x.setCellValue(district);

            if (message_no == 1) {
                cell13x.setCellValue(achieved);
            }

            if (message_no == 2) {
                cell14x.setCellValue(achieved);
            }

            if (message_no == 3) {
                cell15x.setCellValue(achieved);
            }

            if (message_no == 4) {
                cell16x.setCellValue(achieved);
            }

            if (message_no == 5) {
                cell17x.setCellValue(achieved);
            }

            if (message_no == 6) {
                cell18x.setCellValue(achieved);
            }

            if (message_no == 7) {
                cell19x.setCellValue(achieved);
            }

            if (message_no == 8) {
                cell20x.setCellValue(achieved);
            }

            if (message_no == 9) {
                cell21x.setCellValue(achieved);
            }

            if (message_no == 10) {
                cell22x.setCellValue(achieved);
            }

            if (message_no == 11) {
                cell23x.setCellValue(achieved);
            }

            if (message_no == 12) {
                cell24x.setCellValue(achieved);
            }

            if (message_no == 13) {
                cell25x.setCellValue(achieved);
            }

            cell0x.setCellStyle(styleBorder);
            cell1x.setCellStyle(styleBorder);
            cell2x.setCellStyle(styleBorder);

            cell13x.setCellStyle(styleBorder);
            cell14x.setCellStyle(styleBorder);
            cell15x.setCellStyle(styleBorder);
            cell16x.setCellStyle(styleBorder);
            cell17x.setCellStyle(styleBorder);
            cell18x.setCellStyle(styleBorder);
            cell19x.setCellStyle(styleBorder);
            cell20x.setCellStyle(styleBorder);
            cell21x.setCellStyle(styleBorder);
            cell22x.setCellStyle(styleBorder);
            cell23x.setCellStyle(styleBorder);
            cell24x.setCellStyle(styleBorder);
            cell25x.setCellStyle(styleBorder);

            i++;
        }

        else if (current_group.equals(previous_group) && currentPartner.equals(previousPartner)) {
            HSSFRow rw4x = shet1.getRow(i - 1);
            if (message_no == 1) {
                HSSFCell cell13x = rw4x.createCell(13);
                cell13x.setCellValue(achieved);
                cell13x.setCellStyle(styleBorder);
            }
            if (message_no == 2) {
                HSSFCell cell14x = rw4x.createCell(14);
                cell14x.setCellValue(achieved);
                cell14x.setCellStyle(styleBorder);
            }
            if (message_no == 3) {
                HSSFCell cell15x = rw4x.createCell(15);
                cell15x.setCellValue(achieved);
                cell15x.setCellStyle(styleBorder);
            }
            if (message_no == 4) {
                HSSFCell cell16x = rw4x.createCell(16);
                cell16x.setCellValue(achieved);
                cell16x.setCellStyle(styleBorder);
            }
            if (message_no == 5) {
                HSSFCell cell17x = rw4x.createCell(17);
                cell17x.setCellValue(achieved);
                cell17x.setCellStyle(styleBorder);
            }
            if (message_no == 6) {
                HSSFCell cell18x = rw4x.createCell(18);
                cell18x.setCellValue(achieved);
                cell18x.setCellStyle(styleBorder);
            }
            if (message_no == 7) {
                HSSFCell cell19x = rw4x.createCell(19);
                cell19x.setCellValue(achieved);
                cell19x.setCellStyle(styleBorder);
            }
            if (message_no == 8) {
                HSSFCell cell20x = rw4x.createCell(20);
                cell20x.setCellValue(achieved);
                cell20x.setCellStyle(styleBorder);
            }
            if (message_no == 9) {
                HSSFCell cell21x = rw4x.createCell(21);
                cell21x.setCellValue(achieved);
                cell21x.setCellStyle(styleBorder);
            }
            if (message_no == 10) {
                HSSFCell cell22x = rw4x.createCell(22);
                cell22x.setCellValue(achieved);
                cell22x.setCellStyle(styleBorder);
            }
            if (message_no == 11) {
                HSSFCell cell23x = rw4x.createCell(23);
                cell23x.setCellValue(achieved);
                cell23x.setCellStyle(styleBorder);
            }
            if (message_no == 12) {
                HSSFCell cell24x = rw4x.createCell(24);
                cell24x.setCellValue(achieved);
                cell24x.setCellStyle(styleBorder);
            }
            if (message_no == 13) {
                HSSFCell cell25x = rw4x.createCell(25);
                cell25x.setCellValue(achieved);
                cell25x.setCellStyle(styleBorder);
            }

        } else {
            System.out.println("here-------------nothing seen");
        }
    }
    //     System.out.println("total : "+total);

    if (conn.rs != null) {
        conn.rs.close();
    }
    if (conn.st != null) {
        conn.st.close();
    }
    if (conn.rs1 != null) {
        conn.rs1.close();
    }
    if (conn.st1 != null) {
        conn.st1.close();
    }
    if (conn.rs2 != null) {
        conn.rs2.close();
    }
    if (conn.st2 != null) {
        conn.st2.close();
    }
    if (conn.st3 != null) {
        conn.st3.close();
    }
    if (conn.pst != null) {
        conn.pst.close();
    }
    if (conn.pst != null) {
        conn.pst.close();
    }
    if (conn.pst1 != null) {
        conn.pst1.close();
    }
    if (conn.pst1 != null) {
        conn.pst1.close();
    }
    if (conn.conn != null) {
        conn.conn.close();
    }

    // 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=PWP_Raw_Data_DISTRICT.xls");
    OutputStream outStream = response.getOutputStream();
    outStream.write(outArray);
    outStream.flush();
}

From source file:reports.ReceivedMessages.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, SQLException {
    session = request.getSession();//from ww  w  .  java2s.c  om
    dbConn conn = new dbConn();
    startDate = session.getAttribute("custstartDate").toString();
    endDate = session.getAttribute("custendDate").toString();

    i = 4;

    //            ^^^^^^^^^^^^^CREATE STATIC AND WRITE STATIC DATA TO THE EXCELL^^^^^^^^^^^^
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet shet1 = wb.createSheet();
    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short) 18);
    font.setFontName("Arial Black");
    font.setColor((short) 0000);
    CellStyle style = wb.createCellStyle();
    style.setFont(font);
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    HSSFFont font2 = wb.createFont();
    font2.setFontName("Arial Black");
    font2.setColor((short) 0000);
    CellStyle style2 = wb.createCellStyle();
    style2.setFont(font2);

    HSSFCellStyle stborder = wb.createCellStyle();
    stborder.setBorderTop(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderRight(HSSFCellStyle.BORDER_THIN);
    stborder.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    shet1.setColumnWidth(0, 5000);
    shet1.setColumnWidth(1, 5000);
    shet1.setColumnWidth(2, 5000);
    shet1.setColumnWidth(3, 5500);

    shet1.setColumnWidth(4, 5500);

    shet1.setColumnWidth(5, 10);
    shet1.setColumnWidth(6, 10);
    shet1.setColumnWidth(7, 10);
    shet1.setColumnWidth(8, 10);
    shet1.setColumnWidth(9, 10);
    shet1.setColumnWidth(10, 10);
    shet1.setColumnWidth(11, 10);
    shet1.setColumnWidth(12, 10);

    shet1.setColumnWidth(13, 5000);

    shet1.setColumnWidth(14, 5300);
    shet1.setColumnWidth(15, 5000);
    shet1.setColumnWidth(16, 5200);
    shet1.setColumnWidth(17, 5200);
    shet1.setColumnWidth(18, 5200);
    shet1.setColumnWidth(19, 5800);
    shet1.setColumnWidth(20, 5000);
    shet1.setColumnWidth(21, 5300);

    shet1.setColumnWidth(22, 5300);
    shet1.setColumnWidth(23, 5000);
    shet1.setColumnWidth(24, 5200);
    shet1.setColumnWidth(25, 5200);

    //    shet1.setColumnWidth(26, 5200); 
    //    shet1.setColumnWidth(27, 5800); 
    //    shet1.setColumnWidth(28, 5000); 
    //    shet1.setColumnWidth(29, 5300);
    //    shet1.setColumnWidth(30, 5800); 
    //    shet1.setColumnWidth(31, 5000); 
    //    shet1.setColumnWidth(32, 5300);

    //    shet1.setColumnWidth(20, 2000);
    HSSFCellStyle styleBorder = wb.createCellStyle();
    styleBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);
    styleBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    styleBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    styleBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);
    styleBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    HSSFRow rw1 = shet1.createRow(1);
    HSSFCell cell;
    HSSFRow rw4 = shet1.createRow(0);
    rw4.setHeightInPoints(45);
    rw4.setRowStyle(style2);
    // rw4.createCell(1).setCellValue("Number");
    HSSFCell cell0, cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9, cell10, cell11, cell12,
            cell13, cell14, cell15, cell16, cell17, cell18, cell19, cell20;
    HSSFCell cell21, cell22, cell23, cell24, cell25, cell26, cell27, cell28, cell29, cell30, cell31, cell32,
            cell33;

    cell0 = rw4.createCell(0);
    cell1 = rw4.createCell(1);
    cell2 = rw4.createCell(2);
    cell3 = rw4.createCell(3);
    cell4 = rw4.createCell(4);
    //   cell5=rw4.createCell(5);
    cell13 = rw4.createCell(13);
    cell14 = rw4.createCell(14);
    cell15 = rw4.createCell(15);
    cell16 = rw4.createCell(16);
    cell17 = rw4.createCell(17);
    cell18 = rw4.createCell(18);
    cell19 = rw4.createCell(19);
    cell20 = rw4.createCell(20);
    cell21 = rw4.createCell(21);
    cell22 = rw4.createCell(22);
    cell23 = rw4.createCell(23);
    cell24 = rw4.createCell(24);
    cell25 = rw4.createCell(25);
    //   
    //   cell26=rw4.createCell(26);
    //   cell27=rw4.createCell(27);
    //   cell28=rw4.createCell(28);
    //   cell29=rw4.createCell(29);
    //   cell30=rw4.createCell(30);
    //   cell31=rw4.createCell(31);
    //   cell32=rw4.createCell(32);
    //   cell33=rw4.createCell(33);

    cell0.setCellValue("COUNTY NAME");
    cell1.setCellValue("PARTNER NAME");
    cell2.setCellValue("DISTRICT NAME");
    cell3.setCellValue("NEAREST FACILITY");
    cell4.setCellValue("GROUP NAME");
    // cell5.setCellValue("ACHIEVED");

    cell13.setCellValue("Knowledge of HIV Status");
    cell14.setCellValue("Partner HIV Testing");
    cell15.setCellValue("Child HIV Testing");
    cell16.setCellValue("Discordance");
    cell17.setCellValue("HIV Disclosure");
    cell18.setCellValue("Risk Factor/Reduction");
    cell19.setCellValue("Condom Use");
    cell20.setCellValue("Alcohol and Substance Abuse");

    cell21.setCellValue("Adherence");
    cell22.setCellValue("STIs");
    cell23.setCellValue("Family Planning");
    cell24.setCellValue("PMTCT");
    cell25.setCellValue("TB");

    // cell26.setCellValue("Received Contraceptives");
    // cell27.setCellValue("Reffered To Service Point");
    // cell28.setCellValue("Given Condoms");
    // cell29 .setCellValue("Screened For TB");
    // cell30.setCellValue("Screened For STIs");
    // cell31.setCellValue("Partner Tested");
    // cell32.setCellValue("Children Tested");
    // cell33.setCellValue("Disclosed Status");

    HSSFCellStyle stylex = wb.createCellStyle();
    stylex.setFillForegroundColor(HSSFColor.LIME.index);
    stylex.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    stylex.setBorderTop(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderRight(HSSFCellStyle.BORDER_THIN);
    stylex.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    HSSFFont fontx = wb.createFont();
    fontx.setColor(HSSFColor.DARK_BLUE.index);
    stylex.setFont(fontx);
    stylex.setWrapText(true);

    cell0.setCellStyle(stylex);
    cell1.setCellStyle(stylex);
    cell2.setCellStyle(stylex);
    cell3.setCellStyle(stylex);
    cell4.setCellStyle(stylex);
    //cell5.setCellStyle(stylex);
    //cell6.setCellStyle(stylex);
    //cell7.setCellStyle(stylex);
    //cell8.setCellStyle(stylex);
    //cell9.setCellStyle(stylex);
    //cell10.setCellStyle(stylex);
    //cell11.setCellStyle(stylex);
    //cell12.setCellStyle(stylex);
    cell13.setCellStyle(stylex);
    cell14.setCellStyle(stylex);
    cell15.setCellStyle(stylex);
    cell16.setCellStyle(stylex);
    cell17.setCellStyle(stylex);
    cell18.setCellStyle(stylex);
    cell19.setCellStyle(stylex);
    cell20.setCellStyle(stylex);
    cell21.setCellStyle(stylex);
    cell22.setCellStyle(stylex);
    cell23.setCellStyle(stylex);
    cell24.setCellStyle(stylex);
    cell25.setCellStyle(stylex);

    i = 1;
    previousDistrict = currentDistrict = "";
    current_group = "";
    String getClients = "SELECT DISTINCT(personal_information.client_id),personal_information.fname,personal_information.mname,personal_information.lname,"
            + "DATE_FORMAT( NOW( ) , '%Y' ) - DATE_FORMAT( personal_information.dob, '%Y' )-( DATE_FORMAT( NOW( ),'YYYY-%mm-%dd' )< DATE_FORMAT( personal_information.dob, 'YYYY-%mm-%dd' ) )"
            + ",personal_information.gender,groups.group_name,district.district_name,partner.partner_name,"
            + "service_provider.fname,service_provider.mname,service_provider.lname,personal_information.lessons_attended,"
            + "personal_information.national_id,personal_information.ccc_no,personal_information.mobile_no,health_facility.hf_name,county.county_name,"
            + "register2.session_no,SUM(register2.value) " + " FROM personal_information"
            + " LEFT JOIN health_facility ON personal_information.hf_id=health_facility.hf_id "
            + "LEFT JOIN district ON personal_information.district_id=district.district_id "
            + "LEFT JOIN county ON district.county_id=county.county_id "
            + "LEFT JOIN service_provider ON personal_information.provider_id=service_provider.provider_id "
            + "LEFT JOIN partner ON personal_information.partner_id=partner.partner_id "
            + "LEFT JOIN groups ON personal_information.group_id =groups.group_id "
            + "LEFT JOIN register2 ON personal_information.client_id=register2.client_id "
            + " WHERE register2.value<2 && STR_TO_DATE(register2.date,'%m/%d/%Y') BETWEEN STR_TO_DATE('"
            + startDate + "','%m/%d/%Y') AND STR_TO_DATE('" + endDate + "','%m/%d/%Y')  "
            + " GROUP BY county.county_name,partner.partner_name,district.district_name,groups.group_name, register2.session_no ORDER BY partner.partner_name,district.district_name,groups.group_name";
    conn.rs = conn.st.executeQuery(getClients);
    while (conn.rs.next()) {
        county = district = hf = partner = groupname = serviceprovider = clientname = age = gender = groupings = year = providerid = "";
        countyid = districtid = hfid = partnerid = groupid = serviceproviderid = clientid = "";
        s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = s9 = s10 = s11 = s12 = s13 = "";
        cm = rsp = cd = tb = sti = testedpartner = testedchild = session_no = value = status;
        sess = val = cds = 0;
        hf_id = lessons_attended = national_id = ccc_no = mobile_no = dob = "";
        added = 0;
        message_no = achieved = 0;

        clientid = conn.rs.getString(1);
        if (!conn.rs.getString(3).equalsIgnoreCase(conn.rs.getString(4))) {
            clientname = conn.rs.getString(2) + " " + conn.rs.getString(3) + " " + conn.rs.getString(4);
        } else {
            clientname = conn.rs.getString(2) + " " + conn.rs.getString(4);
        }
        age = conn.rs.getString(5);
        gender = conn.rs.getString(6);
        groupname = conn.rs.getString(7);
        if (groupname == null) {
            groupname = "INDIVIDUAL";
        }
        district = conn.rs.getString(8);
        partner = conn.rs.getString(9);
        serviceprovider = conn.rs.getString(10) + " " + conn.rs.getString(11) + " " + conn.rs.getString(12);
        if (conn.rs.getString(11) != null && conn.rs.getString(12) != null) {
            if (conn.rs.getString(11).equals(conn.rs.getString(12))) {
                serviceprovider = conn.rs.getString(10) + " " + conn.rs.getString(12);
            }
        }
        lessons_attended = conn.rs.getString(13);
        national_id = conn.rs.getString(14);
        ccc_no = conn.rs.getString(15);
        mobile_no = conn.rs.getString(16);
        hf = conn.rs.getString(17);
        county = conn.rs.getString(18);
        message_no = conn.rs.getInt(19);
        achieved = conn.rs.getInt(20);
        //      OUTPUT ATTENDED-------------------------------- 
        current_group = groupname;

        currentDistrict = district;

        if (current_group.equals("")) {
            previous_group = current_group;
            previousDistrict = currentDistrict;
            //  OUTPUT SERVICES PROVIDED================================     
            HSSFRow rw4x = shet1.createRow(i);
            rw4.setHeightInPoints(45);
            rw4.setRowStyle(style2);
            // rw4.createCell(1).setCellValue("Number");
            HSSFCell cell0x, cell1x, cell2x, cell3x, cell4x, cell5x, cell6x, cell7x, cell8x, cell9x, cell10x,
                    cell11x, cell12x, cell13x, cell14x, cell15x, cell16x, cell17x, cell18x, cell19x, cell20x;
            HSSFCell cell21x, cell22x, cell23x, cell24x, cell25x, cell26x, cell27x, cell28x, cell29x, cell30x,
                    cell31x, cell32x, cell33x;
            cell0x = rw4x.createCell(0);
            cell1x = rw4x.createCell(1);
            cell2x = rw4x.createCell(2);
            cell3x = rw4x.createCell(3);
            cell4x = rw4x.createCell(4);
            //   cell5x=rw4x.createCell(5);
            cell13x = rw4x.createCell(13);
            cell14x = rw4x.createCell(14);
            cell15x = rw4x.createCell(15);
            cell16x = rw4x.createCell(16);
            cell17x = rw4x.createCell(17);
            cell18x = rw4x.createCell(18);
            cell19x = rw4x.createCell(19);
            cell20x = rw4x.createCell(20);
            cell21x = rw4x.createCell(21);
            cell22x = rw4x.createCell(22);
            cell23x = rw4x.createCell(23);
            cell24x = rw4x.createCell(24);
            cell25x = rw4x.createCell(25);

            cell0x.setCellValue(county);
            cell1x.setCellValue(partner);
            cell2x.setCellValue(district);
            cell3x.setCellValue(hf);
            cell4x.setCellValue(groupname);
            // cell5x.setCellValue(achieved);
            if (message_no == 1) {
                cell13x.setCellValue(achieved);
            }

            if (message_no == 2) {
                cell14x.setCellValue(achieved);
            }

            if (message_no == 3) {
                cell15x.setCellValue(achieved);
            }

            if (message_no == 4) {
                cell16x.setCellValue(achieved);
            }

            if (message_no == 5) {
                cell17x.setCellValue(achieved);
            }

            if (message_no == 6) {
                cell18x.setCellValue(achieved);
            }

            if (message_no == 7) {
                cell19x.setCellValue(achieved);
            }

            if (message_no == 8) {
                cell20x.setCellValue(achieved);
            }

            if (message_no == 9) {
                cell21x.setCellValue(achieved);
            }

            if (message_no == 10) {
                cell22x.setCellValue(achieved);
            }

            if (message_no == 11) {
                cell23x.setCellValue(achieved);
            }

            if (message_no == 12) {
                cell24x.setCellValue(achieved);
            }

            if (message_no == 13) {
                cell25x.setCellValue(achieved);
            }

            cell0x.setCellStyle(styleBorder);
            cell1x.setCellStyle(styleBorder);
            cell2x.setCellStyle(styleBorder);
            cell3x.setCellStyle(styleBorder);
            cell4x.setCellStyle(styleBorder);
            //cell5x.setCellStyle(styleBorder);
            cell13x.setCellStyle(styleBorder);
            cell14x.setCellStyle(styleBorder);
            cell15x.setCellStyle(styleBorder);
            cell16x.setCellStyle(styleBorder);
            cell17x.setCellStyle(styleBorder);
            cell18x.setCellStyle(styleBorder);
            cell19x.setCellStyle(styleBorder);
            cell20x.setCellStyle(styleBorder);
            cell21x.setCellStyle(styleBorder);
            cell22x.setCellStyle(styleBorder);
            cell23x.setCellStyle(styleBorder);
            cell24x.setCellStyle(styleBorder);
            cell25x.setCellStyle(styleBorder);

            i++;
        } else if (!currentDistrict.equals(previousDistrict)) {
            previousDistrict = currentDistrict;
            previous_group = current_group;
            //  OUTPUT SERVICES PROVIDED================================     
            HSSFRow rw4x = shet1.createRow(i);
            rw4.setHeightInPoints(45);
            rw4.setRowStyle(style2);
            // rw4.createCell(1).setCellValue("Number");
            HSSFCell cell0x, cell1x, cell2x, cell3x, cell4x, cell5x, cell6x, cell7x, cell8x, cell9x, cell10x,
                    cell11x, cell12x, cell13x, cell14x, cell15x, cell16x, cell17x, cell18x, cell19x, cell20x;
            HSSFCell cell21x, cell22x, cell23x, cell24x, cell25x, cell26x, cell27x, cell28x, cell29x, cell30x,
                    cell31x, cell32x, cell33x;
            cell0x = rw4x.createCell(0);
            cell1x = rw4x.createCell(1);
            cell2x = rw4x.createCell(2);
            cell3x = rw4x.createCell(3);
            cell4x = rw4x.createCell(4);
            //   cell5x=rw4x.createCell(5);
            cell13x = rw4x.createCell(13);
            cell14x = rw4x.createCell(14);
            cell15x = rw4x.createCell(15);
            cell16x = rw4x.createCell(16);
            cell17x = rw4x.createCell(17);
            cell18x = rw4x.createCell(18);
            cell19x = rw4x.createCell(19);
            cell20x = rw4x.createCell(20);
            cell21x = rw4x.createCell(21);
            cell22x = rw4x.createCell(22);
            cell23x = rw4x.createCell(23);
            cell24x = rw4x.createCell(24);
            cell25x = rw4x.createCell(25);

            cell0x.setCellValue(county);
            cell1x.setCellValue(partner);
            cell2x.setCellValue(district);
            cell3x.setCellValue(hf);
            cell4x.setCellValue(groupname);
            // cell5x.setCellValue(achieved);
            if (message_no == 1) {
                cell13x.setCellValue(achieved);
            }

            if (message_no == 2) {
                cell14x.setCellValue(achieved);
            }

            if (message_no == 3) {
                cell15x.setCellValue(achieved);
            }

            if (message_no == 4) {
                cell16x.setCellValue(achieved);
            }

            if (message_no == 5) {
                cell17x.setCellValue(achieved);
            }

            if (message_no == 6) {
                cell18x.setCellValue(achieved);
            }

            if (message_no == 7) {
                cell19x.setCellValue(achieved);
            }

            if (message_no == 8) {
                cell20x.setCellValue(achieved);
            }

            if (message_no == 9) {
                cell21x.setCellValue(achieved);
            }

            if (message_no == 10) {
                cell22x.setCellValue(achieved);
            }

            if (message_no == 11) {
                cell23x.setCellValue(achieved);
            }

            if (message_no == 12) {
                cell24x.setCellValue(achieved);
            }

            if (message_no == 13) {
                cell25x.setCellValue(achieved);
            }

            cell0x.setCellStyle(styleBorder);
            cell1x.setCellStyle(styleBorder);
            cell2x.setCellStyle(styleBorder);
            cell3x.setCellStyle(styleBorder);
            cell4x.setCellStyle(styleBorder);
            //cell5x.setCellStyle(styleBorder);

            cell13x.setCellStyle(styleBorder);
            cell14x.setCellStyle(styleBorder);
            cell15x.setCellStyle(styleBorder);
            cell16x.setCellStyle(styleBorder);
            cell17x.setCellStyle(styleBorder);
            cell18x.setCellStyle(styleBorder);
            cell19x.setCellStyle(styleBorder);
            cell20x.setCellStyle(styleBorder);
            cell21x.setCellStyle(styleBorder);
            cell22x.setCellStyle(styleBorder);
            cell23x.setCellStyle(styleBorder);
            cell24x.setCellStyle(styleBorder);
            cell25x.setCellStyle(styleBorder);

            i++;
        } else if (!current_group.equals(previous_group)) {
            //      update attended sessions;  
            previous_group = current_group;
            //  OUTPUT SERVICES PROVIDED================================     
            HSSFRow rw4x = shet1.createRow(i);
            rw4.setHeightInPoints(45);
            rw4.setRowStyle(style2);
            // rw4.createCell(1).setCellValue("Number");
            HSSFCell cell0x, cell1x, cell2x, cell3x, cell4x, cell5x, cell6x, cell7x, cell8x, cell9x, cell10x,
                    cell11x, cell12x, cell13x, cell14x, cell15x, cell16x, cell17x, cell18x, cell19x, cell20x;
            HSSFCell cell21x, cell22x, cell23x, cell24x, cell25x, cell26x, cell27x, cell28x, cell29x, cell30x,
                    cell31x, cell32x, cell33x;
            cell0x = rw4x.createCell(0);
            cell1x = rw4x.createCell(1);
            cell2x = rw4x.createCell(2);
            cell3x = rw4x.createCell(3);
            cell4x = rw4x.createCell(4);
            //   cell5x=rw4x.createCell(5);
            cell13x = rw4x.createCell(13);
            cell14x = rw4x.createCell(14);
            cell15x = rw4x.createCell(15);
            cell16x = rw4x.createCell(16);
            cell17x = rw4x.createCell(17);
            cell18x = rw4x.createCell(18);
            cell19x = rw4x.createCell(19);
            cell20x = rw4x.createCell(20);
            cell21x = rw4x.createCell(21);
            cell22x = rw4x.createCell(22);
            cell23x = rw4x.createCell(23);
            cell24x = rw4x.createCell(24);
            cell25x = rw4x.createCell(25);

            cell0x.setCellValue(county);
            cell1x.setCellValue(partner);
            cell2x.setCellValue(district);
            cell3x.setCellValue(hf);
            cell4x.setCellValue(groupname);
            // cell5x.setCellValue(achieved);
            if (message_no == 1) {
                cell13x.setCellValue(achieved);
            }

            if (message_no == 2) {
                cell14x.setCellValue(achieved);
            }

            if (message_no == 3) {
                cell15x.setCellValue(achieved);
            }

            if (message_no == 4) {
                cell16x.setCellValue(achieved);
            }

            if (message_no == 5) {
                cell17x.setCellValue(achieved);
            }

            if (message_no == 6) {
                cell18x.setCellValue(achieved);
            }

            if (message_no == 7) {
                cell19x.setCellValue(achieved);
            }

            if (message_no == 8) {
                cell20x.setCellValue(achieved);
            }

            if (message_no == 9) {
                cell21x.setCellValue(achieved);
            }

            if (message_no == 10) {
                cell22x.setCellValue(achieved);
            }

            if (message_no == 11) {
                cell23x.setCellValue(achieved);
            }

            if (message_no == 12) {
                cell24x.setCellValue(achieved);
            }

            if (message_no == 13) {
                cell25x.setCellValue(achieved);
            }

            cell0x.setCellStyle(styleBorder);
            cell1x.setCellStyle(styleBorder);
            cell2x.setCellStyle(styleBorder);
            cell3x.setCellStyle(styleBorder);
            cell4x.setCellStyle(styleBorder);
            //cell5x.setCellStyle(styleBorder);

            cell13x.setCellStyle(styleBorder);
            cell14x.setCellStyle(styleBorder);
            cell15x.setCellStyle(styleBorder);
            cell16x.setCellStyle(styleBorder);
            cell17x.setCellStyle(styleBorder);
            cell18x.setCellStyle(styleBorder);
            cell19x.setCellStyle(styleBorder);
            cell20x.setCellStyle(styleBorder);
            cell21x.setCellStyle(styleBorder);
            cell22x.setCellStyle(styleBorder);
            cell23x.setCellStyle(styleBorder);
            cell24x.setCellStyle(styleBorder);
            cell25x.setCellStyle(styleBorder);

            i++;
        }

        else if (current_group.equals(previous_group) && currentDistrict.equals(previousDistrict)) {
            HSSFRow rw4x = shet1.getRow(i - 1);
            if (message_no == 1) {
                HSSFCell cell13x = rw4x.createCell(13);
                cell13x.setCellValue(achieved);
                cell13x.setCellStyle(styleBorder);
            }
            if (message_no == 2) {
                HSSFCell cell14x = rw4x.createCell(14);
                cell14x.setCellValue(achieved);
                cell14x.setCellStyle(styleBorder);
            }
            if (message_no == 3) {
                HSSFCell cell15x = rw4x.createCell(15);
                cell15x.setCellValue(achieved);
                cell15x.setCellStyle(styleBorder);
            }
            if (message_no == 4) {
                HSSFCell cell16x = rw4x.createCell(16);
                cell16x.setCellValue(achieved);
                cell16x.setCellStyle(styleBorder);
            }
            if (message_no == 5) {
                HSSFCell cell17x = rw4x.createCell(17);
                cell17x.setCellValue(achieved);
                cell17x.setCellStyle(styleBorder);
            }
            if (message_no == 6) {
                HSSFCell cell18x = rw4x.createCell(18);
                cell18x.setCellValue(achieved);
                cell18x.setCellStyle(styleBorder);
            }
            if (message_no == 7) {
                HSSFCell cell19x = rw4x.createCell(19);
                cell19x.setCellValue(achieved);
                cell19x.setCellStyle(styleBorder);
            }
            if (message_no == 8) {
                HSSFCell cell20x = rw4x.createCell(20);
                cell20x.setCellValue(achieved);
                cell20x.setCellStyle(styleBorder);
            }
            if (message_no == 9) {
                HSSFCell cell21x = rw4x.createCell(21);
                cell21x.setCellValue(achieved);
                cell21x.setCellStyle(styleBorder);
            }
            if (message_no == 10) {
                HSSFCell cell22x = rw4x.createCell(22);
                cell22x.setCellValue(achieved);
                cell22x.setCellStyle(styleBorder);
            }
            if (message_no == 11) {
                HSSFCell cell23x = rw4x.createCell(23);
                cell23x.setCellValue(achieved);
                cell23x.setCellStyle(styleBorder);
            }
            if (message_no == 12) {
                HSSFCell cell24x = rw4x.createCell(24);
                cell24x.setCellValue(achieved);
                cell24x.setCellStyle(styleBorder);
            }
            if (message_no == 13) {
                HSSFCell cell25x = rw4x.createCell(25);
                cell25x.setCellValue(achieved);
                cell25x.setCellStyle(styleBorder);
            }

        } else {
            System.out.println("here-------------nothing seen");
        }
    }
    System.out.println("here : " + i);

    if (conn.rs != null) {
        conn.rs.close();
    }
    if (conn.st != null) {
        conn.st.close();
    }
    if (conn.rs1 != null) {
        conn.rs1.close();
    }
    if (conn.st1 != null) {
        conn.st1.close();
    }
    if (conn.rs2 != null) {
        conn.rs2.close();
    }
    if (conn.st2 != null) {
        conn.st2.close();
    }
    if (conn.st3 != null) {
        conn.st3.close();
    }
    if (conn.pst != null) {
        conn.pst.close();
    }
    if (conn.pst != null) {
        conn.pst.close();
    }
    if (conn.pst1 != null) {
        conn.pst1.close();
    }
    if (conn.pst1 != null) {
        conn.pst1.close();
    }
    if (conn.conn != null) {
        conn.conn.close();
    }

    // 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=PWP_Raw_Data.xls");
    OutputStream outStream = response.getOutputStream();
    outStream.write(outArray);
    outStream.flush();
}

From source file:reports.reportsTracker731.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, SQLException {

    dbConn conn = new dbConn();
    session = request.getSession();/*from  www .  j  a  v  a 2s  . c  o  m*/

    year = Integer.parseInt(request.getParameter("year"));
    //        year=2015;
    prevYear = year - 1;
    IdGenerator IG = new IdGenerator();
    allMonths.clear();
    allReports.clear();
    duration = "WHERE (moh731.yearmonth BETWEEN " + prevYear + "" + 10 + " AND " + year
            + "09) AND (subpartnera.PMTCT=1 OR subpartnera.ART=1 OR subpartnera.PEP=1 OR subpartnera.HTC=1)";

    currentMonth = IG.CurrentMonth();

    monthsData = "";
    //            ^^^^^^^^^^^^^CREATE STATIC AND WRITE STATIC DATA TO THE EXCELL^^^^^^^^^^^^
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet shet1 = wb.createSheet("MOH 731 REPORTS TRACKER");

    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short) 18);
    font.setFontName("Arial Black");
    font.setColor((short) 0000);
    CellStyle style = wb.createCellStyle();
    style.setFont(font);
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    HSSFFont font2 = wb.createFont();
    font2.setFontName("Arial Black");
    font2.setColor((short) 0000);
    CellStyle style2 = wb.createCellStyle();
    style2.setFont(font2);

    HSSFCellStyle stborder = wb.createCellStyle();
    stborder.setBorderTop(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderRight(HSSFCellStyle.BORDER_THIN);
    stborder.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    stborder.setWrapText(true);

    HSSFCellStyle stylex = wb.createCellStyle();
    stylex.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    stylex.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    stylex.setBorderTop(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderRight(HSSFCellStyle.BORDER_THIN);
    stylex.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    HSSFFont fontx = wb.createFont();
    fontx.setColor(HSSFColor.DARK_BLUE.index);
    stylex.setFont(fontx);
    stylex.setWrapText(true);

    HSSFCellStyle styleHeader = wb.createCellStyle();
    styleHeader.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    styleHeader.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    styleHeader.setBorderTop(HSSFCellStyle.BORDER_THIN);
    styleHeader.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    styleHeader.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    styleHeader.setBorderRight(HSSFCellStyle.BORDER_THIN);
    styleHeader.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    HSSFFont fontHeader = wb.createFont();
    fontHeader.setColor(HSSFColor.DARK_BLUE.index);
    styleHeader.setFont(fontHeader);
    styleHeader.setWrapText(true);

    for (int i = 0; i <= 2; i++) {
        shet1.setColumnWidth(i, 8000);
    }
    HSSFRow rw1S1 = shet1.createRow(0);
    HSSFCell S1cell = rw1S1.createCell(0);
    S1cell.setCellValue("COUNTY NAME");
    S1cell.setCellStyle(stylex);

    HSSFCell S1cellX = rw1S1.createCell(1);
    S1cellX.setCellValue("SUB COUNTY");
    S1cellX.setCellStyle(stylex);

    S1cellX = rw1S1.createCell(2);
    S1cellX.setCellValue("HEALTH FACILITY");
    S1cellX.setCellStyle(stylex);

    S1cellX = rw1S1.createCell(3);
    S1cellX.setCellValue("MFL CODE");
    S1cellX.setCellStyle(stylex);

    S1cellX = rw1S1.createCell(4);
    S1cellX.setCellValue("EXPECTED REPORTS");
    S1cellX.setCellStyle(stylex);

    counterHeader = 5;
    String getMaxandMin = "SELECT DISTINCT(month.name),month.id FROM moh731 JOIN month ON moh731.Mois=month.id JOIN subpartnera ON moh731.SubPartnerID=subpartnera.SubPartnerID "
            + duration + " " + " ORDER BY  moh731.yearmonth";
    conn.rs = conn.st.executeQuery(getMaxandMin);
    while (conn.rs.next()) {
        monthName = conn.rs.getString(1);
        monthid = conn.rs.getInt(2);
        if (monthid <= 9) {
            currentYear = year;
        } else {
            currentYear = prevYear;
        }
        System.out.println(" Months are : " + monthName);
        allMonths.add(monthName);
        allReports.add(0);
        S1cellX = rw1S1.createCell(counterHeader);
        S1cellX.setCellValue(monthName);
        S1cellX.setCellStyle(stylex);
        counterHeader++;
    }
    prevFacility = currentFacility = "";
    currentDistrict = prevDistrict = "";
    currentCounty = prevCounty = "";
    counter = districtCounter = countyCounter = districtsMerged = 0;
    arraySize = allReports.size();

    if (allMonths.size() > 0) {
        String checkReports = "SELECT county.County,district.DistrictNom,subpartnera.SubPartnerNom,"
                + "subpartnera.CentreSanteId,COUNT(moh731.SubPartnerID),month.name,subpartnera.SubPartnerID "
                + "FROM subpartnera " + "LEFT JOIN moh731 ON moh731.SubPartnerID=subpartnera.SubPartnerID "
                + "LEFT JOIN district ON subpartnera.DistrictID=district.DistrictID "
                + "LEFT JOIN county ON county.CountyID=district.CountyID "
                + "LEFT JOIN month ON moh731.Mois=month.id " + " " + duration + " "
                + " GROUP BY subpartnera.SubPartnerNom,moh731.Annee,moh731.Mois "
                + "ORDER BY county.County,district.DistrictNom,subpartnera.SubPartnerNom,moh731.Mois";
        System.out.println(checkReports);
        conn.rs = conn.st.executeQuery(checkReports);
        while (conn.rs.next()) {
            countyName = conn.rs.getString(1);
            districtName = conn.rs.getString(2);
            facilityName = conn.rs.getString(3);
            mflcode = conn.rs.getString(4);
            status = conn.rs.getInt(5);
            selectedMonth = conn.rs.getString(6);
            currentFacility = conn.rs.getString(7);
            currentDistrict = districtName;
            currentCounty = countyName;
            //     CHECK WHERE TO PLACE THE NUMBER; 

            monthPosition = allMonths.indexOf(selectedMonth);

            if (!prevFacility.equals(currentFacility)) {

                if (!prevDistrict.equals(currentDistrict) && !prevDistrict.equals("")) {
                    counter++;
                    noReports = districtCounter + 1;
                    HSSFRow rwTotal = shet1.createRow(counter);
                    HSSFCell SX = rwTotal.createCell(0);
                    SX.setCellStyle(stborder);

                    SX = rwTotal.createCell(1);
                    SX.setCellValue(prevDistrict + " TOTALS : ");
                    SX.setCellStyle(styleHeader);

                    SX = rwTotal.createCell(2);
                    SX.setCellStyle(styleHeader);

                    SX = rwTotal.createCell(3);
                    SX.setCellStyle(styleHeader);

                    SX = rwTotal.createCell(4);
                    SX.setCellValue(noReports);
                    SX.setCellStyle(styleHeader);
                    shet1.addMergedRegion(new CellRangeAddress(counter, counter, 1, 3));

                    for (int j = 0; j < allReports.size(); j++) {
                        //         System.out.println("district name : "+prevDistrict+" no of reports : "+allReports.get(j).toString()+" for month : "+allMonths.get(j).toString());
                        int dataPos = 5 + j;
                        SX = rwTotal.createCell(dataPos);
                        SX.setCellValue(Integer.parseInt(allReports.get(j).toString()));
                        SX.setCellStyle(styleHeader);
                    }
                    for (int k = 0; k < arraySize; k++) {
                        allReports.set(k, 0);
                    }
                }
                counter++;
                HSSFRow rw1 = shet1.createRow(counter);
                HSSFCell S1 = rw1.createCell(0);
                S1.setCellValue(countyName);
                S1.setCellStyle(stborder);

                S1 = rw1.createCell(1);
                S1.setCellValue(districtName);
                S1.setCellStyle(stborder);

                S1 = rw1.createCell(2);
                S1.setCellValue(facilityName);
                S1.setCellStyle(stborder);

                S1 = rw1.createCell(3);
                S1.setCellValue(mflcode);
                S1.setCellStyle(stborder);

                S1 = rw1.createCell(4);
                S1.setCellValue(1);
                S1.setCellStyle(stborder);

                for (int j = 0; j < allMonths.size(); j++) {
                    int cellPos = j + 5;
                    S1 = rw1.createCell(cellPos);
                    // System.out.println("counter : "+counter+" datapos : "+cellPos+" status : "+status);
                    S1.setCellStyle(stborder);

                }
                int dataPos = 5 + monthPosition;
                S1 = rw1.getCell(dataPos);
                S1.setCellValue(status);

                if (!prevDistrict.equals(currentDistrict) && !prevDistrict.equals("")) {
                    int distStart = counter - districtCounter - 2;
                    int distEnd = counter - 2;
                    shet1.addMergedRegion(new CellRangeAddress(distStart, distEnd, 1, 1));
                    districtsMerged++;
                    districtCounter = 0;

                    for (int j = 0; j < arraySize; j++) {
                        allReports.set(j, 0);
                    }

                } else {
                    if (counter == 1) {
                    } else {
                        districtCounter++;
                    }
                }
                if (!prevCounty.equals(currentCounty) && !prevCounty.equals("")) {
                    int countyStart = counter - countyCounter - districtsMerged - 1;
                    int countyEnd = counter - 1;
                    shet1.addMergedRegion(new CellRangeAddress(countyStart, countyEnd, 0, 0));
                    countyCounter = 0;
                    districtsMerged = 0;
                } else {
                    if (counter == 1) {
                    } else {
                        countyCounter++;
                    }
                }
                prevCounty = currentCounty;
                prevDistrict = currentDistrict;
            } else {

                HSSFRow rw1 = shet1.getRow(counter);
                int dataPos = 5 + monthPosition;
                HSSFCell S1 = rw1.getCell(dataPos);
                S1.setCellValue(status);
                //    
            }

            if (status == 1) {
                int currentData = Integer.parseInt(allReports.get(monthPosition).toString()) + 1;
                allReports.set(monthPosition, currentData);
            }

            prevFacility = currentFacility;

        }
        //    MATCH THE LAST DISTRICTS
        counter++;
        noReports = districtCounter + 1;
        HSSFRow rwTotal = shet1.createRow(counter);
        HSSFCell SX = rwTotal.createCell(0);
        SX.setCellStyle(stborder);

        SX = rwTotal.createCell(1);
        SX.setCellValue(prevDistrict + " TOTALS : ");
        SX.setCellStyle(styleHeader);

        SX = rwTotal.createCell(2);
        SX.setCellStyle(styleHeader);

        SX = rwTotal.createCell(3);
        SX.setCellStyle(styleHeader);

        SX = rwTotal.createCell(4);
        SX.setCellValue(noReports);
        SX.setCellStyle(styleHeader);
        shet1.addMergedRegion(new CellRangeAddress(counter, counter, 1, 3));

        int distStart = counter - districtCounter - 1;
        int distEnd = counter - 1;
        //     System.out.println("MERGE BETWEEN : START : "+distStart+" END : "+distEnd);
        shet1.addMergedRegion(new CellRangeAddress(distStart, distEnd, 1, 1));
        districtCounter = 0;

        int countyStart = counter - countyCounter - 2;
        int countyEnd = counter;
        //        System.out.println("MERGE BETWEEN : START : "+distStart+" END : "+distEnd);
        shet1.addMergedRegion(new CellRangeAddress(countyStart, countyEnd, 0, 0));
        countyCounter = 0;

        for (int j = 0; j < allReports.size(); j++) {
            //         System.out.println("district name : "+prevDistrict+" no of reports : "+allReports.get(j).toString()+" for month : "+allMonths.get(j).toString());
            int dataPos = 5 + j;
            SX = rwTotal.createCell(dataPos);
            SX.setCellValue(Integer.parseInt(allReports.get(j).toString()));
            SX.setCellStyle(styleHeader);
        }

        if (conn.st != null) {
            conn.st.close();
        }
        if (conn.st1 != null) {
            conn.st1.close();
        }
        if (conn.st2 != null) {
            conn.st2.close();
        }

        if (conn.rs != null) {
            conn.rs.close();
        }
        if (conn.rs1 != null) {
            conn.rs1.close();
        }
        if (conn.rs2 != null) {
            conn.rs2.close();
        }
        if (conn.conn != null) {
            conn.conn.close();
        }

        createdOn = IG.CreatedOn();

        // 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=Reporting_summary_for_PEPFAR_YEAR("
                + year + ")_" + createdOn.trim() + ".xls");
        OutputStream outStream = response.getOutputStream();
        outStream.write(outArray);
        outStream.flush();
    } else {

        session.setAttribute("noTrackerReport",
                "<font color=\"red\"><b>SORRY:</b> No report was found for " + year + ".</red>");
        response.sendRedirect("reportsTracker.jsp");
    }

}

From source file:reports.servicesProvided.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, SQLException {
    session = request.getSession();/*w w w  . ja  va  2 s  .  c o  m*/
    dbConn conn = new dbConn();

    //            ^^^^^^^^^^^^^CREATE STATIC AND WRITE STATIC DATA TO THE EXCELL^^^^^^^^^^^^
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet shet1 = wb.createSheet();
    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short) 18);
    font.setFontName("Arial Black");
    //    font.setItalic(true);
    //    font.setBoldweight((short)12);
    font.setColor((short) 0000);
    CellStyle style = wb.createCellStyle();
    style.setFont(font);
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    HSSFFont font2 = wb.createFont();
    // font2.setFontHeightInPoints((short)15);
    font2.setFontName("Arial Black");
    //    font.setItalic(true);
    //    font2.setBoldweight((short)18);
    font2.setColor((short) 0000);
    CellStyle style2 = wb.createCellStyle();
    style2.setFont(font2);

    HSSFCellStyle stborder = wb.createCellStyle();
    stborder.setBorderTop(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderRight(HSSFCellStyle.BORDER_THIN);
    //    stborder.setFillForegroundColor(HSSFColor.ORANGE.index);
    //    stborder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    stborder.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    //  HSSFSheet sheet1 = wb.getSheetAt(0);
    shet1.setColumnWidth(0, 6000);
    shet1.setColumnWidth(1, 6000);
    shet1.setColumnWidth(2, 6000);
    shet1.setColumnWidth(3, 6000);

    shet1.setColumnWidth(4, 8000);

    //    shet1.setColumnWidth(20, 2000);
    HSSFCellStyle styleBorder = wb.createCellStyle();
    styleBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);
    styleBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    styleBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    styleBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);
    styleBorder.setFillForegroundColor(HSSFColor.ORANGE.index);
    styleBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    styleBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    HSSFRow rw1 = shet1.createRow(1);
    HSSFCell cell;
    cell = rw1.createCell(0);
    cell.setCellValue("PWP SERVICES PROVIDED PER MONTH ");
    cell.setCellStyle(style);
    rw1.setHeightInPoints(30);

    //  Merge the cells
    shet1.addMergedRegion(new CellRangeAddress(1, 1, 0, 4));

    //  CREATE HEADING 2
    HSSFRow rheading2 = shet1.createRow(2);
    rheading2.setHeightInPoints(25);
    HSSFCell cellxx1, cellxx2, cellxx3, cellxx4, cellxx5, cellxx6;
    cellxx1 = rheading2.createCell(0);
    cellxx2 = rheading2.createCell(1);
    cellxx3 = rheading2.createCell(2);
    cellxx4 = rheading2.createCell(3);
    cellxx5 = rheading2.createCell(4);
    //    cellxx6=rheading2.createCell(5);

    cellxx1.setCellValue("COUNTY NAME");
    cellxx2.setCellValue("PARTNER NAME");
    cellxx3.setCellValue("YEAR");
    cellxx4.setCellValue("MONTH");
    cellxx5.setCellValue("NO. OF CLIENTS GIVEN SERVICES");

    cellxx1.setCellStyle(styleBorder);
    cellxx2.setCellStyle(styleBorder);
    cellxx3.setCellStyle(styleBorder);
    cellxx4.setCellStyle(styleBorder);
    cellxx5.setCellStyle(styleBorder);

    pos = 3;

    HSSFCellStyle stylex = wb.createCellStyle();
    //stylex.setFillForegroundColor(HSSFColor.LIME.index);
    //stylex.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    stylex.setBorderTop(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderRight(HSSFCellStyle.BORDER_THIN);
    stylex.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    HSSFFont fontx = wb.createFont();
    fontx.setColor(HSSFColor.DARK_BLUE.index);
    stylex.setFont(fontx);
    stylex.setWrapText(true);

    String getData = "SELECT county.county_name AS COUNTY,partner.partner_name AS PARTNER, "
            + "COUNT(DISTINCT services_provided.client_id) AS ACHIEVED,"
            + "services_provided.submission_year,services_provided.submission_month," + " CASE"
            + "      WHEN max(services_provided.submission_month) BETWEEN 01 AND 03 THEN 'Q2' "
            + "      WHEN max(services_provided.submission_month) BETWEEN 04 AND 06 THEN 'Q3' "
            + "      WHEN max(services_provided.submission_month) BETWEEN 07 AND 09 THEN 'Q4' "
            + "      WHEN max(services_provided.submission_month) BETWEEN 10 AND 12 THEN 'Q1' "
            + "      ELSE 'NOT SELECTED' " + "      END AS QUARTER, " + "CASE"
            + " when services_provided.submission_month =01 THEN 'JAN'"
            + " when services_provided.submission_month =02 THEN 'FEB'"
            + " when services_provided.submission_month =03 THEN 'MAR'"
            + " when services_provided.submission_month=04 THEN 'APR'"
            + " when services_provided.submission_month=05 THEN 'MAY'"
            + " when services_provided.submission_month=06 THEN 'JUN'"
            + " when services_provided.submission_month=07 THEN 'JUL'"
            + " when services_provided.submission_month=08 THEN 'AUG'"
            + " when services_provided.submission_month=09 THEN 'SEPT'"
            + " when services_provided.submission_month=10 THEN 'OCT'"
            + " when services_provided.submission_month=11 THEN 'NOV'"
            + " when services_provided.submission_month=12 THEn 'DEC'" + " END AS MONTHS "
            + "FROM personal_information JOIN services_provided ON personal_information.client_id=services_provided.client_id "
            + "JOIN partner ON partner.partner_id=personal_information.partner_id JOIN (district JOIN county ON county.county_id=district.county_id) "
            + "ON district.district_id=personal_information.district_id "
            + "WHERE contraceptive_method='YES' OR rsp='YES' OR cds_given>0 OR screened_tb='YES' OR screened_stis='YES' "
            + "OR tested_partner='YES' OR tested_children='YES' OR disclosed_status='YES' "
            + "GROUP BY county.county_name,partner.partner_name,services_provided.submission_year,services_provided.submission_month";
    conn.rs = conn.st.executeQuery(getData);
    while (conn.rs.next()) {
        countyname = conn.rs.getString(1);
        partnername = conn.rs.getString(2);
        achieved = conn.rs.getInt(3);
        month = conn.rs.getString(7);
        year = conn.rs.getInt(4);
        //  CREATE ROW AND ADD DATA TO THE DATA CELLS======================
        HSSFRow data = shet1.createRow(pos);
        data.setHeightInPoints(25);
        HSSFCell cellx1, cellx2, cellx3, cellx4, cellx5, cellx6;
        cellx1 = data.createCell(0);
        cellx2 = data.createCell(1);
        cellx3 = data.createCell(2);
        cellx4 = data.createCell(3);
        cellx5 = data.createCell(4);
        //    cellxx6=rheading2.createCell(5);

        cellx1.setCellValue(countyname);
        cellx2.setCellValue(partnername);
        cellx3.setCellValue(year);
        cellx4.setCellValue(month);
        cellx5.setCellValue(achieved);

        cellx1.setCellStyle(stylex);
        cellx2.setCellStyle(stylex);
        cellx3.setCellStyle(stylex);
        cellx4.setCellStyle(stylex);
        cellx5.setCellStyle(stylex);

        System.out.println("county : " + countyname + " partner : " + partnername + " ahieved:" + achieved
                + " month: " + month + " quarter: " + quarter);

        pos++;
    }

    if (conn.rs != null) {
        conn.rs.close();
    }
    if (conn.st != null) {
        conn.st.close();
    }
    if (conn.rs1 != null) {
        conn.rs1.close();
    }
    if (conn.st1 != null) {
        conn.st1.close();
    }
    if (conn.rs2 != null) {
        conn.rs2.close();
    }
    if (conn.st2 != null) {
        conn.st2.close();
    }
    if (conn.st3 != null) {
        conn.st3.close();
    }
    if (conn.pst != null) {
        conn.pst.close();
    }
    if (conn.pst != null) {
        conn.pst.close();
    }
    if (conn.pst1 != null) {
        conn.pst1.close();
    }
    if (conn.pst1 != null) {
        conn.pst1.close();
    }
    if (conn.conn != null) {
        conn.conn.close();
    }

    // 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=PWP_SERVICES_PROVIDED_REPORT.xls");
    OutputStream outStream = response.getOutputStream();
    outStream.write(outArray);
    outStream.flush();
}

From source file:reports.sessionsBetween.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, SQLException {
    dbConn conn = new dbConn();
    session = request.getSession();//from www.j  a va 2s .c om

    String[] starter = request.getParameter("start_date").split("/");
    String[] ender = request.getParameter("end_date").split("/");

    //   String [] starter="10/02/2010".split("/");
    //   String [] ender="24/10/2014".split("/");
    String m1 = "", m2 = "";
    String d1 = "", d2 = "", y1 = "", y2 = "";

    start = request.getParameter("start_date");
    end = request.getParameter("end_date");

    m1 = starter[1];
    m2 = ender[1];
    d1 = starter[0];
    d2 = ender[0];
    y1 = starter[2];
    y2 = ender[2];

    start_dateKey = m1 + "/" + d1 + "/" + y1;
    end_dateKey = m2 + "/" + d2 + "/" + y2;

    //   System.out.println("");

    //            ^^^^^^^^^^^^^CREATE STATIC AND WRITE STATIC DATA TO THE EXCELL^^^^^^^^^^^^
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet shet1 = wb.createSheet();
    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short) 18);
    font.setFontName("Arial Black");
    //    font.setItalic(true);
    //    font.setBoldweight((short)12);
    font.setColor((short) 0000);
    CellStyle style = wb.createCellStyle();
    style.setFont(font);
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    HSSFFont font2 = wb.createFont();
    // font2.setFontHeightInPoints((short)15);
    font2.setFontName("Arial Black");
    //    font.setItalic(true);
    //    font2.setBoldweight((short)18);
    font2.setColor((short) 0000);
    CellStyle style2 = wb.createCellStyle();
    style2.setFont(font2);

    HSSFCellStyle stborder = wb.createCellStyle();
    stborder.setBorderTop(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderRight(HSSFCellStyle.BORDER_THIN);
    //    stborder.setFillForegroundColor(HSSFColor.ORANGE.index);
    //    stborder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    stborder.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    //  HSSFSheet sheet1 = wb.getSheetAt(0);
    shet1.setColumnWidth(0, 4000);
    shet1.setColumnWidth(1, 4000);
    shet1.setColumnWidth(2, 4000);
    shet1.setColumnWidth(3, 4500);

    shet1.setColumnWidth(4, 4300);

    shet1.setColumnWidth(5, 4300);
    shet1.setColumnWidth(6, 4000);
    shet1.setColumnWidth(7, 4200);
    shet1.setColumnWidth(8, 4200);
    shet1.setColumnWidth(9, 4200);

    HSSFCellStyle styleBorder = wb.createCellStyle();
    styleBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);
    styleBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    styleBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    styleBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);
    styleBorder.setFillForegroundColor(HSSFColor.ORANGE.index);
    styleBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    styleBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    HSSFRow rw1 = shet1.createRow(1);
    HSSFCell cell;
    cell = rw1.createCell(0);
    cell.setCellValue("PWP number of individual provided with services from " + start + " to " + end);
    cell.setCellStyle(style);
    rw1.setHeightInPoints(30);

    //  Merge the cells
    shet1.addMergedRegion(new CellRangeAddress(1, 1, 0, 9));

    HSSFRow rw4 = shet1.createRow(2);
    rw4.setHeightInPoints(45);
    rw4.setRowStyle(style2);
    // rw4.createCell(1).setCellValue("Number");
    HSSFCell cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9, cell10;

    cell1 = rw4.createCell(0);
    cell2 = rw4.createCell(1);
    cell3 = rw4.createCell(2);
    cell4 = rw4.createCell(3);
    cell5 = rw4.createCell(4);
    cell6 = rw4.createCell(5);
    cell7 = rw4.createCell(6);
    cell8 = rw4.createCell(7);
    cell9 = rw4.createCell(8);
    cell10 = rw4.createCell(9);

    cell1.setCellValue("County Name");
    cell2.setCellValue("Partner Name");
    cell3.setCellValue("Received Contraceptives");
    cell4.setCellValue("Reffered To Service Point");
    cell5.setCellValue("Given Condoms");
    cell6.setCellValue("Screened For TB");
    cell7.setCellValue("Screened For STIs");
    cell8.setCellValue("Partner Tested");
    cell9.setCellValue("Children Tested");
    cell10.setCellValue("Disclosed Status");

    HSSFCellStyle stylex = wb.createCellStyle();
    stylex.setFillForegroundColor(HSSFColor.LIME.index);
    stylex.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    stylex.setBorderTop(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderRight(HSSFCellStyle.BORDER_THIN);
    stylex.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    HSSFFont fontx = wb.createFont();
    fontx.setColor(HSSFColor.DARK_BLUE.index);
    stylex.setFont(fontx);
    stylex.setWrapText(true);
    cell1.setCellStyle(stylex);
    cell2.setCellStyle(stylex);
    cell3.setCellStyle(stylex);
    cell4.setCellStyle(stylex);
    cell5.setCellStyle(stylex);
    cell6.setCellStyle(stylex);
    cell7.setCellStyle(stylex);
    cell8.setCellStyle(stylex);
    cell9.setCellStyle(stylex);
    cell10.setCellStyle(stylex);

    pos = 3;

    String ct_selector = "SELECT * FROM county";
    conn.rs3 = conn.st3.executeQuery(ct_selector);
    while (conn.rs3.next()) {
        county_name = conn.rs3.getString(2);
        county_id = conn.rs3.getString(1);
        String partner_name_selector = "SELECT * FROM partner";
        conn.rs = conn.st.executeQuery(partner_name_selector);
        while (conn.rs.next()) {
            partner_name = conn.rs.getString(2);
            partner_id = conn.rs.getString(1);
            cm2 = rsp2 = tb2 = stis2 = partner2 = children2 = status2 = cds2 = 0;
            all_clients = 0;
            //                   pos++;
            String district_selector = "SELECT district_id FROM district WHERE county_id='" + county_id + "'";
            conn.rs1 = conn.st1.executeQuery(district_selector);
            while (conn.rs1.next()) {
                String district_id = conn.rs1.getString(1);

                String getClients = "SELECT client_id FROM personal_information WHERE partner_id='" + partner_id
                        + "' && district_id='" + district_id + "'";
                conn.rs4 = conn.st4.executeQuery(getClients);
                while (conn.rs4.next()) {
                    String client_id = conn.rs4.getString(1);
                    cm1 = rsp1 = tb1 = stis1 = partner1 = children1 = status1 = cds1 = 0;
                    String getServices = "SELECT * FROM services_provided WHERE client_id='" + client_id
                            + "' && " + "STR_TO_DATE(submission_date,'%m/%d/%Y') BETWEEN STR_TO_DATE('"
                            + start_dateKey + "','%m/%d/%Y') AND STR_TO_DATE('" + end_dateKey
                            + "','%m/%d/%Y') ";
                    System.out.println(getServices);
                    conn.rs2 = conn.st2.executeQuery(getServices);
                    while (conn.rs2.next()) {
                        cm = rsp = tb = stis = partner = children = status = "";
                        System.out.println("client id : " + client_id);
                        cds = 0;
                        cm = conn.rs2.getString("contraceptive_method");
                        rsp = conn.rs2.getString("rsp");
                        tb = conn.rs2.getString("screened_tb");
                        stis = conn.rs2.getString("screened_stis");
                        partner = conn.rs2.getString("tested_partner");
                        children = conn.rs2.getString("tested_children");
                        status = conn.rs2.getString("disclosed_status");
                        cds = conn.rs2.getInt("cds_given");

                        if (cm.equals("YES")) {
                            cm1++;
                        }
                        if (rsp.equals("YES")) {
                            rsp1++;
                        }
                        if (tb.equals("YES")) {
                            tb1++;
                        }
                        if (stis.equals("YES")) {
                            stis1++;
                        }
                        if (partner.equals("YES")) {
                            partner1++;
                        }
                        if (children.equals("YES")) {
                            children1++;
                        }
                        if (status.equals("YES")) {
                            status1++;
                        }
                        if (cds > 0) {
                            cds1++;
                        }

                    }
                    //    ADD TO THE RESPECTIVE SERVICES PROVIDED========================================
                    if (cm1 > 0) {
                        cm2++;
                    }
                    if (rsp1 > 0) {
                        rsp2++;
                    }
                    if (tb1 > 0) {
                        tb2++;
                    }
                    if (stis1 > 0) {
                        stis2++;
                    }
                    if (partner1 > 0) {
                        partner2++;
                    }
                    if (children1 > 0) {
                        children2++;
                    }
                    if (status1 > 0) {
                        status2++;
                    }
                    if (cds1 > 0) {
                        cds2++;
                    }

                }

            }
            System.out.println("out==" + pos + "-----------" + cm2 + "-" + rsp2 + "-" + tb2 + "-" + stis2 + "-"
                    + partner2 + "-" + children2 + "-" + status2 + "-" + cds2);
            if (cm2 > 0 || rsp2 > 0 || tb2 > 0 || stis2 > 0 || partner2 > 0 || children2 > 0 || status2 > 0
                    || cds2 > 0) {
                HSSFRow rw5 = shet1.createRow(pos);
                rw5.setHeightInPoints(45);
                rw5.setRowStyle(style2);
                // rw4.createCell(1).setCellValue("Number");
                HSSFCell cell1x, cell2x, cell3x, cell4x, cell5x, cell6x, cell7x, cell8x, cell9x, cell10x;

                cell1x = rw5.createCell(0);
                cell2x = rw5.createCell(1);
                cell3x = rw5.createCell(2);
                cell4x = rw5.createCell(3);
                cell5x = rw5.createCell(4);
                cell6x = rw5.createCell(5);
                cell7x = rw5.createCell(6);
                cell8x = rw5.createCell(7);
                cell9x = rw5.createCell(8);
                cell10x = rw5.createCell(9);

                cell1x.setCellValue(county_name);
                cell2x.setCellValue(partner_name);
                cell3x.setCellValue(cm2);
                cell4x.setCellValue(rsp2);
                cell5x.setCellValue(cds2);
                cell6x.setCellValue(tb2);
                cell7x.setCellValue(stis2);
                cell8x.setCellValue(partner2);
                cell9x.setCellValue(children2);
                cell10x.setCellValue(status2);

                cell1x.setCellStyle(stborder);
                cell2x.setCellStyle(stborder);
                cell3x.setCellStyle(stborder);
                cell4x.setCellStyle(stborder);
                cell5x.setCellStyle(stborder);
                cell6x.setCellStyle(stborder);
                cell7x.setCellStyle(stborder);
                cell8x.setCellStyle(stborder);
                cell9x.setCellStyle(stborder);
                cell10x.setCellStyle(stborder);

                pos++;
            }
        }
        //       end of partner
    }

    if (conn.rs != null) {
        conn.rs.close();
    }
    if (conn.st != null) {
        conn.st.close();
    }
    if (conn.rs1 != null) {
        conn.rs1.close();
    }
    if (conn.st1 != null) {
        conn.st1.close();
    }
    if (conn.rs2 != null) {
        conn.rs2.close();
    }
    if (conn.st2 != null) {
        conn.st2.close();
    }
    if (conn.st3 != null) {
        conn.st3.close();
    }
    if (conn.pst != null) {
        conn.pst.close();
    }
    if (conn.pst != null) {
        conn.pst.close();
    }
    if (conn.pst1 != null) {
        conn.pst1.close();
    }
    if (conn.pst1 != null) {
        conn.pst1.close();
    }
    if (conn.conn != null) {
        conn.conn.close();
    }

    // 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=PWP_SERVICES_PROVIDED.xls");
    OutputStream outStream = response.getOutputStream();
    outStream.write(outArray);
    outStream.flush();
}

From source file:reports.staticReportExcel731.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, SQLException {

    dbConn conn = new dbConn();
    session = request.getSession();//from w w  w  .j  a v a  2s . co  m

    //--------------------------------------------------------------------------------
    //--------------------------------------------------------------------------------
    //added later to accomodate the years
    String subpartnerid = "SubPartnerID";
    String subpartnera = "subpartnera";

    int monthint = 0;
    int yearint = 0;

    reportType = request.getParameter("reportType");
    year = Integer.parseInt(request.getParameter("year"));
    reportDuration = request.getParameter("reportDuration");
    yearint = year;
    //        reportType="2";
    //        year=2015;
    //        reportDuration="3";

    prevYear = year - 1;
    maxYearMonth = 0;

    //        GET REPORT DURATION============================================
    startPMTCT = startART = startPEP = startHTC = noPMTCT = noART = noPEP = noHTC = 0;
    if (reportDuration.equals("1")) {

        //_________________________________annualy_____________________________________

        //solve subpartner table and facil_id first            
        if (yearint <= 2014) {
            subpartnerid = "SP_ID";
            subpartnera = "subpartnera2014";
        } else if (yearint > 2015) {
            subpartnerid = "SubPartnerID";
            subpartnera = "subpartnera";
        } else if (yearint == 2015) {
            //this should be skipped since it picks both facil tables. 
            //It has been disabled at the interface position
            subpartnerid = "SubPartnerID";
            subpartnera = "subpartnera";
        }

        duration = " moh731.yearmonth BETWEEN " + prevYear + "10 AND " + year + "09";
    } else if (reportDuration.equals("2")) {
        //_________________________________SemiAnnualy_________________________________

        //oct-mar
        //          if(quarter.equals("1")||quarter.equals("2")){
        //      if(yearint<=2014){
        //       subpartnerid="SP_ID";
        //       subpartnera="subpartnera2014";
        //       }
        //       else if(yearint>2015) {
        //        subpartnerid="SubPartnerID";
        //        subpartnera="subpartnera";
        //       }
        //      else if(yearint==2015){
        //         //for oct-mar, use old database list
        //    subpartnerid="SP_ID";
        //       subpartnera="subpartnera2014";  
        //         
        //      }
        //            
        //          }
        //          else if(quarter.equals("3")||quarter.equals("4")){
        //          //apr-sep
        //          
        //               //apr-sep
        //           
        //              if(yearint<=2014){
        //       subpartnerid="SP_ID";
        //       subpartnera="subpartnera2014";
        //       }
        //       else if(yearint>2015) {
        //        subpartnerid="SubPartnerID";
        //        subpartnera="subpartnera";
        //       }
        //      else if(yearint==2015){
        //        subpartnerid="SubPartnerID";
        //        subpartnera="subpartnera";      
        //         
        //      }
        //              
        //          }

        semi_annual = request.getParameter("semi_annual");
        //        semi_annual="2";
        if (semi_annual.equals("1")) {

            //oct-mar            
            if (yearint <= 2014) {
                subpartnerid = "SP_ID";
                subpartnera = "subpartnera2014";
            } else if (yearint > 2015) {
                subpartnerid = "SubPartnerID";
                subpartnera = "subpartnera";
            } else if (yearint == 2015) {
                //for oct-mar, use old database list
                subpartnerid = "SP_ID";
                subpartnera = "subpartnera2014";

            }

            duration = " moh731.yearmonth BETWEEN " + prevYear + "10 AND " + year + "03";
        } else {

            //apr-sep

            if (yearint <= 2014) {
                subpartnerid = "SP_ID";
                subpartnera = "subpartnera2014";
            } else if (yearint > 2015) {
                subpartnerid = "SubPartnerID";
                subpartnera = "subpartnera";
            } else if (yearint == 2015) {
                subpartnerid = "SubPartnerID";
                subpartnera = "subpartnera";

            }

            duration = " moh731.yearmonth BETWEEN " + year + "04 AND " + year + "09";
        }
    }

    else if (reportDuration.equals("3")) {
        //quarterly

        String startMonth, endMonth;

        //_________________________________Quarterly__________________________________

        quarter = request.getParameter("quarter");
        //specify subparter table and facil id first

        //oct-mar
        if (quarter.equals("1") || quarter.equals("2")) {
            if (yearint <= 2014) {
                subpartnerid = "SP_ID";
                subpartnera = "subpartnera2014";
            } else if (yearint > 2015) {
                subpartnerid = "SubPartnerID";
                subpartnera = "subpartnera";
            } else if (yearint == 2015) {
                //for oct-mar, use old database list
                subpartnerid = "SP_ID";
                subpartnera = "subpartnera2014";

            }

        } else if (quarter.equals("3") || quarter.equals("4")) {
            //apr-sep

            //apr-sep

            if (yearint <= 2014) {
                subpartnerid = "SP_ID";
                subpartnera = "subpartnera2014";
            } else if (yearint > 2015) {
                subpartnerid = "SubPartnerID";
                subpartnera = "subpartnera";
            } else if (yearint == 2015) {
                subpartnerid = "SubPartnerID";
                subpartnera = "subpartnera";

            }

        }

        //       quarter="3";
        String getMonths = "SELECT months FROM quarter WHERE id='" + quarter + "'";
        conn.rs = conn.st.executeQuery(getMonths);
        if (conn.rs.next() == true) {
            String months[] = conn.rs.getString(1).split(",");
            startMonth = months[0];
            endMonth = months[2];
            if (quarter.equals("1")) {
                duration = " moh731.yearmonth BETWEEN " + prevYear + "" + startMonth + " AND " + prevYear + ""
                        + endMonth;
            } else {
                duration = " moh731.yearmonth BETWEEN " + year + "" + startMonth + " AND " + year + ""
                        + endMonth;
            }
        }
    }

    else if (reportDuration.equals("4")) {

        //_____________________________monthly________________________________

        //__________________________monthly reports_________________________

        //deal with subpartnertable and facilid first

        monthint = month;

        if (yearint == 2015) {

            if (monthint == 10 || monthint == 11 || monthint == 12 || monthint == 1 || monthint == 2
                    || monthint == 3) {
                //here use a different subpartner id
                subpartnerid = "SP_ID";
                subpartnera = "subpartnera2014";
            } else {

                subpartnerid = "SubPartnerID";
                subpartnera = "subpartnera";

            }

        } else if (yearint <= 2014) {
            subpartnerid = "SP_ID";
            subpartnera = "subpartnera2014";
        } else if (yearint > 2015) {
            subpartnerid = "SubPartnerID";
            subpartnera = "subpartnera";
        }
        //---------------------------------------------------------------------------------------
        //---------------------------------------------------------------------------------------

        month = Integer.parseInt(request.getParameter("month"));
        //     month=5;
        if (month >= 10) {
            duration = " moh731.yearmonth=" + prevYear + "" + month;
        } else {
            duration = " moh731.yearmonth=" + year + "0" + month;
        }
    } else {
        duration = "";
    }

    //     GET FACILITIES TO OUTPUT.................................
    mflcode = countyName = districtName = facilityName = "";

    if (reportType.equals("1")) {
        facility = "";

        facilityName = "ALL APHIA PLUS SUPPORTED HEALTH FACILITIES";
        districtName = "ALL";
        countyName = "ALL COUNTIES";
        mflcode = "NONE";
    }

    else {
        facilityId = request.getParameter("facility");

        String spid = "";

        //  facilityId="403";
        facility = "moh731.SubPartnerID='" + facilityId + "' &&";

        String getName = "SELECT subpartnera.SubPartnerNom,district.DistrictNom,county.County,subpartnera.CentreSanteId   , SP_ID FROM subpartnera "
                + "JOIN district ON subpartnera.DistrictID=district.DistrictID JOIN county ON "
                + "district.CountyID=county.CountyID WHERE subpartnera.SubPartnerID='" + facilityId + "'";
        conn.rs = conn.st.executeQuery(getName);

        if (conn.rs.next() == true) {
            facilityName = conn.rs.getString(1);
            districtName = conn.rs.getString(2);
            countyName = conn.rs.getString(3);
            mflcode = conn.rs.getString(4);

            spid = conn.rs.getString(5);

        }

        if (subpartnerid.equalsIgnoreCase("SP_ID")) {

            facility = "moh731.SubPartnerID='" + spid + "' &&";

        }

    }

    header += "</table>";

    String getMaxYearMonth = "SELECT MAX(yearmonth) FROM moh731 WHERE " + facility + " " + duration;
    conn.rs = conn.st.executeQuery(getMaxYearMonth);
    if (conn.rs.next() == true) {
        maxYearMonth = conn.rs.getInt(1);
    }
    System.out.println("max year month : " + maxYearMonth);

    //            ^^^^^^^^^^^^^CREATE STATIC AND WRITE STATIC DATA TO THE EXCELL^^^^^^^^^^^^
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet shet4 = wb.createSheet("HTC");
    HSSFSheet shet1 = wb.createSheet("PMTCT");
    HSSFSheet shet2 = wb.createSheet("Care and Treatment");
    HSSFSheet shet3 = wb.createSheet("PEP");

    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short) 18);
    font.setFontName("Cambria");
    font.setColor((short) 0000);
    CellStyle style = wb.createCellStyle();
    style.setFont(font);
    style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
    HSSFFont font2 = wb.createFont();
    font2.setFontName("Arial Black");
    font2.setColor((short) 0000);
    CellStyle style2 = wb.createCellStyle();
    style2.setFont(font2);

    HSSFCellStyle stborder = wb.createCellStyle();
    stborder.setBorderTop(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    stborder.setBorderRight(HSSFCellStyle.BORDER_THIN);
    stborder.setAlignment(HSSFCellStyle.ALIGN_LEFT);
    stborder.setWrapText(true);

    HSSFCellStyle stylex = wb.createCellStyle();
    stylex.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    stylex.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    stylex.setBorderTop(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    stylex.setBorderRight(HSSFCellStyle.BORDER_THIN);
    stylex.setAlignment(HSSFCellStyle.ALIGN_LEFT);

    HSSFFont fontx = wb.createFont();
    fontx.setColor(HSSFColor.DARK_BLUE.index);
    stylex.setFont(fontx);
    stylex.setWrapText(true);

    HSSFCellStyle styleHeader = wb.createCellStyle();
    styleHeader.setFillForegroundColor(HSSFColor.LIME.index);
    styleHeader.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    styleHeader.setBorderTop(HSSFCellStyle.BORDER_THIN);
    styleHeader.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    styleHeader.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    styleHeader.setBorderRight(HSSFCellStyle.BORDER_THIN);
    styleHeader.setAlignment(HSSFCellStyle.ALIGN_LEFT);

    HSSFFont fontHeader = wb.createFont();
    fontHeader.setColor(HSSFColor.DARK_BLUE.index);
    styleHeader.setFont(fontHeader);
    styleHeader.setWrapText(true);

    for (int i = 0; i <= 1; i++) {
        shet1.setColumnWidth(i, 14000);
    }

    for (int i = 0; i <= 1; i++) {
        shet2.setColumnWidth(i, 14000);
    }

    for (int i = 0; i <= 1; i++) {
        shet3.setColumnWidth(i, 14000);
    }
    for (int i = 0; i <= 1; i++) {
        shet4.setColumnWidth(i, 14000);
    }

    shet1.setColumnWidth(2, 4000);
    shet2.setColumnWidth(2, 4000);
    shet3.setColumnWidth(2, 4000);
    shet4.setColumnWidth(2, 4000);

    String headers = "COUNTY,SUB COUNTY,FACILITY NAME,MFL CODE";

    String arrayHeader[] = headers.split(",");
    int headerno = 0;
    int valueNo = 0;
    int arrayCounter = 0;

    //   XSSFRow rw0S1=shet1.createRow(0);
    HSSFRow rw1S1 = shet1.createRow(0);

    //    XSSFRow rw0S2=shet2.createRow(0);
    HSSFRow rw1S2 = shet2.createRow(0);

    //    XSSFRow rw0S3=shet3.createRow(0);
    HSSFRow rw1S3 = shet3.createRow(0);

    HSSFRow rw1S4 = shet4.createRow(0);

    String getMonth = "SELECT name FROM month WHERE id='" + month + "'";
    conn.rs = conn.st.executeQuery(getMonth);
    if (conn.rs.next() == true) {
        monthName = conn.rs.getString(1);
    }

    counterPMTCT = counterART = counterPEP = 3;

    //   START OUTPUTTING THE RESULTS=================================================   

    System.out.println("facility : " + facility + "   duration : " + duration);

    prevSection = currentSection = "";
    secCounter = 0;

    String checker = "SELECT "
            + "SUM(HV0101),SUM(HV0102),SUM(HV0103),SUM(HV0105),SUM(HV0106),SUM(HV0107),SUM(HV0108),SUM(HV0109),SUM(HV0110),SUM(HV0111),SUM(HV0112),SUM(HV0113),SUM(HV0114),"
            + "SUM(HV0115),SUM(HV0116),"
            + "SUM(HV0201),SUM(HV0202),SUM(HV0203),SUM(HV0204),SUM(HV0205),SUM(HV0206),SUM(HV0207),SUM(HV0208),SUM(HV0209),SUM(HV0210),SUM(HV0211),SUM(HV0212),SUM(HV0213),"
            + "SUM(HV0214),SUM(HV0215),SUM(HV0216),SUM(HV0217),SUM(HV0218),SUM(HV0219),SUM(HV0220),SUM(HV0221),SUM(HV0224),SUM(HV0225),SUM(HV0226),SUM(HV0227),SUM(HV0228),SUM(HV0229),"
            + "SUM(HV0230),SUM(HV0231),SUM(HV0232),SUM(HV0233),SUM(HV0234),SUM(HV0235),SUM(HV0236),SUM(HV0237),SUM(HV0238),SUM(HV0239),SUM(HV0240),SUM(HV0241),SUM(HV0242),"
            + "SUM(HV0243),SUM(HV0244),"
            + "SUM(HV0301),SUM(HV0302),SUM(HV0303),SUM(HV0304),SUM(HV0305),SUM(HV0306),SUM(HV0307),SUM(HV0308),SUM(HV0309),SUM(HV0310),SUM(HV0311),SUM(HV0312),SUM(HV0313),SUM(HV0314),"
            + "SUM(HV0315),SUM(HV0316),SUM(HV0317),SUM(HV0318),SUM(HV0319),SUM(HV0320),SUM(HV0321),SUM(HV0322),SUM(HV0323),SUM(HV0324),SUM(HV0325),SUM(HV0326),SUM(HV0327),SUM(HV0328),"
            + "SUM(HV0329),SUM(HV0330),SUM(HV0331),SUM(HV0332),SUM(HV0333),SUM(HV0334),SUM(HV0335),SUM(HV0336),SUM(HV0337),SUM(HV0338),SUM(HV0339),SUM(HV0340),SUM(HV0341),"
            + "SUM(HV0342),SUM(HV0343),SUM(HV0344),SUM(HV0345),SUM(HV0346),SUM(HV0347),SUM(HV0348),SUM(HV0349),SUM(HV0350),SUM(HV0351),SUM(HV0352),SUM(HV0353),"
            + "SUM(HV0354),SUM(HV0355),SUM(HV0904),SUM(HV0905),SUM(HV0370),SUM(HV0371),SUM(HV0372),SUM(HV0373),"
            + "SUM(HV0401),SUM(HV0402),SUM(HV0403),SUM(HV0406),SUM(HV0407),SUM(HV0408),SUM(HV0409),SUM(HV0410),SUM(HV0411),SUM(HV0412),SUM(HV0413),SUM(HV0414),SUM(HV0415),"
            + "SUM(HV0501),SUM(HV0502),SUM(HV0503),SUM(HV0504),SUM(HV0505),SUM(HV0506),SUM(HV0507),SUM(HV0508),SUM(HV0509),SUM(HV0510),SUM(HV0511),SUM(HV0512),SUM(HV0513),SUM(HV0514),"
            + "SUM(HV0101),SUM(HV0102),SUM(HV0103),SUM(HV0105),SUM(HV0106),SUM(HV0107),SUM(HV0108),SUM(HV0109),SUM(HV0110),SUM(HV0111),SUM(HV0112),SUM(HV0113),SUM(HV0114),SUM(HV0115),SUM(HV0116),"
            + //added later
            "SUM(HV0601),SUM(HV0602),SUM(HV0605)," + subpartnera + ".PMTCT, " + subpartnera + ".ART,"
            + subpartnera + ".PEP, " + subpartnera + ".HTC " + "FROM moh731 JOIN " + subpartnera
            + " ON moh731.SubPartnerID=" + subpartnera + "." + subpartnerid + " WHERE " + facility + " "
            + duration;

    System.out.println("@@@  " + checker);
    conn.rs = conn.st.executeQuery(checker);
    if (conn.rs.next() == true) {

        isPMTCT = conn.rs.getInt(subpartnera + ".PMTCT");
        isART = conn.rs.getInt(subpartnera + ".ART");
        isPEP = conn.rs.getInt(subpartnera + ".PEP");
        isHTC = conn.rs.getInt(subpartnera + ".HTC");

        String headerValues = countyName + "," + districtName + "," + facilityName + "," + mflcode;
        String arrayValues[] = headerValues.split(",");
        String headerValue = "";
        pos = 0;
        //   CREATE HEADERS
        for (String headername : arrayHeader) {
            headerValue = arrayValues[arrayCounter];

            HSSFRow rw1S10 = shet1.createRow(pos);
            HSSFRow rw1S20 = shet2.createRow(pos);
            HSSFRow rw1S30 = shet3.createRow(pos);
            HSSFRow rw1S40 = shet4.createRow(pos);

            HSSFCell S1cell = rw1S10.createCell(0);
            S1cell.setCellValue(headername);
            S1cell.setCellStyle(stylex);

            HSSFCell S1cellX = rw1S10.createCell(1);
            S1cellX.setCellValue(headerValue);
            S1cellX.setCellStyle(stylex);

            S1cellX = rw1S10.createCell(2);
            S1cellX.setCellValue("");
            S1cellX.setCellStyle(stylex);

            S1cellX = rw1S10.createCell(3);
            S1cellX.setCellValue("");
            S1cellX.setCellStyle(stylex);

            HSSFCell S2cell = rw1S20.createCell(0);
            S2cell.setCellValue(headername);
            S2cell.setCellStyle(stylex);

            HSSFCell S2cellX = rw1S20.createCell(1);
            S2cellX.setCellValue(headerValue);
            S2cellX.setCellStyle(stylex);

            S2cellX = rw1S20.createCell(2);
            S2cellX.setCellValue("");
            S2cellX.setCellStyle(stylex);

            S2cellX = rw1S20.createCell(3);
            S2cellX.setCellValue("");
            S2cellX.setCellStyle(stylex);

            HSSFCell S3cell = rw1S30.createCell(0);
            S3cell.setCellValue(headername);
            S3cell.setCellStyle(stylex);

            HSSFCell S3cellX = rw1S30.createCell(1);
            S3cellX.setCellValue(headerValue);
            S3cellX.setCellStyle(stylex);

            S3cellX = rw1S30.createCell(2);
            S3cellX.setCellValue("");
            S3cellX.setCellStyle(stylex);

            S3cellX = rw1S30.createCell(3);
            S3cellX.setCellValue("");
            S3cellX.setCellStyle(stylex);

            //_______________________________

            HSSFCell S4cell = rw1S40.createCell(0);
            S4cell.setCellValue(headername);
            S4cell.setCellStyle(stylex);

            HSSFCell S4cellX = rw1S40.createCell(1);
            S4cellX.setCellValue(headerValue);
            S4cellX.setCellStyle(stylex);

            S4cellX = rw1S40.createCell(2);
            S4cellX.setCellValue("");
            S4cellX.setCellStyle(stylex);

            S4cellX = rw1S40.createCell(3);
            S4cellX.setCellValue("");
            S4cellX.setCellStyle(stylex);

            arrayCounter++;
            pos++;
        }

        //     pos+=1;
        //  OUTPUT ELEMENT HEADING

        HSSFRow rw1S10 = shet1.createRow(pos);
        HSSFRow rw1S20 = shet2.createRow(pos);
        HSSFRow rw1S30 = shet3.createRow(pos);
        HSSFRow rw1S40 = shet4.createRow(pos);

        rw1S10.setHeightInPoints(25);
        rw1S20.setHeightInPoints(25);
        rw1S30.setHeightInPoints(25);
        rw1S40.setHeightInPoints(25);

        HSSFCell S1cell = rw1S10.createCell(0);
        S1cell.setCellValue("SUB SECTION");
        S1cell.setCellStyle(styleHeader);

        HSSFCell S1cellX = rw1S10.createCell(1);
        S1cellX.setCellValue("ELEMENT TITLE");
        S1cellX.setCellStyle(styleHeader);

        HSSFCell S1cellX2 = rw1S10.createCell(2);
        S1cellX2.setCellValue("LABEL");
        S1cellX2.setCellStyle(styleHeader);

        S1cellX2 = rw1S10.createCell(3);
        S1cellX2.setCellValue("VALUE");
        S1cellX2.setCellStyle(styleHeader);

        HSSFCell S2cell = rw1S20.createCell(0);
        S2cell.setCellValue("SUB SECTION");
        S2cell.setCellStyle(styleHeader);

        HSSFCell S2cellX = rw1S20.createCell(1);
        S2cellX.setCellValue("ELEMENT TITLE");
        S2cellX.setCellStyle(styleHeader);

        HSSFCell S2cellX2 = rw1S20.createCell(2);
        S2cellX2.setCellValue("LABEL");
        S2cellX2.setCellStyle(styleHeader);

        S2cellX2 = rw1S20.createCell(3);
        S2cellX2.setCellValue("VALUE");
        S2cellX2.setCellStyle(styleHeader);

        HSSFCell S3cell = rw1S30.createCell(0);
        S3cell.setCellValue("SUB SECTION");
        S3cell.setCellStyle(styleHeader);

        HSSFCell S3cellX = rw1S30.createCell(1);
        S3cellX.setCellValue("ELEMENT TITLE");
        S3cellX.setCellStyle(styleHeader);

        HSSFCell S3cellX2 = rw1S30.createCell(2);
        S3cellX2.setCellValue("LABEL");
        S3cellX2.setCellStyle(styleHeader);

        S3cellX2 = rw1S30.createCell(3);
        S3cellX2.setCellValue("VALUE");
        S3cellX2.setCellStyle(styleHeader);

        //--------------------htc
        HSSFCell S4cell = rw1S40.createCell(0);
        S4cell.setCellValue("SUB SECTION");
        S4cell.setCellStyle(styleHeader);

        HSSFCell S4cellX = rw1S40.createCell(1);
        S4cellX.setCellValue("ELEMENT TITLE");
        S4cellX.setCellStyle(styleHeader);

        HSSFCell S4cellX2 = rw1S40.createCell(2);
        S4cellX2.setCellValue("LABEL");
        S4cellX2.setCellStyle(styleHeader);

        S4cellX2 = rw1S40.createCell(3);
        S4cellX2.setCellValue("VALUE");
        S4cellX2.setCellStyle(styleHeader);

        elementCounter = 1;
        valueCounter = 1;
        specialElement = 0;
        String getCummulatives = "SELECT "
                //              + "SUM(HV0301),SUM(HV0302),"
                + "SUM(HV0303),SUM(HV0304),SUM(HV0305),SUM(HV0306),SUM(HV0307),"
                + "SUM(HV0314),SUM(HV0315),SUM(HV0316),SUM(HV0317),SUM(HV0318),SUM(HV0319),"
                + "SUM(HV0328),SUM(HV0329),SUM(HV0330),SUM(HV0331),SUM(HV0332),SUM(HV0333),SUM(HV0334),SUM(HV0335),"
                + "SUM(HV0336),SUM(HV0337),SUM(HV0338),SUM(HV0339),SUM(HV0340),SUM(HV0341),SUM(HV0342),SUM(HV0343),SUM(HV0344), "
                + "SUM(HV0350),SUM(HV0351),SUM(HV0352),SUM(HV0353),SUM(HV0354),SUM(HV0355) "
                + "FROM moh731 join subpartnera on moh731.subpartnerid=subpartnera.subpartnerid WHERE "
                + facility + " art=1 && yearmonth=" + maxYearMonth;
        conn.rs2 = conn.st2.executeQuery(getCummulatives);
        if (conn.rs2.next() == true) {
            System.out.println("entered to get cumulatives : " + maxYearMonth);

            int j = 5;
            int i = 5;
            int k = 5;
            int l = 5;
            String getElements = "SELECT subsection,shortlabel,label FROM pivottable WHERE form='moh731' ORDER BY tableid";
            conn.rs1 = conn.st1.executeQuery(getElements);
            while (conn.rs1.next()) {
                //     subsection,shortlabel,label;  
                subsection = conn.rs1.getString(1);
                shortlabel = conn.rs1.getString(2);
                label = conn.rs1.getString(3);

                elementCounter++;
                valueCounter = elementCounter + 4;
                //pmtct
                if (elementCounter >= 17 && elementCounter <= 58) {
                    if (isPMTCT == 1 && j <= 47) {
                        int valuePos = j + 15 - 4;
                        HSSFRow rw1S11 = shet1.createRow(j);
                        HSSFCell S1cell1 = rw1S11.createCell(0);
                        S1cell1.setCellValue(subsection);
                        S1cell1.setCellStyle(stborder);

                        HSSFCell S1cellX1 = rw1S11.createCell(1);
                        S1cellX1.setCellValue(shortlabel);
                        S1cellX1.setCellStyle(stborder);

                        HSSFCell S1cellX21 = rw1S11.createCell(2);
                        S1cellX21.setCellValue(label);
                        S1cellX21.setCellStyle(stborder);

                        S1cellX21 = rw1S11.createCell(3);
                        S1cellX21.setCellValue(conn.rs.getInt(valuePos));
                        S1cellX21.setCellStyle(stborder);

                        currentSection = subsection;

                        if (prevSection.equals(currentSection) && !prevSection.equals("")) {
                            secCounter++;
                            //    System.out.println("THey are equal  :"+prevSection+" current sec:   "+currentSection);
                        } else if (j == 5) {
                            prevSection = currentSection = subsection;
                            //secCounter++;         
                            //     System.out.println("entered j=5 :"+j+"  :"+prevSection+" current sec:  "+currentSection);
                        }

                        else if (!prevSection.equals(currentSection)) {
                            int startMerger = j - secCounter - 1;
                            int endMerger = j - 1;
                            shet1.addMergedRegion(new CellRangeAddress(startMerger, endMerger, 0, 0));
                            secCounter = 0;
                            //    System.out.println("merged cells from :"+startMerger+" to :"+endMerger);

                        }

                        else {
                            System.out.println("cant think anymore");
                        }
                        prevSection = currentSection;

                        j++;
                        //          System.out.println("j values : "+j);
                        if (j == 47) {
                            System.out.println("entered end here j " + j);
                            int startMerger = j - secCounter - 1;
                            int endMerger = j - 1;
                            shet1.addMergedRegion(new CellRangeAddress(startMerger, endMerger, 0, 0));
                            secCounter = 0;
                            prevSection = currentSection = "";
                        }
                    }

                }
                //art
                if (elementCounter >= 59 && elementCounter <= 119) {
                    if (isART == 1 && i <= 66) {
                        int valuePos = i + 57 - 4;
                        HSSFRow rw1S11 = shet2.createRow(i);
                        HSSFCell S1cell1 = rw1S11.createCell(0);
                        S1cell1.setCellValue(subsection);
                        S1cell1.setCellStyle(stborder);

                        HSSFCell S1cellX1 = rw1S11.createCell(1);
                        S1cellX1.setCellValue(shortlabel);
                        S1cellX1.setCellStyle(stborder);

                        HSSFCell S1cellX21 = rw1S11.createCell(2);
                        S1cellX21.setCellValue(label);
                        S1cellX21.setCellStyle(stborder);

                        if (elementCounter >= 61 && elementCounter <= 65) {
                            specialElement++;
                            //        System.out.println("entered on cumus for ctx>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                            //        System.out.println("Value is >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+conn.rs2.getInt(specialElement));
                            S1cellX21 = rw1S11.createCell(3);
                            S1cellX21.setCellValue(conn.rs2.getInt(specialElement));
                            //    S1cellX21.setCellValue("");
                            S1cellX21.setCellStyle(stborder);

                        }

                        else if (elementCounter >= 72 && elementCounter <= 77) {
                            specialElement++;
                            S1cellX21 = rw1S11.createCell(3);
                            S1cellX21.setCellValue(conn.rs2.getInt(specialElement));
                            S1cellX21.setCellStyle(stborder);

                        }

                        else if (elementCounter >= 86 && elementCounter <= 91) {
                            specialElement++;
                            S1cellX21 = rw1S11.createCell(3);
                            S1cellX21.setCellValue(conn.rs2.getInt(specialElement));
                            S1cellX21.setCellStyle(stborder);

                        }

                        else if (elementCounter >= 92 && elementCounter <= 102) {
                            specialElement++;
                            S1cellX21 = rw1S11.createCell(3);
                            S1cellX21.setCellValue(conn.rs2.getInt(specialElement));
                            S1cellX21.setCellStyle(stborder);
                        } else if (elementCounter >= 108 && elementCounter <= 113) {
                            specialElement++;
                            S1cellX21 = rw1S11.createCell(3);
                            S1cellX21.setCellValue(conn.rs2.getInt(specialElement));
                            S1cellX21.setCellStyle(stborder);
                        } else {
                            S1cellX21 = rw1S11.createCell(3);
                            S1cellX21.setCellValue(conn.rs.getInt(valuePos));
                            S1cellX21.setCellStyle(stborder);
                        }

                        currentSection = subsection;

                        if (prevSection.equals(currentSection) && !prevSection.equals("")) {
                            secCounter++;
                            //    System.out.println("THey are equal  :"+prevSection+" current sec:   "+currentSection);
                        } else if (i == 5) {
                            prevSection = currentSection = subsection;
                            //secCounter++;         
                            //    System.out.println("entered j=5 :"+i+"  :"+prevSection+" current sec:  "+currentSection);
                        }

                        else if (!prevSection.equals(currentSection)) {
                            int startMerger = i - secCounter - 1;
                            int endMerger = i - 1;
                            shet2.addMergedRegion(new CellRangeAddress(startMerger, endMerger, 0, 0));
                            secCounter = 0;
                            //    System.out.println("merged cells from :"+startMerger+" to :"+endMerger);

                        }

                        else {
                            //         System.out.println("cant think anymore");
                        }
                        prevSection = currentSection;

                        i++;

                        if (i == 66) {
                            //       System.out.println("entered end here i "+i);
                            int startMerger = i - secCounter - 1;
                            int endMerger = i - 1;
                            shet2.addMergedRegion(new CellRangeAddress(startMerger, endMerger, 0, 0));
                            secCounter = 0;
                            prevSection = currentSection = "";
                        }

                    }
                }
                //PEP
                if (elementCounter >= 133 && elementCounter <= 146) {
                    if (isPEP == 1 && k <= 18) {
                        int valuePos = k + 131 - 4;
                        System.out.println("k values : " + k);
                        HSSFRow rw1S11 = shet3.createRow(k);
                        HSSFCell S1cell1 = rw1S11.createCell(0);
                        S1cell1.setCellValue(subsection);
                        S1cell1.setCellStyle(stborder);

                        HSSFCell S1cellX1 = rw1S11.createCell(1);
                        S1cellX1.setCellValue(shortlabel);
                        S1cellX1.setCellStyle(stborder);

                        HSSFCell S1cellX21 = rw1S11.createCell(2);
                        S1cellX21.setCellValue(label);
                        S1cellX21.setCellStyle(stborder);

                        S1cellX21 = rw1S11.createCell(3);
                        S1cellX21.setCellValue(conn.rs.getInt(valuePos));
                        S1cellX21.setCellStyle(stborder);

                        currentSection = subsection;

                        if (prevSection.equals(currentSection) && !prevSection.equals("")) {
                            secCounter++;
                        } else if (k == 5) {
                            prevSection = currentSection = subsection;
                        }

                        else if (!prevSection.equals(currentSection)) {
                            int startMerger = k - secCounter - 1;
                            int endMerger = k - 1;
                            if (startMerger == endMerger) {
                            } else {
                                shet3.addMergedRegion(new CellRangeAddress(startMerger, endMerger, 0, 0));
                            }
                            secCounter = 0;
                            System.out.println("merged cells from :" + startMerger + " to :" + endMerger);

                        }

                        else {
                            System.out.println("cant think anymore");
                        }
                        prevSection = currentSection;

                        k++;

                        if (k == 18) {
                            int startMerger = k - secCounter - 1;
                            int endMerger = k;
                            shet3.addMergedRegion(new CellRangeAddress(startMerger, endMerger, 0, 0));
                            secCounter = 0;
                            prevSection = currentSection = "";
                        }

                    }

                }

                //==========================HTC===============================  

                if (elementCounter <= 16) {
                    if (isHTC == 1 && l <= 19) {
                        int valuePos = l - 4;
                        System.out.println("l values : " + l);
                        HSSFRow rw1S11 = shet4.createRow(l);
                        HSSFCell S1cell1 = rw1S11.createCell(0);
                        S1cell1.setCellValue(subsection);
                        S1cell1.setCellStyle(stborder);

                        HSSFCell S1cellX1 = rw1S11.createCell(1);
                        S1cellX1.setCellValue(shortlabel);
                        S1cellX1.setCellStyle(stborder);

                        HSSFCell S1cellX21 = rw1S11.createCell(2);
                        S1cellX21.setCellValue(label);
                        S1cellX21.setCellStyle(stborder);

                        S1cellX21 = rw1S11.createCell(3);
                        S1cellX21.setCellValue(conn.rs.getInt(valuePos));
                        S1cellX21.setCellStyle(stborder);

                        currentSection = subsection;

                        if (prevSection.equals(currentSection) && !prevSection.equals("")) {
                            secCounter++;
                        } else if (l == 5) {
                            prevSection = currentSection = subsection;
                        }

                        else if (!prevSection.equals(currentSection)) {
                            int startMerger = l - secCounter - 1;
                            int endMerger = l - 1;
                            if (startMerger == endMerger) {
                            } else {
                                shet4.addMergedRegion(new CellRangeAddress(startMerger, endMerger, 0, 0));
                            }
                            secCounter = 0;
                            System.out.println("merged cells from :" + startMerger + " to :" + endMerger);

                        }

                        else {
                            System.out.println("cant think anymore");
                        }
                        prevSection = currentSection;

                        l++;

                        if (l == 19) {
                            int startMerger = l - secCounter - 1;
                            int endMerger = l;
                            shet4.addMergedRegion(new CellRangeAddress(startMerger, endMerger, 0, 0));
                            secCounter = 0;
                            prevSection = currentSection = "";
                        }

                    }

                }

            }
        }
        System.out.println("Data already exist loading............................");

        counter++;
    }

    System.out.println("Validity checker : " + isValidated);

    if (conn.st != null) {
        conn.st.close();
    }
    if (conn.st1 != null) {
        conn.st1.close();
    }
    if (conn.st2 != null) {
        conn.st2.close();
    }

    if (conn.rs != null) {
        conn.rs.close();
    }
    if (conn.rs1 != null) {
        conn.rs1.close();
    }
    if (conn.rs2 != null) {
        conn.rs2.close();
    }
    if (conn.conn != null) {
        conn.conn.close();
    }

    IdGenerator IG = new IdGenerator();
    createdOn = IG.CreatedOn();

    // 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=MOH731_STATIC_REPORT_CREATED_" + createdOn.trim() + ".xls");
    OutputStream outStream = response.getOutputStream();
    outStream.write(outArray);
    outStream.flush();

}