Example usage for org.apache.poi.ss.usermodel Row createCell

List of usage examples for org.apache.poi.ss.usermodel Row createCell

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Row createCell.

Prototype

Cell createCell(int column);

Source Link

Document

Use this to create new cells within the row and return it.

Usage

From source file:co.turnus.profiling.io.XlsHalsteadAnalysisWriter.java

License:Open Source License

private void writeSummary(HSSFWorkbook workbook, Network network, SourceCodeData report) {
    BasicStaticData networkData = ProfilingFactory.eINSTANCE.createBasicStaticData();

    for (ActorClassStaticData aData : report.getActorsData()) {
        StaticDataUtil.merge(networkData, aData);
    }/*from w  w w.  j  a v a2 s .  c  o m*/

    HalsteadAnalyser a = new HalsteadAnalyser(networkData);
    HSSFSheet sheet = workbook.createSheet("Halstead Summary");
    // Action Actor Class Results
    Cell cell = sheet.createRow(0).createCell(0);
    HSSFRichTextString title = new HSSFRichTextString("Halstead Analysis: Network and Actor Classes");
    title.applyFont(titleFont);
    cell.setCellValue(title);

    Row row = sheet.createRow(1);
    row.createCell(0).setCellValue("Network");
    row = sheet.createRow(2);
    row.createCell(0).setCellValue("n1");
    row.createCell(1).setCellValue("n2");
    row.createCell(2).setCellValue("n");
    row.createCell(3).setCellValue("N1");
    row.createCell(4).setCellValue("N2");
    row.createCell(5).setCellValue("N");
    row.createCell(6).setCellValue("cN");
    row.createCell(7).setCellValue("V");
    row.createCell(8).setCellValue("D");
    row.createCell(9).setCellValue("E");
    row.createCell(10).setCellValue("T");
    row.createCell(11).setCellValue("B");
    row.createCell(12).setCellValue("I");
    row.createCell(13).setCellValue("NoL");
    row = sheet.createRow(3);
    row.createCell(0).setCellValue(a.getn1());
    row.createCell(1).setCellValue(a.getn2());
    row.createCell(2).setCellValue(a.getn());
    row.createCell(3).setCellValue(a.getN1());
    row.createCell(4).setCellValue(a.getN2());
    row.createCell(5).setCellValue(a.getN());
    row.createCell(6).setCellValue(a.getcN());
    row.createCell(7).setCellValue(a.getV());
    row.createCell(8).setCellValue(a.getD());
    row.createCell(9).setCellValue(a.getE());
    row.createCell(10).setCellValue(a.getT());
    row.createCell(11).setCellValue(a.getB());
    row.createCell(12).setCellValue(a.getI());
    row.createCell(13).setCellValue(networkData.getNumberOfLines());

    row = sheet.createRow(5);
    row.createCell(0).setCellValue("Actor Class");
    row.createCell(1).setCellValue("n1");
    row.createCell(2).setCellValue("n2");
    row.createCell(3).setCellValue("n");
    row.createCell(4).setCellValue("N1");
    row.createCell(5).setCellValue("N2");
    row.createCell(6).setCellValue("N");
    row.createCell(7).setCellValue("cN");
    row.createCell(8).setCellValue("V");
    row.createCell(9).setCellValue("D");
    row.createCell(10).setCellValue("E");
    row.createCell(11).setCellValue("T");
    row.createCell(12).setCellValue("B");
    row.createCell(13).setCellValue("I");
    row.createCell(14).setCellValue("NoL");

    int i = 6;
    for (ActorClassStaticData aData : report.getActorsData()) {
        a = new HalsteadAnalyser(aData);
        row = sheet.createRow(i);
        row.createCell(0).setCellValue(aData.getActorClass().getName());
        row.createCell(1).setCellValue(a.getn1());
        row.createCell(2).setCellValue(a.getn2());
        row.createCell(3).setCellValue(a.getn());
        row.createCell(4).setCellValue(a.getN1());
        row.createCell(5).setCellValue(a.getN2());
        row.createCell(6).setCellValue(a.getN());
        row.createCell(7).setCellValue(a.getcN());
        row.createCell(8).setCellValue(a.getV());
        row.createCell(9).setCellValue(a.getD());
        row.createCell(10).setCellValue(a.getE());
        row.createCell(11).setCellValue(a.getT());
        row.createCell(12).setCellValue(a.getB());
        row.createCell(13).setCellValue(a.getI());
        row.createCell(14).setCellValue(aData.getNumberOfLines());

        i++;
    }
}

From source file:co.turnus.profiling.io.XlsProfilingDataWriter.java

License:Open Source License

private void writeFifosData(HSSFWorkbook workbook, ProfilingData data) {
    HSSFSheet sheet = workbook.createSheet("Fifos Data");
    // Action Actor Class Results
    Cell cell = sheet.createRow(0).createCell(0);
    HSSFRichTextString title = new HSSFRichTextString("Communication data summary");
    title.applyFont(titleFont);/*from w  w  w  .j  av a2  s  .  c om*/
    cell.setCellValue(title);

    int rowIdx = 2;
    Row row = sheet.createRow(rowIdx++);
    sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 1));
    row.createCell(0).setCellValue("Source");
    sheet.addMergedRegion(new CellRangeAddress(2, 2, 2, 3));
    row.createCell(2).setCellValue("Target");
    sheet.addMergedRegion(new CellRangeAddress(2, 2, 4, 5));
    row.createCell(4).setCellValue("Type");
    sheet.addMergedRegion(new CellRangeAddress(2, 2, 6, 12));
    row.createCell(6).setCellValue("Writing (tokens)");
    sheet.addMergedRegion(new CellRangeAddress(2, 2, 13, 20));
    row.createCell(13).setCellValue("Reading (tokens)");

    // header
    row = sheet.createRow(rowIdx++);
    row.createCell(0).setCellValue("Actor");
    row.createCell(1).setCellValue("Out Port");
    row.createCell(2).setCellValue("Actor");
    row.createCell(3).setCellValue("In Port");

    // type
    row.createCell(4).setCellValue("Name");
    row.createCell(5).setCellValue("Bit:Token");

    // writing
    row.createCell(6).setCellValue("hits");
    row.createCell(7).setCellValue("misses");
    row.createCell(8).setCellValue("total");
    row.createCell(9).setCellValue("average");
    row.createCell(10).setCellValue("min");
    row.createCell(11).setCellValue("max");
    row.createCell(12).setCellValue("variance");

    // reading
    row.createCell(13).setCellValue("peeks");
    row.createCell(14).setCellValue("hits");
    row.createCell(15).setCellValue("misses");
    row.createCell(16).setCellValue("total");
    row.createCell(17).setCellValue("average");
    row.createCell(18).setCellValue("min");
    row.createCell(19).setCellValue("max");
    row.createCell(20).setCellValue("variance");

    for (FifoProfilingData fData : data.getFifosData()) {
        row = sheet.createRow(rowIdx++);
        Fifo fifo = fData.getFifo();
        // header
        row.createCell(0).setCellValue(fifo.getSourceActor().getId());
        row.createCell(1).setCellValue(fifo.getSourcePort().getName());
        row.createCell(2).setCellValue(fifo.getTargetActor().getId());
        row.createCell(3).setCellValue(fifo.getTargetPort().getName());
        // type
        row.createCell(4).setCellValue(fifo.getType().toString());
        row.createCell(5).setCellValue(fifo.getType().getBits());
        // writes
        row.createCell(6).setCellValue(fData.getWriteHit());
        row.createCell(7).setCellValue(fData.getWriteMiss());
        row.createCell(8).setCellValue(fData.getWriteTokens().getSum());
        row.createCell(9).setCellValue(fData.getWriteTokens().getMean());
        row.createCell(10).setCellValue(fData.getWriteTokens().getMin());
        row.createCell(11).setCellValue(fData.getWriteTokens().getMax());
        row.createCell(12).setCellValue(fData.getWriteTokens().getVariance());
        // reads
        row.createCell(13).setCellValue(fData.getPeeks());
        row.createCell(14).setCellValue(fData.getReadHit());
        row.createCell(15).setCellValue(fData.getWriteMiss());
        row.createCell(16).setCellValue(fData.getReadTokens().getSum());
        row.createCell(17).setCellValue(fData.getWriteTokens().getMean());
        row.createCell(18).setCellValue(fData.getWriteTokens().getMin());
        row.createCell(19).setCellValue(fData.getWriteTokens().getMax());
        row.createCell(20).setCellValue(fData.getWriteTokens().getVariance());

    }

}

From source file:code.excelreport.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try {//www . ja v  a  2 s. c o  m
        /* TODO output your page here. You may use following sample code. */

        String allpath = getServletContext().getRealPath("/MOIS.xlsm");

        XSSFWorkbook wb1;

        String pathtodelete = null;

        Date da = new Date();
        String dat2 = da.toString().replace(" ", "_");

        dat2 = dat2.toString().replace(":", "_");

        String mydrive = allpath.substring(0, 1);

        String np = mydrive + ":\\APHIAPLUS\\MOIS\\MACROS\\MOIS_REPORT" + dat2 + ".xlsm";
        //check if file exists
        String sourcepath = getServletContext().getRealPath("/MOIS.xlsm");

        File f = new File(np);
        if (!f.exists() && !f.isFile()) {
            /* do something */

            copytemplates ct = new copytemplates();
            System.out.println("Copying macros..");
            ct.transfermacros(sourcepath, np);

        } else
        //copy the file alone  
        {

            copytemplates ct = new copytemplates();
            //copy the agebased file only
            ct.copymacros(sourcepath, np);

        }

        File allpathfile = new File(np);

        OPCPackage pkg = OPCPackage.open(allpathfile);

        pathtodelete = np;

        //wb = new XSSFWorkbook( OPCPackage.open(allpath) );
        wb1 = new XSSFWorkbook(pkg);

        SXSSFWorkbook wb = new SXSSFWorkbook(wb1, 100);

        Sheet rawdata = wb.getSheet("Facility Report");
        String startdate = "2016-10-01";

        String enddate = "2016-10-30";
        String county = "";

        if (request.getParameter("startdate") != null) {
            startdate = request.getParameter("startdate");
        }
        if (request.getParameter("county") != null) {
            if (!request.getParameter("county").equals("")) {

                county = request.getParameter("county");

            }
        }

        if (request.getParameter("enddate") != null) {
            enddate = request.getParameter("enddate");
        }
        String countywhere = " and 1=1";
        if (!county.equals("")) {
            countywhere = "  and ( county like '" + county + "' ) ";
        }

        DateTime dateTime1 = new DateTime(startdate);
        DateTime dateTime2 = new DateTime(enddate);

        int weeks = Weeks.weeksBetween(dateTime1, dateTime2).getWeeks();
        System.out.println("___Weeks____" + weeks);

        if (weeks == 0) {
            weeks = 1;
        }
        //String header[]="county,subcounty,facilityname,startdate,enddate,hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total,hiv_pos_child,hiv_pos_adult,hiv_pos_total,new_care_child,new_care_adult,new_care_total,new_art_target_child,new_art_target_adult,new_art_target_total,started_art_child,started_art_adult,started_art_total,viral_load_target_child,viral_load_target_adult,viral_load_target_total,viral_load_done_child,viral_load_done_adult,viral_load_done_total,ipt_target_child,ipt_target_adult,ipt_target_total,ipt_child,ipt_adult,ipt_total,testing_target_child,testing_target_adult,testing_target_total,test_child,test_adult,test_total,pmtct_hiv_pos_target,pmtct_hiv_pos,eid_target,eid_done,viral_load_mothers_target,viral_load_mothers_done,hiv_pos_yield_perc_child,hiv_pos_yield_perc_adult,hiv_pos_yield_perc_all,hiv_pos_care_perc_child,hiv_pos_care_perc_adult,hiv_pos_care_perc_all,started_art_perc_child,started_art_perc_adult,started_art_perc_all,viral_test_perc_child,viral_test_perc_adult,viral_test_perc_all,ipt_done_perc_child,ipt_done_perc_adult,ipt_done_perc_all,tested_perc_child,tested_perc_adult,tested_perc_all,viral_load_mothers_perc,eid_done_perc,pmtct_hiv_pos_perc,hiv_pos_yield_cmts,hiv_pos_care_cmts,started_art_cmts,viral_test_cmts,ipt_done_cmts,tested_cmts,viral_load_mothers_cmts,eid_done_cmts,pmtct_hiv_pos_cmts".split(",");
        //String headername[]="COUNTY,SUB-COUNTY,FACILITY,START DATE,END DATE,HIV POSITIVE TARGET CHILDREN,HIV POSITIVE TARGET ADULT,HIV POSITIVE TARGET TOTAL,HIV POSITIVE CHILDREN,HIV POSITIVE ADULT,HIV POSITIVE TOTAL, NEW CARE CHILDREN,NEW CARE ADULT,NEW CARE TOTAL,NEW ART TARGET CHILDREN,NEW ART TARGET ADULT,NEW ART TARGET TOTAL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART TOTAL,VIRAL LOAD TARGET CHILDREN,VIRAL LOAD TARGET ADULT,VIRAL LOAD TARGET TOTAL,VIRAL LOAD DONE CHILDREN,VIRAL LOAD DONE ADULT,VIRAL LOAD DONE TOTAL,IPT TARGET CHILDREN,IPT TARGET ADULT,IPT TARGET TOTAL,IPT CHILDREN,IPT ADULT,IPT TOTAL,TESTING TARGET CHILDREN,TESTING TARGET ADULT,TESTING TARGET TOTAL,TESTING CHILDREN,TESTING ADULT,TESTING TOTAL,PMTCT HIV POSITIVE TARGET,PMTCT HIV POSITIVE ,EID TARGET,EID DONE,VIRAL LOAD MOTHERS TARGET,VIRAL LOAD MOTHERS DONE,HIV POSITIVE YIELD CHILDREN ,HIV POSITIVE YIELD ADULT,HIV POSITIVE YIELD ALL,HIV POSITIVE CARE CHILDREN,HIV POSITIVE CARE ADULT,HIV POSITIVE CARE ALL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART ,VIRAL TEST CHILDREN,VIRAL TEST ADULT,VIRAL TEST ALL,IPT DONE CHILD,IPT DONE ADULT,IPT DONE ALL,TESTED CHILD,TESTED ADULT,TESTED ALL,VIRAL LOAD MOTHERS ,EID DONE,PMTCT HIV POSITIVE,HIV POSITIVE YIELD COMMENTS,HIV POSITIVE CARE COMMENTS,STARTED ART COMMENTS,VIRAL TEST COMMENTS,IPT DONE COMMENTS ,TESTED COMMENTS,VIRAL LOAD MOTHERS COMMENTS,EID DONE COMMENTS,PMTCT HIV POSITIVE COMMENTS,REPORTING RATE".split(",");

        String header[] = "county,subcounty,facilityname,startdate,enddate,testing_target_child,testing_target_adult,testing_target_total,test_child,test_adult,test_total,tested_perc_child,tested_perc_adult,tested_perc_all,tested_cmts,hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total,hiv_pos_child,hiv_pos_adult,hiv_pos_total,hiv_pos_yield_perc_child,hiv_pos_yield_perc_adult,hiv_pos_yield_perc_all,hiv_pos_yield_cmts,new_care_child,new_care_adult,new_care_total,hiv_pos_care_perc_child,hiv_pos_care_perc_adult,hiv_pos_care_perc_all,hiv_pos_care_cmts,new_art_target_child,new_art_target_adult,new_art_target_total,started_art_child,started_art_adult,started_art_total,started_art_perc_child,started_art_perc_adult,started_art_perc_all,started_art_cmts,viral_load_target_child,viral_load_target_adult,viral_load_target_total,viral_load_done_child,viral_load_done_adult,viral_load_done_total,viral_test_perc_child,viral_test_perc_adult,viral_test_perc_all,viral_test_cmts,ipt_target_child,ipt_target_adult,ipt_target_total,ipt_child,ipt_adult,ipt_total,ipt_done_perc_child,ipt_done_perc_adult,ipt_done_perc_all,ipt_done_cmts,pmtct_hiv_pos_target,pmtct_hiv_pos,pmtct_hiv_pos_perc,pmtct_hiv_pos_cmts,eid_target,eid_done,eid_done_perc,eid_done_cmts,viral_load_mothers_target,viral_load_mothers_done,viral_load_mothers_perc,viral_load_mothers_cmts"
                .split(",");
        String headername[] = "COUNTY,SUB-COUNTY,FACILITY,START DATE,END DATE,TESTING TARGET CHILDREN,TESTING TARGET ADULT,TESTING TARGET TOTAL,TESTING CHILDREN,TESTING ADULT,TESTING TOTAL,PERCENTAGE TESTED AGAINST TARGET CHILDREN,PERCENTAGE TESTED AGAINST TARGET ADULT,PERCENTAGE TESTED AGAINST TARGET ALL,PERCENTAGE TESTED AGAINST TARGET COMMENTS,HIV POSITIVE TARGET CHILDREN,HIV POSITIVE TARGET ADULT,HIV POSITIVE TARGET TOTAL,HIV POSITIVE CHILDREN,HIV POSITIVE ADULT,HIV POSITIVE TOTAL,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED CHILDREN,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED ADULT,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED ALL,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED COMMENTS, NEW CARE CHILDREN,NEW CARE ADULT,NEW CARE TOTAL,PERCENTAGE HIV POSITIVE ENROLLED ON CARE CHILDREN,PERCENTAGE HIV POSITIVE ENROLLED ON CARE ADULT,PERCENTAGE HIV POSITIVE ENROLLED ON CARE ALL,PERCENTAGE HIV POSITIVE ENROLLED ON CARE COMMENTS,NEW ART TARGET CHILDREN,NEW ART TARGET ADULT,NEW ART TARGET TOTAL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART TOTAL,PERCENTAGE OF TARGET STARTED ON ART CHILDREN,PERCENTAGE OF TARGET STARTED ON ART ADULT,PERCENTAGE OF TARGET STARTED ON ART ALL,PERCENTAGE OF TARGET STARTED ON ART COMMENTS,VIRAL LOAD TARGET CHILDREN,VIRAL LOAD TARGET ADULT,VIRAL LOAD TARGET TOTAL,VIRAL LOAD DONE CHILDREN,VIRAL LOAD DONE ADULT,VIRAL LOAD DONE TOTAL,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET CHILDREN,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET ADULT,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET ALL,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET COMMENTS,IPT TARGET CHILDREN,IPT TARGET ADULT,IPT TARGET TOTAL,IPT CHILDREN,IPT ADULT,IPT TOTAL,PERCENTAGE OF IPT DONE AGAINST TARGET CHILDREN,PERCENTAGE OF IPT DONE AGAINST TARGET ADULT,PERCENTAGE OF IPT DONE AGAINST TARGET ALL,PERCENTAGE OF IPT DONE AGAINST TARGET COMMENTS,PMTCT HIV POSITIVE TARGET,PMTCT HIV POSITIVE ,PERCENTAGE PMTCT HIV POSITIVE YIELD ACHIEVED AGAINST TARGET ALL,PERCENTAGE PMTCT HIV POSITIVE YIELD ACHIEVED AGAINST TARGET COMMENTS,EID TARGET,EID DONE,PERCENTAGE EID DONE AGAINST TARGET ALL,PERCENTAGE EID DONE AGAINST TARGET COMMENTS,VIRAL LOAD MOTHERS TARGET,VIRAL LOAD MOTHERS DONE,PERCENTAGE VIRAL LOAD TESTS DONE FOR MOTHERS AGAINST TARGET ALL,PERCENTAGE VIRAL LOAD TESTS DONE FOR MOTHERS AGAINST TARGET COMMENTS,REPORTING RATE"
                .split(",");
        //
        //
        Row rw0 = rawdata.createRow(0);
        rw0.setHeightInPoints(25);

        for (int d = 0; d < headername.length; d++) {
            Cell ce = rw0.createCell(d);
            ce.setCellValue(headername[d]);

        }

        dbConnweb conn = new dbConnweb();

        conn.st.executeUpdate("SET GLOBAL max_allowed_packet = 209715200");
        conn.rs = conn.st.executeQuery("SHOW VARIABLES LIKE 'max_allowed_packet' ");
        if (conn.rs.next()) {
            System.out.println("Generating report | Max_allowed_connection_" + conn.rs.getString(2));

        }

        String where = " (enddate between '" + startdate + "' and '" + enddate + "')  " + countywhere
                + " and (id not like '%_weekly%' )  ";
        String where1 = " (enddate between '" + startdate + "' and '" + enddate
                + "')  and (id not like '%_weekly%' ) ";

        //get data in report form and add into the various json macros.
        //String query="select facilityname,startdate,enddate, hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total ,hiv_pos_child as hiv_pos_child , hiv_pos_adult as hiv_pos_adult  ,hiv_pos_total as hiv_pos_total  ,new_care_child as new_care_child ,new_care_adult as new_care_adult ,new_care_total as new_care_total  , new_art_target_child  ,new_art_target_adult  ,new_art_target_total  ,started_art_child as started_art_child, started_art_adult as started_art_adult ,started_art_total as started_art_total ,viral_load_target_child  ,viral_load_target_adult  ,viral_load_target_total  ,viral_load_done_child as viral_load_done_child ,viral_load_done_adult as viral_load_done_adult ,viral_load_done_total as viral_load_done_total  ,ipt_target_child  ,ipt_target_adult  ,ipt_target_total  ,ipt_child as ipt_child ,ipt_adult as ipt_adult ,ipt_total as ipt_total ,testing_target_child  ,testing_target_adult  ,testing_target_total  ,test_child as test_child ,test_adult as test_adult   ,test_total as test_total , pmtct_hiv_pos_target,pmtct_hiv_pos as pmtct_hiv_pos, eid_target  , eid_done as eid_done, viral_load_mothers_target, viral_load_mothers_done as viral_load_mothers_done  from weekly_data_new join facility on weekly_data_new.facilityname=facility.facility_name ";

        String query = " select facility.county,facility.subcounty,cur.facilityname,cur.startdate,cur.enddate,cur.testing_target_child,cur.testing_target_adult,cur.testing_target_total,cur.test_child,cur.test_adult,cur.test_total,cur.tested_perc_child,cur.tested_perc_adult,cur.tested_perc_all,cur.tested_cmts,cur.hiv_pos_target_child,cur.hiv_pos_target_adult,cur.hiv_pos_target_total,cur.hiv_pos_child,cur.hiv_pos_adult,cur.hiv_pos_total,cur.hiv_pos_yield_perc_child,cur.hiv_pos_yield_perc_adult,cur.hiv_pos_yield_perc_all,cur.hiv_pos_yield_cmts,cur.new_care_child,cur.new_care_adult,cur.new_care_total,cur.hiv_pos_care_perc_child,cur.hiv_pos_care_perc_adult,cur.hiv_pos_care_perc_all,cur.hiv_pos_care_cmts,cur.new_art_target_child,cur.new_art_target_adult,cur.new_art_target_total,cur.started_art_child,cur.started_art_adult,cur.started_art_total,cur.started_art_perc_child,cur.started_art_perc_adult,cur.started_art_perc_all,cur.started_art_cmts,cur.viral_load_target_child,cur.viral_load_target_adult,cur.viral_load_target_total,cur.viral_load_done_child,cur.viral_load_done_adult,cur.viral_load_done_total,cur.viral_test_perc_child,cur.viral_test_perc_adult,cur.viral_test_perc_all,cur.viral_test_cmts,cur.ipt_target_child,cur.ipt_target_adult,cur.ipt_target_total,cur.ipt_child,cur.ipt_adult,cur.ipt_total,cur.ipt_done_perc_child,cur.ipt_done_perc_adult,cur.ipt_done_perc_all,cur.ipt_done_cmts,cur.pmtct_hiv_pos_target,cur.pmtct_hiv_pos,cur.pmtct_hiv_pos_perc,cur.pmtct_hiv_pos_cmts,cur.eid_target,cur.eid_done,cur.eid_done_perc,cur.eid_done_cmts,cur.viral_load_mothers_target,cur.viral_load_mothers_done,cur.viral_load_mothers_perc,cur.viral_load_mothers_cmts  from weekly_data_new cur join facility on cur.facilityname=facility.facility_name where  "
                + where + " "
                + " and not exists ( select * from weekly_data_new high join facility on high.facilityname=facility.facility_name  where high.facilityname = cur.facilityname and high.enddate > cur.enddate and "
                + where + ")";

        System.out.println("" + query);
        try {

            conn.rs = conn.st.executeQuery(query);
            String facilname = "";
            String facilnamecopy = "";

            String id = "";

            //percentages

            String viral_load_mothers_perc = null;
            String eid_done_perc = null;
            String pmtct_hiv_pos_perc = null;

            //new percenatages
            String hiv_pos_yield_perc_child = null;
            String hiv_pos_yield_perc_adult = null;
            String hiv_pos_yield_perc_all = null;
            String hiv_pos_care_perc_child = null;
            String hiv_pos_care_perc_adult = null;
            String hiv_pos_care_perc_all = null;
            String started_art_perc_child = null;
            String started_art_perc_adult = null;
            String started_art_perc_all = null;
            String viral_test_perc_child = null;
            String viral_test_perc_adult = null;
            String viral_test_perc_all = null;
            String ipt_done_perc_child = null;
            String ipt_done_perc_adult = null;
            String ipt_done_perc_all = null;
            String tested_perc_child = null;
            String tested_perc_adult = null;
            String tested_perc_all = null;
            //comments
            String viral_load_mothers_cmts = null;
            String eid_done_cmts = null;
            String pmtct_hiv_pos_cmts = null;
            String hiv_pos_yield_cmts = null;
            String hiv_pos_care_cmts = null;
            String started_art_cmts = null;
            String viral_test_cmts = null;
            String ipt_done_cmts = null;
            String tested_cmts = null;

            Row rwx = null;

            int rowno = 0;

            while (conn.rs.next()) {

                rowno++;

                rwx = rawdata.createRow(rowno);
                rwx.setHeightInPoints(23);

                //do a per facility 

                //System.out.println("___FACILITY : "+conn.rs.getString("facilityname")+" => hiv_pos_total "+conn.rs.getString("hiv_pos_total"));

                for (int a = 1; a <= header.length; a++) {

                    System.out.print(header[a - 1] + " _ " + conn.rs.getString(a));
                    Cell ce = rwx.createCell(a - 1);

                    if (a > 5 && a <= 73) {
                        if (header[a - 1].contains("cmts")) {
                            ce.setCellValue(conn.rs.getString(a));
                        } else if (header[a - 1].contains("_perc")) {
                            ce.setCellValue(conn.rs.getInt(a));
                            System.out.println("__Percent");
                        } else {
                            ce.setCellValue(conn.rs.getInt(a));
                        }
                    } else {
                        ce.setCellValue(conn.rs.getString(a));
                    }

                }
                //reporting rate
                int reportingrate = 0;

                String getrates = "select ROUND(((count(facilityname))/(" + weeks
                        + ")*100)) as count from weekly_data_new where facilityname like '"
                        + conn.rs.getString("facilityname") + "' and " + where1 + " ";

                conn.rs1 = conn.st1.executeQuery(getrates);

                if (conn.rs1.next()) {

                    reportingrate = conn.rs1.getInt(1);
                }

                Cell ce = rwx.createCell(header.length);
                ce.setCellValue(reportingrate);

            }

            if (conn.rs != null) {
                conn.rs.close();
            }
            if (conn.rs1 != null) {
                conn.rs1.close();
            }
            if (conn.st != null) {
                conn.st.close();
            }
            if (conn.st1 != null) {
                conn.st1.close();
            }
            if (conn.conne != null) {
                conn.conne.close();
            }

            Date dat = new Date();

            String dat1 = dat.toString().replace(" ", "_");

            // 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=MOIS_Cum_Report_From"
                    + startdate.replace(" ", "-") + "_To_" + enddate.replace(" ", "_") + ".xlsm");
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            OutputStream outStream = response.getOutputStream();
            outStream.write(outArray);
            outStream.flush();
            pkg.close();
            wb.dispose();
            // response.sendRedirect("index.jsp");

            File file = new File(pathtodelete);

            if (file.delete()) {
                System.out.println(file.getName() + " is deleted!");
            } else {
                System.out.println("Delete operation is failed.");
            }

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

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

}

