`

POI导出Excel文件

    博客分类:
  • Java
阅读更多

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet(filename);
       
        HSSFRow row=null;
        HSSFCell cell=null;
        Settlement settlement=null;
       
        //列头
        row = sheet.createRow((short)(0));
       
        cell = row.createCell((short)(0));
        cell.setCellValue("订单号");
       
        cell = row.createCell((short)(1));   
        cell.setCellValue("商户名");
       
        cell = row.createCell((short)(2));   
        cell.setCellValue("公司名称");
       
        cell = row.createCell((short)(3));   
        cell.setCellValue("下单时间");
       
        cell = row.createCell((short)(4));   
        cell.setCellValue("金额");
       
        short length=(short)list.size();
       
        for(int i=1;i<length;i++){
            settlement=(Settlement)list.get(i);
           
            row = sheet.createRow((short)i);
               
            cell = row.createCell((short)0);           
            cell.setCellValue(settlement.getOrderNum());       
              
            cell = row.createCell((short)1);           
            cell.setCellValue(settlement.getUsername());   
              
            cell = row.createCell((short)2);           
            cell.setCellValue(settlement.getCompany_name().toString());   
              
            cell = row.createCell((short)3);           
            cell.setCellValue(settlement.getCreatetime().toLocaleString());   
              
            cell = row.createCell((short)4);           
            cell.setCellValue(round(settlement.getTotal().doubleValue()/100,2));   
           
        }
       
        row = sheet.createRow((short)(length));
        cell = row.createCell((short)(0));
        cell.setCellValue("合计");   
       
        cell = row.createCell((short)(1));   
        cell.setCellValue(round(count.doubleValue()/100,2));      
       

       
        //写文件
        FileOutputStream fileOut = new FileOutputStream("/data/jsp/admin.yimei.com/webapps/WEB-INF/work/"+filename+".xls");
       
        wb.write(fileOut);
        fileOut.close();
       
       
       
        //读文件
        byte[] b=new byte[100];
        File fileBook= new File(this.getServletContext().getRealPath("/WEB-INF/work/"+filename+".xls"));
       
        if(!fileBook.exists()){
            return;
        }else{
             try{ 
                 //设置输出的格式
                 InputStream in=new FileInputStream(fileBook);
                 response.reset();
                 response.setContentType("application/vnd.ms-excel;chartset=utf-8;charset=UTF-8");
                    response.addHeader("Content-Disposition","attachment; filename=\"" + fileBook.getName() + "\"");
          
                   log.info(new String(fileBook.getName().getBytes("ISO-8859-1"),"UTF-8"));
                  
                 //循环取出流中的数据
                 int len=0;
                 while((len=in.read(b)) >0) {
                     response.getOutputStream().write(b,0,len);
                 }
                 
                 in.close();
             }catch(Exception e)
             {
                 log.info(e.getMessage());
             }

 

需要poi包的支持

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics