1

Below snippet exports the resultset data to the excel file however it does not creates the headers. I have used

HSSFRow rowhead = sheet.createRow((short) 0); to create header but the created excel sheet contains only data with no headers. what can be the issue?.Please guide.

public boolean prepareExcelFilefromQuery(Collection<List> queryDataRowWise,HttpServletResponse response)        
    HSSFRow row = null;
                HSSFCell cell=null;
                Integer rowCounter=0;
                Integer colCounter;
                boolean success=false;
                try {

                    Iterator<List> rowIterator = queryDataRowWise.iterator();

                    HSSFWorkbook workbook = new HSSFWorkbook();
                    HSSFSheet sheet = workbook.createSheet("Zero_Report_N");

                    HSSFRow rowhead = sheet.createRow((short) 0);

                      rowhead.createCell((short) 0).setCellValue("APPLN_RECD_DT");
                    rowhead.createCell((short) 1).setCellValue("POLICY_NO");
                    rowhead.createCell((short) 2).setCellValue("APPLN_NO");
                    rowhead.createCell((short) 3).setCellValue("OR_NUMBER");


                    while(rowIterator.hasNext())
                    {
                        colCounter=0;

                        queryDataColWise=(List)rowIterator.next();

                        Iterator colIterator = queryDataColWise.iterator();

                        row = sheet.createRow(rowCounter++);
                        while(colIterator.hasNext())
                        {
                            cell = row.createCell(colCounter++);


                            Object o = colIterator.next();

                            if(o instanceof java.lang.String )
                            {   
                                String s=(String)o;
                                cell.setCellValue(s);
                            }
                            else if(o instanceof java.lang.Double )
                            {
                                Double d=(Double)o;
                                cell.setCellValue(d);
                            }
                            else if(o instanceof java.lang.Integer )
                            {
                                Integer i=(Integer)o;
                                cell.setCellValue(i);
                            }
                            else if(o instanceof java.util.Date )
                            {   
                                Date date=(Date)o;

                                SimpleDateFormat FORMATTER;              

                                FORMATTER = new SimpleDateFormat("MM/dd/yyyy");  
                                String date11 = FORMATTER.format(date);     

                                HSSFCellStyle cellStyle = workbook.createCellStyle();
                                cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
                                cell.setCellStyle(cellStyle);
                                cell.setCellValue(FORMATTER.parse(date11));
                            }
                        }
                    }

        workbook.write(response.getOutputStream());
                success=true;
    catch (Exception e) {
            logger.debug("Exception caught in prepareExcelFilefromQuery class ", e);
                }
        return success;
    }

where expReportDetailCol contains resulteset data Collection<List> expReportDetailCol = new ArrayList<List>();

2 Answers 2

4

You want ++rowCounter not rowCounter++. rowCounter++ means give me the current value, then increment. Your rowCounter value is zero to start, so your first set of data overwrites it

Either increment rowCounter when you're done with the header, or use ++rowCounter instead

Sign up to request clarification or add additional context in comments.

Comments

0

HSSFSheet sheet = workbook.createSheet("Zero_Report_N"); // after creating sheet

u can add headers by putting in arraylist

  HSSFCell cell = row.createCell(i);//u can pass values of headers
    cell.setCellValue(value);
    sheet.autoSizeColumn(i);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.