From source file:code.excelreport_cumulative.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try {//from w w w. j ava 2 s . c om
        /* TODO output your page here. You may use following sample code. */

        String allpath = getServletContext().getRealPath("/MOIS.xlsm");

        XSSFWorkbook wb1;

        String pathtodelete = null;

        Date da = new Date();
        String dat2 = da.toString().replace(" ", "_");

        dat2 = dat2.toString().replace(":", "_");

        String mydrive = allpath.substring(0, 1);

        String np = mydrive + ":\\APHIAPLUS\\MOIS\\MACROS\\MOIS_REPORT" + dat2 + ".xlsm";
        //check if file exists
        String sourcepath = getServletContext().getRealPath("/MOIS.xlsm");

        File f = new File(np);
        if (!f.exists() && !f.isFile()) {
            /* do something */

            copytemplates ct = new copytemplates();
            System.out.println("Copying macros..");
            ct.transfermacros(sourcepath, np);

        } else
        //copy the file alone  
        {

            copytemplates ct = new copytemplates();
            //copy the agebased file only
            ct.copymacros(sourcepath, np);

        }

        File allpathfile = new File(np);

        OPCPackage pkg = OPCPackage.open(allpathfile);

        pathtodelete = np;

        //wb = new XSSFWorkbook( OPCPackage.open(allpath) );
        wb1 = new XSSFWorkbook(pkg);

        SXSSFWorkbook wb = new SXSSFWorkbook(wb1, 100);

        Sheet rawdata = wb.getSheet("Facility Report");
        String startdate = "2016-10-01";

        String enddate = "2016-10-30";
        String county = "";

        if (request.getParameter("startdate") != null) {
            startdate = request.getParameter("startdate");
        }
        if (request.getParameter("county") != null) {
            if (!request.getParameter("county").equals("")) {

                county = request.getParameter("county");

            }
        }

        if (request.getParameter("enddate") != null) {
            enddate = request.getParameter("enddate");
        }
        String countywhere = " and 1=1";
        if (!county.equals("")) {
            countywhere = "  and ( county like '" + county + "' )";
        }

        DateTime dateTime1 = new DateTime(startdate);
        DateTime dateTime2 = new DateTime(enddate);

        int weeks = Weeks.weeksBetween(dateTime1, dateTime2).getWeeks();
        System.out.println("___Weeks____" + weeks);

        if (weeks == 0) {
            weeks = 1;
        }
        //String header[]="county,subcounty,facilityname,startdate,enddate,hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total,hiv_pos_child,hiv_pos_adult,hiv_pos_total,new_care_child,new_care_adult,new_care_total,new_art_target_child,new_art_target_adult,new_art_target_total,started_art_child,started_art_adult,started_art_total,viral_load_target_child,viral_load_target_adult,viral_load_target_total,viral_load_done_child,viral_load_done_adult,viral_load_done_total,ipt_target_child,ipt_target_adult,ipt_target_total,ipt_child,ipt_adult,ipt_total,testing_target_child,testing_target_adult,testing_target_total,test_child,test_adult,test_total,pmtct_hiv_pos_target,pmtct_hiv_pos,eid_target,eid_done,viral_load_mothers_target,viral_load_mothers_done,hiv_pos_yield_perc_child,hiv_pos_yield_perc_adult,hiv_pos_yield_perc_all,hiv_pos_care_perc_child,hiv_pos_care_perc_adult,hiv_pos_care_perc_all,started_art_perc_child,started_art_perc_adult,started_art_perc_all,viral_test_perc_child,viral_test_perc_adult,viral_test_perc_all,ipt_done_perc_child,ipt_done_perc_adult,ipt_done_perc_all,tested_perc_child,tested_perc_adult,tested_perc_all,viral_load_mothers_perc,eid_done_perc,pmtct_hiv_pos_perc,hiv_pos_yield_cmts,hiv_pos_care_cmts,started_art_cmts,viral_test_cmts,ipt_done_cmts,tested_cmts,viral_load_mothers_cmts,eid_done_cmts,pmtct_hiv_pos_cmts".split(",");
        //String headername[]="COUNTY,SUB-COUNTY,FACILITY,START DATE,END DATE,HIV POSITIVE TARGET CHILDREN,HIV POSITIVE TARGET ADULT,HIV POSITIVE TARGET TOTAL,HIV POSITIVE CHILDREN,HIV POSITIVE ADULT,HIV POSITIVE TOTAL, NEW CARE CHILDREN,NEW CARE ADULT,NEW CARE TOTAL,NEW ART TARGET CHILDREN,NEW ART TARGET ADULT,NEW ART TARGET TOTAL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART TOTAL,VIRAL LOAD TARGET CHILDREN,VIRAL LOAD TARGET ADULT,VIRAL LOAD TARGET TOTAL,VIRAL LOAD DONE CHILDREN,VIRAL LOAD DONE ADULT,VIRAL LOAD DONE TOTAL,IPT TARGET CHILDREN,IPT TARGET ADULT,IPT TARGET TOTAL,IPT CHILDREN,IPT ADULT,IPT TOTAL,TESTING TARGET CHILDREN,TESTING TARGET ADULT,TESTING TARGET TOTAL,TESTING CHILDREN,TESTING ADULT,TESTING TOTAL,PMTCT HIV POSITIVE TARGET,PMTCT HIV POSITIVE ,EID TARGET,EID DONE,VIRAL LOAD MOTHERS TARGET,VIRAL LOAD MOTHERS DONE,HIV POSITIVE YIELD CHILDREN ,HIV POSITIVE YIELD ADULT,HIV POSITIVE YIELD ALL,HIV POSITIVE CARE CHILDREN,HIV POSITIVE CARE ADULT,HIV POSITIVE CARE ALL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART ,VIRAL TEST CHILDREN,VIRAL TEST ADULT,VIRAL TEST ALL,IPT DONE CHILD,IPT DONE ADULT,IPT DONE ALL,TESTED CHILD,TESTED ADULT,TESTED ALL,VIRAL LOAD MOTHERS ,EID DONE,PMTCT HIV POSITIVE,HIV POSITIVE YIELD COMMENTS,HIV POSITIVE CARE COMMENTS,STARTED ART COMMENTS,VIRAL TEST COMMENTS,IPT DONE COMMENTS ,TESTED COMMENTS,VIRAL LOAD MOTHERS COMMENTS,EID DONE COMMENTS,PMTCT HIV POSITIVE COMMENTS,REPORTING RATE".split(",");

        String header[] = "county,subcounty,facilityname,startdate,enddate,testing_target_child,testing_target_adult,testing_target_total,test_child,test_adult,test_total,tested_perc_child,tested_perc_adult,tested_perc_all,tested_cmts,hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total,hiv_pos_child,hiv_pos_adult,hiv_pos_total,hiv_pos_yield_perc_child,hiv_pos_yield_perc_adult,hiv_pos_yield_perc_all,hiv_pos_yield_cmts,new_care_child,new_care_adult,new_care_total,hiv_pos_care_perc_child,hiv_pos_care_perc_adult,hiv_pos_care_perc_all,hiv_pos_care_cmts,new_art_target_child,new_art_target_adult,new_art_target_total,started_art_child,started_art_adult,started_art_total,started_art_perc_child,started_art_perc_adult,started_art_perc_all,started_art_cmts,viral_load_target_child,viral_load_target_adult,viral_load_target_total,viral_load_done_child,viral_load_done_adult,viral_load_done_total,viral_test_perc_child,viral_test_perc_adult,viral_test_perc_all,viral_test_cmts,ipt_target_child,ipt_target_adult,ipt_target_total,ipt_child,ipt_adult,ipt_total,ipt_done_perc_child,ipt_done_perc_adult,ipt_done_perc_all,ipt_done_cmts,pmtct_hiv_pos_target,pmtct_hiv_pos,pmtct_hiv_pos_perc,pmtct_hiv_pos_cmts,eid_target,eid_done,eid_done_perc,eid_done_cmts,viral_load_mothers_target,viral_load_mothers_done,viral_load_mothers_perc,viral_load_mothers_cmts"
                .split(",");
        String headername[] = "COUNTY,SUB-COUNTY,FACILITY,START DATE,END DATE,TESTING TARGET CHILDREN,TESTING TARGET ADULT,TESTING TARGET TOTAL,TESTING CHILDREN,TESTING ADULT,TESTING TOTAL,PERCENTAGE TESTED AGAINST TARGET CHILDREN,PERCENTAGE TESTED AGAINST TARGET ADULT,PERCENTAGE TESTED AGAINST TARGET ALL,PERCENTAGE TESTED AGAINST TARGET COMMENTS,HIV POSITIVE TARGET CHILDREN,HIV POSITIVE TARGET ADULT,HIV POSITIVE TARGET TOTAL,HIV POSITIVE CHILDREN,HIV POSITIVE ADULT,HIV POSITIVE TOTAL,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED CHILDREN,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED ADULT,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED ALL,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED COMMENTS, NEW CARE CHILDREN,NEW CARE ADULT,NEW CARE TOTAL,PERCENTAGE HIV POSITIVE ENROLLED ON CARE CHILDREN,PERCENTAGE HIV POSITIVE ENROLLED ON CARE ADULT,PERCENTAGE HIV POSITIVE ENROLLED ON CARE ALL,PERCENTAGE HIV POSITIVE ENROLLED ON CARE COMMENTS,NEW ART TARGET CHILDREN,NEW ART TARGET ADULT,NEW ART TARGET TOTAL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART TOTAL,PERCENTAGE OF TARGET STARTED ON ART CHILDREN,PERCENTAGE OF TARGET STARTED ON ART ADULT,PERCENTAGE OF TARGET STARTED ON ART ALL,PERCENTAGE OF TARGET STARTED ON ART COMMENTS,VIRAL LOAD TARGET CHILDREN,VIRAL LOAD TARGET ADULT,VIRAL LOAD TARGET TOTAL,VIRAL LOAD DONE CHILDREN,VIRAL LOAD DONE ADULT,VIRAL LOAD DONE TOTAL,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET CHILDREN,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET ADULT,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET ALL,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET COMMENTS,IPT TARGET CHILDREN,IPT TARGET ADULT,IPT TARGET TOTAL,IPT CHILDREN,IPT ADULT,IPT TOTAL,PERCENTAGE OF IPT DONE AGAINST TARGET CHILDREN,PERCENTAGE OF IPT DONE AGAINST TARGET ADULT,PERCENTAGE OF IPT DONE AGAINST TARGET ALL,PERCENTAGE OF IPT DONE AGAINST TARGET COMMENTS,PMTCT HIV POSITIVE TARGET,PMTCT HIV POSITIVE ,PERCENTAGE PMTCT HIV POSITIVE YIELD ACHIEVED AGAINST TARGET ALL,PERCENTAGE PMTCT HIV POSITIVE YIELD ACHIEVED AGAINST TARGET COMMENTS,EID TARGET,EID DONE,PERCENTAGE EID DONE AGAINST TARGET ALL,PERCENTAGE EID DONE AGAINST TARGET COMMENTS,VIRAL LOAD MOTHERS TARGET,VIRAL LOAD MOTHERS DONE,PERCENTAGE VIRAL LOAD TESTS DONE FOR MOTHERS AGAINST TARGET ALL,PERCENTAGE VIRAL LOAD TESTS DONE FOR MOTHERS AGAINST TARGET COMMENTS,REPORTING RATE"
                .split(",");
        //
        //
        Row rw0 = rawdata.createRow(0);
        rw0.setHeightInPoints(25);

        for (int d = 0; d < headername.length; d++) {
            Cell ce = rw0.createCell(d);
            ce.setCellValue(headername[d]);

        }

        dbConnweb conn = new dbConnweb();

        conn.st.executeUpdate("SET GLOBAL max_allowed_packet = 209715200");
        conn.rs = conn.st.executeQuery("SHOW VARIABLES LIKE 'max_allowed_packet' ");
        if (conn.rs.next()) {
            System.out.println("Generating report | Max_allowed_connection_" + conn.rs.getString(2));

        }

        String where = " (enddate between '" + startdate + "' and '" + enddate + "')  " + countywhere
                + " and cur.id  like '%_weekly%' ";
        String where1 = " (enddate between '" + startdate + "' and '" + enddate
                + "') and weekly_data_new.id  like '%_weekly%'  ";

        String period = "1";
        String yr = enddate.substring(0, 4);
        String tar[] = enddate.split("-");

        int mwaka = new Integer(yr);
        if (new Integer(tar[1]) >= 10 && new Integer(tar[1]) <= 12) {
            mwaka = mwaka + 1;
        }
        //get data in report form and add into the various json macros.
        //String query="select facilityname,startdate,enddate, hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total ,hiv_pos_child as hiv_pos_child , hiv_pos_adult as hiv_pos_adult  ,hiv_pos_total as hiv_pos_total  ,new_care_child as new_care_child ,new_care_adult as new_care_adult ,new_care_total as new_care_total  , new_art_target_child  ,new_art_target_adult  ,new_art_target_total  ,started_art_child as started_art_child, started_art_adult as started_art_adult ,started_art_total as started_art_total ,viral_load_target_child  ,viral_load_target_adult  ,viral_load_target_total  ,viral_load_done_child as viral_load_done_child ,viral_load_done_adult as viral_load_done_adult ,viral_load_done_total as viral_load_done_total  ,ipt_target_child  ,ipt_target_adult  ,ipt_target_total  ,ipt_child as ipt_child ,ipt_adult as ipt_adult ,ipt_total as ipt_total ,testing_target_child  ,testing_target_adult  ,testing_target_total  ,test_child as test_child ,test_adult as test_adult   ,test_total as test_total , pmtct_hiv_pos_target,pmtct_hiv_pos as pmtct_hiv_pos, eid_target  , eid_done as eid_done, viral_load_mothers_target, viral_load_mothers_done as viral_load_mothers_done  from weekly_data_new join facility on weekly_data_new.facilityname=facility.facility_name ";

        String query = " select facility.county,facility.subcounty,cur.facilityname,cur.startdate,cur.enddate,case when  ROUND(targets.testing_target_child/"
                + period + ") >0 then ROUND(targets.testing_target_child/" + period
                + ") else 1 end as testing_target_child,case when  ROUND(targets.testing_target_adult/" + period
                + ") >0 then ROUND(targets.testing_target_adult/" + period
                + ") else 1 end as testing_target_adult,case when  ROUND(targets.testing_target_total/" + period
                + ") >0 then ROUND(targets.testing_target_total/" + period
                + ") else 1 end as testing_target_total,SUM(cur.test_child) as test_child,SUM(cur.test_adult) as test_adult,SUM(cur.test_total) as test_total,AVG(cur.tested_perc_child) as tested_perc_child,AVG(cur.tested_perc_adult) as tested_perc_adult,AVG(cur.tested_perc_all) as tested_perc_all,cur.tested_cmts,case when  ROUND(targets.hiv_pos_target_child/"
                + period + ") >0 then ROUND(targets.hiv_pos_target_child/" + period
                + ") else 1 end as hiv_pos_target_child,case when  ROUND(targets.hiv_pos_target_adult/" + period
                + ") >0 then ROUND(targets.hiv_pos_target_adult/" + period
                + ") else 1 end as hiv_pos_target_adult,case when  ROUND(targets.hiv_pos_target_total/" + period
                + ") >0 then ROUND(targets.hiv_pos_target_total/" + period
                + ") else 1 end as hiv_pos_target_total,SUM(cur.hiv_pos_child) as hiv_pos_child,SUM(cur.hiv_pos_adult) as hiv_pos_adult,SUM(cur.hiv_pos_total) as hiv_pos_total,AVG(cur.hiv_pos_yield_perc_child) as hiv_pos_yield_perc_child,AVG(cur.hiv_pos_yield_perc_adult) as hiv_pos_yield_perc_adult,AVG(cur.hiv_pos_yield_perc_all) as hiv_pos_yield_perc_all,cur.hiv_pos_yield_cmts,SUM(cur.new_care_child) as new_care_child,SUM(cur.new_care_adult) as new_care_adult,SUM(cur.new_care_total) as new_care_total,AVG(cur.hiv_pos_care_perc_child) as hiv_pos_care_perc_child,AVG(cur.hiv_pos_care_perc_adult) as hiv_pos_care_perc_adult,AVG(cur.hiv_pos_care_perc_all) as hiv_pos_care_perc_all,cur.hiv_pos_care_cmts,case when  ROUND(targets.new_art_target_child/"
                + period + ") >0 then ROUND(targets.new_art_target_child/" + period
                + ") else 1 end as new_art_target_child,case when  ROUND(targets.new_art_target_adult/" + period
                + ") >0 then ROUND(targets.new_art_target_adult/" + period
                + ") else 1 end as new_art_target_adult,case when  ROUND(targets.new_art_target_total/" + period
                + ") >0 then ROUND(targets.new_art_target_total/" + period
                + ") else 1 end as new_art_target_total,SUM(cur.started_art_child) as started_art_child,SUM(cur.started_art_adult) as started_art_adult,SUM(cur.started_art_total) as started_art_total,AVG(cur.started_art_perc_child) as started_art_perc_child,AVG(cur.started_art_perc_adult) as started_art_perc_adult, AVG(cur.started_art_perc_all) as started_art_perc_all,cur.started_art_cmts,case when  ROUND(targets.viral_load_target_child/"
                + period + ") >0 then ROUND(targets.viral_load_target_child/" + period
                + ") else 1 end as viral_load_target_child,case when  ROUND(targets.viral_load_target_adult/"
                + period + ") >0 then ROUND(targets.viral_load_target_adult/" + period
                + ") else 1 end as viral_load_target_adult,case when  ROUND(targets.viral_load_target_total/"
                + period + ") >0 then ROUND(targets.viral_load_target_total/" + period
                + ") else 1 end as viral_load_target_total,SUM(cur.viral_load_done_child) as viral_load_done_child,SUM(cur.viral_load_done_adult) as viral_load_done_adult, SUM(cur.viral_load_done_total) as viral_load_done_total,AVG(cur.viral_test_perc_child) as viral_test_perc_child,AVG(cur.viral_test_perc_adult) as viral_test_perc_adult, AVG(cur.viral_test_perc_all) as viral_test_perc_all,cur.viral_test_cmts,case when  ROUND(targets.ipt_target_child/"
                + period + ") >0 then ROUND(targets.ipt_target_child/" + period
                + ") else 1 end as ipt_target_child,case when  ROUND(targets.ipt_target_adult/" + period
                + ") >0 then ROUND(targets.ipt_target_adult/" + period
                + ") else 1 end as ipt_target_adult,case when  ROUND(targets.ipt_target_total/" + period
                + ") >0 then ROUND(targets.ipt_target_total/" + period
                + ") else 1 end as ipt_target_total,SUM(cur.ipt_child) as ipt_child,SUM(cur.ipt_adult) as ipt_adult ,SUM(cur.ipt_total) as ipt_total,AVG(cur.ipt_done_perc_child) as ipt_done_perc_child,AVG(cur.ipt_done_perc_adult) as ipt_done_perc_adult, AVG(cur.ipt_done_perc_all) as ipt_done_perc_all,cur.ipt_done_cmts,case when ROUND(targets.pmtct_hiv_pos_target/"
                + period + ") >0 then ROUND(targets.pmtct_hiv_pos_target/" + period
                + ") else 1 end as pmtct_hiv_pos_target,SUM(cur.pmtct_hiv_pos) as pmtct_hiv_pos,AVG(cur.pmtct_hiv_pos_perc) as pmtct_hiv_pos_perc,cur.pmtct_hiv_pos_cmts,case when  ROUND(targets.eid_target/"
                + period + ") >0 then ROUND(targets.eid_target/" + period
                + ") else 1 end as eid_target,SUM(cur.eid_done) as eid_done,AVG(cur.eid_done_perc) as eid_done_perc,cur.eid_done_cmts,case when  ROUND(targets.viral_load_mothers_target/"
                + period + ") >0 then ROUND(targets.viral_load_mothers_target/" + period
                + ") else 1 end as viral_load_mothers_target,SUM(cur.viral_load_mothers_done) as viral_load_mothers_done,AVG(cur.viral_load_mothers_perc) as viral_load_mothers_perc,cur.viral_load_mothers_cmts  from weekly_data_new cur join (facility join targets on (facility.mflcode=targets.facility and targets.year='"
                + mwaka + "' ) ) on cur.facilityname=facility.facility_name where  " + where + " "
                + " group by facilityname ";

        System.out.println("" + query);
        try {

            conn.rs = conn.st.executeQuery(query);
            String facilname = "";
            String facilnamecopy = "";

            String id = "";

            //percentages

            String viral_load_mothers_perc = null;
            String eid_done_perc = null;
            String pmtct_hiv_pos_perc = null;

            //new percenatages
            String hiv_pos_yield_perc_child = null;
            String hiv_pos_yield_perc_adult = null;
            String hiv_pos_yield_perc_all = null;
            String hiv_pos_care_perc_child = null;
            String hiv_pos_care_perc_adult = null;
            String hiv_pos_care_perc_all = null;
            String started_art_perc_child = null;
            String started_art_perc_adult = null;
            String started_art_perc_all = null;
            String viral_test_perc_child = null;
            String viral_test_perc_adult = null;
            String viral_test_perc_all = null;
            String ipt_done_perc_child = null;
            String ipt_done_perc_adult = null;
            String ipt_done_perc_all = null;
            String tested_perc_child = null;
            String tested_perc_adult = null;
            String tested_perc_all = null;
            //comments
            String viral_load_mothers_cmts = null;
            String eid_done_cmts = null;
            String pmtct_hiv_pos_cmts = null;
            String hiv_pos_yield_cmts = null;
            String hiv_pos_care_cmts = null;
            String started_art_cmts = null;
            String viral_test_cmts = null;
            String ipt_done_cmts = null;
            String tested_cmts = null;

            Row rwx = null;

            int rowno = 0;

            while (conn.rs.next()) {

                rowno++;

                rwx = rawdata.createRow(rowno);
                rwx.setHeightInPoints(23);

                //do a per facility 

                //System.out.println("___FACILITY : "+conn.rs.getString("facilityname")+" => hiv_pos_total "+conn.rs.getString("hiv_pos_total"));

                for (int a = 1; a <= header.length; a++) {

                    System.out.print(header[a - 1] + " _ " + conn.rs.getString(a));
                    Cell ce = rwx.createCell(a - 1);

                    if (a > 5 && a <= 73) {
                        if (header[a - 1].contains("cmts")) {
                            ce.setCellValue(conn.rs.getString(a));
                        } else if (header[a - 1].contains("_perc")) {
                            ce.setCellValue(conn.rs.getInt(a));
                            System.out.println("__Percent");
                        } else {
                            ce.setCellValue(conn.rs.getInt(a));
                        }
                    } else {

                        ce.setCellValue(conn.rs.getString(a));

                    }

                }
                //reporting rate
                int reportingrate = 0;

                String getrates = "select ROUND(((count(facilityname))/(" + weeks
                        + ")*100)) as count from weekly_data_new where facilityname like '"
                        + conn.rs.getString("facilityname") + "' and " + where1 + " ";

                conn.rs1 = conn.st1.executeQuery(getrates);

                if (conn.rs1.next()) {

                    reportingrate = conn.rs1.getInt(1);
                }

                Cell ce = rwx.createCell(header.length);
                ce.setCellValue(reportingrate);

            }

            if (conn.rs != null) {
                conn.rs.close();
            }
            if (conn.rs1 != null) {
                conn.rs1.close();
            }
            if (conn.st != null) {
                conn.st.close();
            }
            if (conn.st1 != null) {
                conn.st1.close();
            }
            if (conn.conne != null) {
                conn.conne.close();
            }

            Date dat = new Date();

            String dat1 = dat.toString().replace(" ", "_");

            // 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=MOIS_Cum_Rpt_From"
                    + startdate.replace(" ", "-") + "_To_" + enddate.replace(" ", "_") + ".xlsm");
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            OutputStream outStream = response.getOutputStream();
            outStream.write(outArray);
            outStream.flush();
            pkg.close();
            wb.dispose();
            // response.sendRedirect("index.jsp");

            File file = new File(pathtodelete);

            if (file.delete()) {
                System.out.println(file.getName() + " is deleted!");
            } else {
                System.out.println("Delete operation is failed.");
            }

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

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

}

