2012-07-26 157 views
1

下面的代碼片斷將結果集數據導出到excel文件,但它不會創建標題。 我已經使用使用Apache-POI創建的Excelsheet沒有標題

HSSFRow rowhead = sheet.createRow((short) 0);來創建標題,但創建的Excel表只包含沒有標題的數據。可能是什麼問題?請引導。

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; 
    } 

其中expReportDetailCol包含resulteset數據Collection<List> expReportDetailCol = new ArrayList<List>();

回答

4

你想++rowCounterrowCounter++rowCounter++表示給我當前值,然後遞增。你rowCounter值爲零時開始,所以你的第一組數據將其覆蓋

要麼當你用插頭來實現,或者使用++ rowCounter代替

0

HSSFSheet片= workbook.createSheet(」增量rowCounter Zero_Report_N「); //生成用紙

ü可以通過將ArrayList中

HSSFCell cell = row.createCell(i);//u can pass values of headers 
    cell.setCellValue(value); 
    sheet.autoSizeColumn(i); 
添加頁眉
相關問題