From source file:code.excelreport_weekly.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try {/*  w  w  w .  j  a v  a  2s. co  m*/
        /* TODO output your page here. You may use following sample code. */

        String allpath = getServletContext().getRealPath("/MOIS_WEEKLY.xlsm");

        XSSFWorkbook wb1;

        String pathtodelete = null;

        Date da = new Date();
        String dat2 = da.toString().replace(" ", "_");

        dat2 = dat2.toString().replace(":", "_");

        String mydrive = allpath.substring(0, 1);

        String np = mydrive + ":\\APHIAPLUS\\MOIS\\MACROS\\MOIS_REPORT" + dat2 + ".xlsm";
        //check if file exists
        String sourcepath = getServletContext().getRealPath("/MOIS_WEEKLY.xlsm");

        File f = new File(np);
        if (!f.exists() && !f.isFile()) {
            /* do something */

            copytemplates ct = new copytemplates();
            System.out.println("Copying macros..");
            ct.transfermacros(sourcepath, np);

        } else
        //copy the file alone  
        {

            copytemplates ct = new copytemplates();
            //copy the agebased file only
            ct.copymacros(sourcepath, np);

        }

        File allpathfile = new File(np);

        OPCPackage pkg = OPCPackage.open(allpathfile);

        pathtodelete = np;

        //wb = new XSSFWorkbook( OPCPackage.open(allpath) );
        wb1 = new XSSFWorkbook(pkg);

        SXSSFWorkbook wb = new SXSSFWorkbook(wb1, 100);

        Sheet rawdata = wb.getSheet("Facility Report");
        String startdate = "2016-10-01";

        String enddate = "2016-10-30";
        String county = "";

        if (request.getParameter("startdate") != null) {
            startdate = request.getParameter("startdate");
        }
        if (request.getParameter("county") != null) {
            if (!request.getParameter("county").equals("")) {

                county = request.getParameter("county");

            }
        }

        if (request.getParameter("enddate") != null) {
            enddate = request.getParameter("enddate");
        }
        String countywhere = " and 1=1";
        if (!county.equals("")) {
            countywhere = "  and ( county like '" + county + "' )";
        }

        DateTime dateTime1 = new DateTime(startdate);
        DateTime dateTime2 = new DateTime(enddate);

        int weeks = Weeks.weeksBetween(dateTime1, dateTime2).getWeeks();
        System.out.println("___Weeks____" + weeks);

        if (weeks == 0) {
            weeks = 1;
        }
        //String header[]="county,subcounty,facilityname,startdate,enddate,hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total,hiv_pos_child,hiv_pos_adult,hiv_pos_total,new_care_child,new_care_adult,new_care_total,new_art_target_child,new_art_target_adult,new_art_target_total,started_art_child,started_art_adult,started_art_total,viral_load_target_child,viral_load_target_adult,viral_load_target_total,viral_load_done_child,viral_load_done_adult,viral_load_done_total,ipt_target_child,ipt_target_adult,ipt_target_total,ipt_child,ipt_adult,ipt_total,testing_target_child,testing_target_adult,testing_target_total,test_child,test_adult,test_total,pmtct_hiv_pos_target,pmtct_hiv_pos,eid_target,eid_done,viral_load_mothers_target,viral_load_mothers_done,hiv_pos_yield_perc_child,hiv_pos_yield_perc_adult,hiv_pos_yield_perc_all,hiv_pos_care_perc_child,hiv_pos_care_perc_adult,hiv_pos_care_perc_all,started_art_perc_child,started_art_perc_adult,started_art_perc_all,viral_test_perc_child,viral_test_perc_adult,viral_test_perc_all,ipt_done_perc_child,ipt_done_perc_adult,ipt_done_perc_all,tested_perc_child,tested_perc_adult,tested_perc_all,viral_load_mothers_perc,eid_done_perc,pmtct_hiv_pos_perc,hiv_pos_yield_cmts,hiv_pos_care_cmts,started_art_cmts,viral_test_cmts,ipt_done_cmts,tested_cmts,viral_load_mothers_cmts,eid_done_cmts,pmtct_hiv_pos_cmts".split(",");
        //String headername[]="COUNTY,SUB-COUNTY,FACILITY,START DATE,END DATE,HIV POSITIVE TARGET CHILDREN,HIV POSITIVE TARGET ADULT,HIV POSITIVE TARGET TOTAL,HIV POSITIVE CHILDREN,HIV POSITIVE ADULT,HIV POSITIVE TOTAL, NEW CARE CHILDREN,NEW CARE ADULT,NEW CARE TOTAL,NEW ART TARGET CHILDREN,NEW ART TARGET ADULT,NEW ART TARGET TOTAL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART TOTAL,VIRAL LOAD TARGET CHILDREN,VIRAL LOAD TARGET ADULT,VIRAL LOAD TARGET TOTAL,VIRAL LOAD DONE CHILDREN,VIRAL LOAD DONE ADULT,VIRAL LOAD DONE TOTAL,IPT TARGET CHILDREN,IPT TARGET ADULT,IPT TARGET TOTAL,IPT CHILDREN,IPT ADULT,IPT TOTAL,TESTING TARGET CHILDREN,TESTING TARGET ADULT,TESTING TARGET TOTAL,TESTING CHILDREN,TESTING ADULT,TESTING TOTAL,PMTCT HIV POSITIVE TARGET,PMTCT HIV POSITIVE ,EID TARGET,EID DONE,VIRAL LOAD MOTHERS TARGET,VIRAL LOAD MOTHERS DONE,HIV POSITIVE YIELD CHILDREN ,HIV POSITIVE YIELD ADULT,HIV POSITIVE YIELD ALL,HIV POSITIVE CARE CHILDREN,HIV POSITIVE CARE ADULT,HIV POSITIVE CARE ALL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART ,VIRAL TEST CHILDREN,VIRAL TEST ADULT,VIRAL TEST ALL,IPT DONE CHILD,IPT DONE ADULT,IPT DONE ALL,TESTED CHILD,TESTED ADULT,TESTED ALL,VIRAL LOAD MOTHERS ,EID DONE,PMTCT HIV POSITIVE,HIV POSITIVE YIELD COMMENTS,HIV POSITIVE CARE COMMENTS,STARTED ART COMMENTS,VIRAL TEST COMMENTS,IPT DONE COMMENTS ,TESTED COMMENTS,VIRAL LOAD MOTHERS COMMENTS,EID DONE COMMENTS,PMTCT HIV POSITIVE COMMENTS,REPORTING RATE".split(",");

        String header[] = "county,subcounty,facilityname,startdate,enddate,testing_target_child,testing_target_adult,testing_target_total,test_child,test_adult,test_total,tested_perc_child,tested_perc_adult,tested_perc_all,tested_cmts,hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total,hiv_pos_child,hiv_pos_adult,hiv_pos_total,hiv_pos_yield_perc_child,hiv_pos_yield_perc_adult,hiv_pos_yield_perc_all,hiv_pos_yield_cmts,new_care_child,new_care_adult,new_care_total,hiv_pos_care_perc_child,hiv_pos_care_perc_adult,hiv_pos_care_perc_all,hiv_pos_care_cmts,new_art_target_child,new_art_target_adult,new_art_target_total,started_art_child,started_art_adult,started_art_total,started_art_perc_child,started_art_perc_adult,started_art_perc_all,started_art_cmts,viral_load_target_child,viral_load_target_adult,viral_load_target_total,viral_load_done_child,viral_load_done_adult,viral_load_done_total,viral_test_perc_child,viral_test_perc_adult,viral_test_perc_all,viral_test_cmts,ipt_target_child,ipt_target_adult,ipt_target_total,ipt_child,ipt_adult,ipt_total,ipt_done_perc_child,ipt_done_perc_adult,ipt_done_perc_all,ipt_done_cmts,pmtct_hiv_pos_target,pmtct_hiv_pos,pmtct_hiv_pos_perc,pmtct_hiv_pos_cmts,eid_target,eid_done,eid_done_perc,eid_done_cmts,viral_load_mothers_target,viral_load_mothers_done,viral_load_mothers_perc,viral_load_mothers_cmts"
                .split(",");
        String headername[] = "COUNTY,SUB-COUNTY,FACILITY,START DATE,END DATE,TESTING TARGET CHILDREN,TESTING TARGET ADULT,TESTING TARGET TOTAL,TESTING CHILDREN,TESTING ADULT,TESTING TOTAL,PERCENTAGE TESTED AGAINST TARGET CHILDREN,PERCENTAGE TESTED AGAINST TARGET ADULT,PERCENTAGE TESTED AGAINST TARGET ALL,PERCENTAGE TESTED AGAINST TARGET COMMENTS,HIV POSITIVE TARGET CHILDREN,HIV POSITIVE TARGET ADULT,HIV POSITIVE TARGET TOTAL,HIV POSITIVE CHILDREN,HIV POSITIVE ADULT,HIV POSITIVE TOTAL,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED CHILDREN,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED ADULT,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED ALL,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED COMMENTS, NEW CARE CHILDREN,NEW CARE ADULT,NEW CARE TOTAL,PERCENTAGE HIV POSITIVE ENROLLED ON CARE CHILDREN,PERCENTAGE HIV POSITIVE ENROLLED ON CARE ADULT,PERCENTAGE HIV POSITIVE ENROLLED ON CARE ALL,PERCENTAGE HIV POSITIVE ENROLLED ON CARE COMMENTS,NEW ART TARGET CHILDREN,NEW ART TARGET ADULT,NEW ART TARGET TOTAL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART TOTAL,PERCENTAGE OF TARGET STARTED ON ART CHILDREN,PERCENTAGE OF TARGET STARTED ON ART ADULT,PERCENTAGE OF TARGET STARTED ON ART ALL,PERCENTAGE OF TARGET STARTED ON ART COMMENTS,VIRAL LOAD TARGET CHILDREN,VIRAL LOAD TARGET ADULT,VIRAL LOAD TARGET TOTAL,VIRAL LOAD DONE CHILDREN,VIRAL LOAD DONE ADULT,VIRAL LOAD DONE TOTAL,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET CHILDREN,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET ADULT,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET ALL,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET COMMENTS,IPT TARGET CHILDREN,IPT TARGET ADULT,IPT TARGET TOTAL,IPT CHILDREN,IPT ADULT,IPT TOTAL,PERCENTAGE OF IPT DONE AGAINST TARGET CHILDREN,PERCENTAGE OF IPT DONE AGAINST TARGET ADULT,PERCENTAGE OF IPT DONE AGAINST TARGET ALL,PERCENTAGE OF IPT DONE AGAINST TARGET COMMENTS,PMTCT HIV POSITIVE TARGET,PMTCT HIV POSITIVE ,PERCENTAGE PMTCT HIV POSITIVE YIELD ACHIEVED AGAINST TARGET ALL,PERCENTAGE PMTCT HIV POSITIVE YIELD ACHIEVED AGAINST TARGET COMMENTS,EID TARGET,EID DONE,PERCENTAGE EID DONE AGAINST TARGET ALL,PERCENTAGE EID DONE AGAINST TARGET COMMENTS,VIRAL LOAD MOTHERS TARGET,VIRAL LOAD MOTHERS DONE,PERCENTAGE VIRAL LOAD TESTS DONE FOR MOTHERS AGAINST TARGET ALL,PERCENTAGE VIRAL LOAD TESTS DONE FOR MOTHERS AGAINST TARGET COMMENTS,REPORTING RATE"
                .split(",");
        //
        //
        Row rw0 = rawdata.createRow(0);
        rw0.setHeightInPoints(25);

        for (int d = 0; d < headername.length; d++) {
            Cell ce = rw0.createCell(d);
            ce.setCellValue(headername[d]);

        }

        dbConnweb conn = new dbConnweb();

        conn.st.executeUpdate("SET GLOBAL max_allowed_packet = 209715200");
        conn.rs = conn.st.executeQuery("SHOW VARIABLES LIKE 'max_allowed_packet' ");
        if (conn.rs.next()) {
            System.out.println("Generating report | Max_allowed_connection_" + conn.rs.getString(2));

        }

        String where = " (enddate between '" + startdate + "' and '" + enddate + "')  " + countywhere
                + " and cur.id  like '%_weekly%' ";
        String where1 = " (enddate between '" + startdate + "' and '" + enddate
                + "') and weekly_data_new.id  like '%_weekly%'  ";

        String period = "52";
        String yr = enddate.substring(0, 4);

        String tar[] = enddate.split("-");

        int mwaka = new Integer(yr);
        if (new Integer(tar[1]) >= 10 && new Integer(tar[1]) <= 12) {
            mwaka = mwaka + 1;
        }

        //get data in report form and add into the various json macros.
        //String query="select facilityname,startdate,enddate, hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total ,hiv_pos_child as hiv_pos_child , hiv_pos_adult as hiv_pos_adult  ,hiv_pos_total as hiv_pos_total  ,new_care_child as new_care_child ,new_care_adult as new_care_adult ,new_care_total as new_care_total  , new_art_target_child  ,new_art_target_adult  ,new_art_target_total  ,started_art_child as started_art_child, started_art_adult as started_art_adult ,started_art_total as started_art_total ,viral_load_target_child  ,viral_load_target_adult  ,viral_load_target_total  ,viral_load_done_child as viral_load_done_child ,viral_load_done_adult as viral_load_done_adult ,viral_load_done_total as viral_load_done_total  ,ipt_target_child  ,ipt_target_adult  ,ipt_target_total  ,ipt_child as ipt_child ,ipt_adult as ipt_adult ,ipt_total as ipt_total ,testing_target_child  ,testing_target_adult  ,testing_target_total  ,test_child as test_child ,test_adult as test_adult   ,test_total as test_total , pmtct_hiv_pos_target,pmtct_hiv_pos as pmtct_hiv_pos, eid_target  , eid_done as eid_done, viral_load_mothers_target, viral_load_mothers_done as viral_load_mothers_done  from weekly_data_new join facility on weekly_data_new.facilityname=facility.facility_name ";

        String query = " select facility.county,facility.subcounty,cur.facilityname,cur.startdate,cur.enddate,case when  ROUND(targets.testing_target_child/"
                + period + ") >0 then ROUND(targets.testing_target_child/" + period
                + ") else 1 end as testing_target_child,case when  ROUND(targets.testing_target_adult/" + period
                + ") >0 then ROUND(targets.testing_target_adult/" + period
                + ") else 1 end as testing_target_adult,case when  ROUND(targets.testing_target_total/" + period
                + ") >0 then ROUND(targets.testing_target_total/" + period
                + ") else 1 end as testing_target_total,cur.test_child,cur.test_adult,cur.test_total,cur.tested_perc_child,cur.tested_perc_adult,cur.tested_perc_all,cur.tested_cmts,case when  ROUND(targets.hiv_pos_target_child/"
                + period + ") >0 then ROUND(targets.hiv_pos_target_child/" + period
                + ") else 1 end as hiv_pos_target_child,case when  ROUND(targets.hiv_pos_target_adult/" + period
                + ") >0 then ROUND(targets.hiv_pos_target_adult/" + period
                + ") else 1 end as hiv_pos_target_adult,case when  ROUND(targets.hiv_pos_target_total/" + period
                + ") >0 then ROUND(targets.hiv_pos_target_total/" + period
                + ") else 1 end as hiv_pos_target_total,cur.hiv_pos_child,cur.hiv_pos_adult,cur.hiv_pos_total,cur.hiv_pos_yield_perc_child,cur.hiv_pos_yield_perc_adult,cur.hiv_pos_yield_perc_all,cur.hiv_pos_yield_cmts,cur.new_care_child,cur.new_care_adult,cur.new_care_total,cur.hiv_pos_care_perc_child,cur.hiv_pos_care_perc_adult,cur.hiv_pos_care_perc_all,cur.hiv_pos_care_cmts,case when  ROUND(targets.new_art_target_child/"
                + period + ") >0 then ROUND(targets.new_art_target_child/" + period
                + ") else 1 end as new_art_target_child,case when  ROUND(targets.new_art_target_adult/" + period
                + ") >0 then ROUND(targets.new_art_target_adult/" + period
                + ") else 1 end as new_art_target_adult,case when  ROUND(targets.new_art_target_total/" + period
                + ") >0 then ROUND(targets.new_art_target_total/" + period
                + ") else 1 end as new_art_target_total,cur.started_art_child,cur.started_art_adult,cur.started_art_total,cur.started_art_perc_child,cur.started_art_perc_adult,cur.started_art_perc_all,cur.started_art_cmts,case when  ROUND(targets.viral_load_target_child/"
                + period + ") >0 then ROUND(targets.viral_load_target_child/" + period
                + ") else 1 end as viral_load_target_child,case when  ROUND(targets.viral_load_target_adult/"
                + period + ") >0 then ROUND(targets.viral_load_target_adult/" + period
                + ") else 1 end as viral_load_target_adult,case when  ROUND(targets.viral_load_target_total/"
                + period + ") >0 then ROUND(targets.viral_load_target_total/" + period
                + ") else 1 end as viral_load_target_total,cur.viral_load_done_child,cur.viral_load_done_adult,cur.viral_load_done_total,cur.viral_test_perc_child,cur.viral_test_perc_adult,cur.viral_test_perc_all,cur.viral_test_cmts,case when  ROUND(targets.ipt_target_child/"
                + period + ") >0 then ROUND(targets.ipt_target_child/" + period
                + ") else 1 end as ipt_target_child,case when  ROUND(targets.ipt_target_adult/" + period
                + ") >0 then ROUND(targets.ipt_target_adult/" + period
                + ") else 1 end as ipt_target_adult,case when  ROUND(targets.ipt_target_total/" + period
                + ") >0 then ROUND(targets.ipt_target_total/" + period
                + ") else 1 end as ipt_target_total,cur.ipt_child,cur.ipt_adult,cur.ipt_total,cur.ipt_done_perc_child,cur.ipt_done_perc_adult,cur.ipt_done_perc_all,cur.ipt_done_cmts,case when  ROUND(targets.pmtct_hiv_pos_target/"
                + period + ") >0 then ROUND(targets.pmtct_hiv_pos_target/" + period
                + ") else 1 end as pmtct_hiv_pos_target,cur.pmtct_hiv_pos,cur.pmtct_hiv_pos_perc,cur.pmtct_hiv_pos_cmts,case when  ROUND(targets.eid_target/"
                + period + ") >0 then ROUND(targets.eid_target/" + period
                + ") else 1 end as eid_target,cur.eid_done,cur.eid_done_perc,cur.eid_done_cmts,case when  ROUND(targets.viral_load_mothers_target/"
                + period + ") >0 then ROUND(targets.viral_load_mothers_target/" + period
                + ") else 1 end as viral_load_mothers_target,cur.viral_load_mothers_done,cur.viral_load_mothers_perc,cur.viral_load_mothers_cmts  from weekly_data_new cur join (facility join targets on (facility.mflcode=targets.facility and targets.year='"
                + mwaka + "' ) ) on cur.facilityname=facility.facility_name where  " + where + " "
                + " group by cur.id ";

        System.out.println("" + query);
        try {

            conn.rs = conn.st.executeQuery(query);
            String facilname = "";
            String facilnamecopy = "";

            String id = "";

            //percentages

            String viral_load_mothers_perc = null;
            String eid_done_perc = null;
            String pmtct_hiv_pos_perc = null;

            //new percenatages
            String hiv_pos_yield_perc_child = null;
            String hiv_pos_yield_perc_adult = null;
            String hiv_pos_yield_perc_all = null;
            String hiv_pos_care_perc_child = null;
            String hiv_pos_care_perc_adult = null;
            String hiv_pos_care_perc_all = null;
            String started_art_perc_child = null;
            String started_art_perc_adult = null;
            String started_art_perc_all = null;
            String viral_test_perc_child = null;
            String viral_test_perc_adult = null;
            String viral_test_perc_all = null;
            String ipt_done_perc_child = null;
            String ipt_done_perc_adult = null;
            String ipt_done_perc_all = null;
            String tested_perc_child = null;
            String tested_perc_adult = null;
            String tested_perc_all = null;
            //comments
            String viral_load_mothers_cmts = null;
            String eid_done_cmts = null;
            String pmtct_hiv_pos_cmts = null;
            String hiv_pos_yield_cmts = null;
            String hiv_pos_care_cmts = null;
            String started_art_cmts = null;
            String viral_test_cmts = null;
            String ipt_done_cmts = null;
            String tested_cmts = null;

            Row rwx = null;

            int rowno = 0;

            while (conn.rs.next()) {

                rowno++;

                rwx = rawdata.createRow(rowno);
                rwx.setHeightInPoints(23);

                //do a per facility 

                //System.out.println("___FACILITY : "+conn.rs.getString("facilityname")+" => hiv_pos_total "+conn.rs.getString("hiv_pos_total"));

                for (int a = 1; a <= header.length; a++) {

                    System.out.print(header[a - 1] + " _ " + conn.rs.getString(a));
                    Cell ce = rwx.createCell(a - 1);

                    if (a > 5 && a <= 73) {
                        if (header[a - 1].contains("cmts")) {
                            ce.setCellValue(conn.rs.getString(a));
                        } else if (header[a - 1].contains("_perc")) {
                            ce.setCellValue(conn.rs.getInt(a));
                            System.out.println("__Percent");
                        } else {
                            ce.setCellValue(conn.rs.getInt(a));
                        }
                    } else {
                        ce.setCellValue(conn.rs.getString(a));
                    }

                }
                //reporting rate
                int reportingrate = 0;

                String getrates = "select ROUND(((count(facilityname))/(" + weeks
                        + ")*100)) as count from weekly_data_new where facilityname like '"
                        + conn.rs.getString("facilityname") + "' and " + where1 + " ";

                conn.rs1 = conn.st1.executeQuery(getrates);

                if (conn.rs1.next()) {

                    reportingrate = conn.rs1.getInt(1);
                }

                Cell ce = rwx.createCell(header.length);
                ce.setCellValue(reportingrate);

            }

            if (conn.rs != null) {
                conn.rs.close();
            }
            if (conn.rs1 != null) {
                conn.rs1.close();
            }
            if (conn.st != null) {
                conn.st.close();
            }
            if (conn.st1 != null) {
                conn.st1.close();
            }
            if (conn.conne != null) {
                conn.conne.close();
            }

            Date dat = new Date();

            String dat1 = dat.toString().replace(" ", "_");

            // 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=MOIS_Weekly_Report_From"
                    + startdate.replace(" ", "-") + "_To_" + enddate.replace(" ", "_") + ".xlsm");
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            OutputStream outStream = response.getOutputStream();
            outStream.write(outArray);
            outStream.flush();
            pkg.close();
            wb.dispose();
            // response.sendRedirect("index.jsp");

            File file = new File(pathtodelete);

            if (file.delete()) {
                System.out.println(file.getName() + " is deleted!");
            } else {
                System.out.println("Delete operation is failed.");
            }

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

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

}

From source file:CODIGOS.EditaPlanilha.java

public static void edita(String diretorio, String arquivo, int linha, int coluna, String texto) {
    try {/*from   ww  w  . j av a2s.  c  o  m*/
        File dir = new File(diretorio);
        File file = new File(dir, arquivo + ".xlsx");
        InputStream inp = new FileInputStream(file);
        try {
            Workbook wb = WorkbookFactory.create(inp);
            Sheet sheet = wb.getSheetAt(0);
            Row row = sheet.getRow(linha);//LINHA
            Cell cell = row.getCell(0);
            cell = row.createCell(coluna);//COLUNA
            cell.setCellType(Cell.CELL_TYPE_STRING);
            cell.setCellValue(texto);
            try (FileOutputStream fileOut = new FileOutputStream(file)) {
                wb.write(fileOut);
            }
        } catch (IOException | InvalidFormatException ex) {
            Logger.getLogger(EditaPlanilha.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(EditaPlanilha.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.a9ski.jsf.exporter.DataTableExcelExporter.java

License:Apache License

protected Cell addColumnValue(final Row row, final ExportValue value, final UIColumn tableCol)
        throws ExportException {
    final int cellIndex = row.getLastCellNum() == -1 ? 0 : row.getLastCellNum();
    final Cell cell = row.createCell(cellIndex);

    addColumnValue(cell, value, tableCol);

    return cell;/*from  w  w  w  . j a v  a  2 s. c om*/
}

From source file:com.a9ski.jsf.exporter.DataTableExcelExporter.java

License:Apache License

protected void addColumnValue(final Row row, final List<UIComponent> components, final UIColumn tableCol,
        final DataTable table) throws ExportException {
    final int cellIndex = row.getLastCellNum() == -1 ? 0 : row.getLastCellNum();
    final Cell cell = row.createCell(cellIndex);
    final StringBuilder builder = new StringBuilder();
    final FacesContext context = FacesContext.getCurrentInstance();

    for (final UIComponent component : components) {
        if (component.isRendered()) {
            final String value = exportValue(context, component).getStringValue();

            if (value != null) {
                builder.append(value);//from www .j a va  2s.  co m
            }
        }
    }

    final String textValue = builder.toString();
    addColumnValue(cell, new ExportValue(textValue, textValue, table), tableCol);
}

From source file:com.aan.girsang.client.ui.master.barang.BarangPanel.java

private void exportExcel(List<Barang> dataList) throws IOException {
    if (dataList != null && !dataList.isEmpty()) {
        HSSFWorkbook workBook = new HSSFWorkbook();
        HSSFSheet sheet = workBook.createSheet();
        HSSFSheet worksheet = workBook.createSheet("Sheet 0");
        // Nama Field
        Row judul = sheet.createRow((short) 0);
        Cell cell = judul.createCell((short) 0);
        cell.setCellValue("This is a test of merging");
        HSSFRow headingRow = sheet.createRow((short) 2);
        headingRow.createCell((short) 0).setCellValue("ID");
        headingRow.createCell((short) 1).setCellValue("BARCODE 1");
        headingRow.createCell((short) 2).setCellValue("BARCODE 2");
        headingRow.createCell((short) 3).setCellValue("NAMA BARANG");
        headingRow.createCell((short) 4).setCellValue("GOLONGAN");
        headingRow.createCell((short) 5).setCellValue("SAT. JUAL");
        headingRow.createCell((short) 6).setCellValue("ST. TOKO");
        headingRow.createCell((short) 7).setCellValue("ST. GUDANG");
        headingRow.createCell((short) 8).setCellValue("SAT. BELI");
        headingRow.createCell((short) 9).setCellValue("ISI PEM.");
        headingRow.createCell((short) 10).setCellValue("HRG PEM.");
        headingRow.createCell((short) 11).setCellValue("HRG NORMAL");
        headingRow.createCell((short) 12).setCellValue("HRG MEMBER");
        headingRow.createCell((short) 13).setCellValue("JUAL");
        int panjang = headingRow.getLastCellNum() - 1;
        short rowNo = 3;

        sheet.addMergedRegion(new CellRangeAddress(0, //first row (0-based)
                0, //last row  (0-based)
                0, //first column (0-based)
                panjang //last column  (0-based)
        ));/*w  w  w  . j  a v  a 2 s .c om*/
        CellStyle styleData = workBook.createCellStyle();
        styleData.setBorderBottom(CellStyle.BORDER_THIN);
        styleData.setBorderRight(CellStyle.BORDER_THIN);
        styleData.setBorderLeft(CellStyle.BORDER_THIN);
        for (Barang b : dataList) {
            HSSFRow row = sheet.createRow(rowNo);
            String jual;
            if (b.getJual() == true) {
                jual = "Jual";
            } else {
                jual = "Tidak";
            }
            row.createCell((short) 0).setCellValue(b.getPlu());
            row.createCell((short) 1).setCellValue(b.getBarcode1());
            row.createCell((short) 2).setCellValue(b.getBarcode2());
            row.createCell((short) 3).setCellValue(b.getNamaBarang());
            row.createCell((short) 4).setCellValue(b.getGolonganBarang().getGolonganBarang());
            row.createCell((short) 5).setCellValue(b.getSatuan());
            row.createCell((short) 6).setCellValue(b.getStokToko());
            row.createCell((short) 7).setCellValue(b.getStokGudang());
            row.createCell((short) 8).setCellValue(b.getSatuanPembelian());
            row.createCell((short) 9).setCellValue(b.getIsiPembelian());
            row.createCell((short) 10).setCellValue(TextComponentUtils.formatNumber(b.getHargaBeli()));
            row.createCell((short) 11).setCellValue(TextComponentUtils.formatNumber(b.getHargaNormal()));
            row.createCell((short) 12).setCellValue(TextComponentUtils.formatNumber(b.getHargaMember()));
            row.createCell((short) 13).setCellValue(jual);
            for (int i = 0; i <= 13; i++) {
                row.getCell((short) i).setCellStyle(styleData);
            }
            rowNo++;
        }
        for (int i = 0; i <= 13; i++) {
            sheet.autoSizeColumn(i);
        }
        Font font = workBook.createFont();
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        //style judul
        CellStyle styleTitle = workBook.createCellStyle();
        styleTitle.setAlignment(CellStyle.ALIGN_CENTER_SELECTION);
        styleTitle.setFont(font);
        judul.getCell(0).setCellStyle(styleTitle);

        //judul field
        CellStyle styleHeading = workBook.createCellStyle();
        styleHeading.setFont(font);
        styleHeading.setAlignment(CellStyle.ALIGN_CENTER_SELECTION);
        styleHeading.setBorderBottom(CellStyle.BORDER_THIN);
        styleHeading.setBorderTop(CellStyle.BORDER_THIN);
        styleHeading.setBorderRight(CellStyle.BORDER_THIN);
        styleHeading.setBorderLeft(CellStyle.BORDER_THIN);
        for (int i = 0; i < headingRow.getLastCellNum(); i++) {//For each cell in the row 
            headingRow.getCell(i).setCellStyle(styleHeading);//Set the style
        }
        String file = "D:/Student_detais.xls";
        try {
            try (FileOutputStream fos = new FileOutputStream(file)) {
                workBook.write(fos);
            }
            JOptionPane.showMessageDialog(null, "Sukses");
        } catch (FileNotFoundException e) {
            System.out.println("Invalid directory or file not found");
        } catch (IOException e) {
            System.out.println("Error occurred while writting excel file to directory");
        }
    }
}

From source file:com.actelion.research.spiritapp.report.AbstractReport.java

License:Open Source License

protected Cell set(Sheet sheet, int row, int col, Object text, Style style, int rowspan, int colspan) {
    Row r = sheet.getRow(row);
    if (r == null)
        r = sheet.createRow(row);//from w ww .j  a  v  a  2  s .  com
    Cell c = r.getCell(col);
    if (c == null)
        c = r.createCell(col);
    c.setCellStyle(styles.get(style));
    if (text == null) {
        if (c.getCellStyle().getDataFormatString().startsWith("0")) {
            c.setCellType(Cell.CELL_TYPE_NUMERIC);
            c.setCellValue("");
        } else {
            c.setCellType(Cell.CELL_TYPE_STRING);
            c.setCellValue("");
        }
    } else if (text instanceof String) {
        try {
            c.setCellType(Cell.CELL_TYPE_NUMERIC);
            c.setCellValue(Integer.parseInt((String) text));
        } catch (Exception e) {
            try {
                c.setCellType(Cell.CELL_TYPE_NUMERIC);
                c.setCellValue(Double.parseDouble((String) text));
            } catch (Exception e2) {
                c.setCellType(Cell.CELL_TYPE_STRING);
                c.setCellValue((String) text);
            }
        }
    } else if (text instanceof Double) {
        c.setCellValue((Double) text);
        c.setCellType(Cell.CELL_TYPE_NUMERIC);
    } else if (text instanceof Integer) {
        c.setCellValue((Integer) text);
        c.setCellType(Cell.CELL_TYPE_NUMERIC);
    } else if (text instanceof Date) {
        c.setCellValue((Date) text);
    }
    if (rowspan > 1 || colspan > 1) {
        sheet.addMergedRegion(new CellRangeAddress(row, row + rowspan - 1, col, col + colspan - 1));
        for (int i = 0; i < rowspan; i++) {
            for (int j = 0; j < colspan; j++) {
                if (i > 0 || j > 0)
                    set(sheet, row + i, col + j, "", style);
            }
        }
    }
    return c;
}