Excel导出

//导出

  1. 引入jar包(poi-3.10-FINAL.jar);
  2. 编写方法 .java
    @RequestMapping(value=”/exportExcel.do”,method{RequestMethod.POST,RequestMethod.GET})
    public void exportExcel() throws Exception{

        HttpServletRequest  request=ServletActionContext.getRequest();
        HttpServletResponse response=ServletActionContext.getResponse();
        String cityId = request.getParameter("regionid");
        String countryId = request.getParameter("countyid");
        String cityNm = request.getParameter("cityNm");
        String countyNm = request.getParameter("countyNm");
        String table = request.getParameter("table");
        String title = request.getParameter("title")+".xls";
    
        response.reset();// 清空输出流        
    response.setHeader("Content-disposition", "attachment; filename="+ new String(title.getBytes("GBK"),"ISO8859-1"));  
        // 设定输出文件头        
    response.setContentType("application/msexcel");// 定义输出类型  
        BufferedOutputStream bos=null;
        OutputStream  out=null;
    
        List list = new ArrayList();
        String sql="";
        HSSFWorkbook workbook=null;
    
        sql="select t.city_name,t.county_name"+
        " from sdtower_eval_prop_all t where 1=1 ";
    
        if(cityId != null && !cityId.equals(""))
        sql += " and city_id = '" + cityId + "' ";
        if(countryId != null && !countryId.equals(""))
        sql += " and county_id = '" + countryId + "' ";
        list = dataPersistDao.searchByConnection(sql);
        String[] zdmc ={"地市","区县","等等"};
    workbook = this.getExc(table,zdmc,list);  // 写入数据到Excel
        out=response.getOutputStream();
          bos=new BufferedOutputStream(out);
            workbook.write(bos);
            bos.flush();
            out.close();
    

    }

  3. 调用方法
    public HSSFWorkbook getExc(String table,String[] zdmc,List list){

    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("sheet1");
    HSSFRow row = sheet.createRow(0);//创建第一行
    HSSFCell cell = row.createCell(0);//第一列
      for(int i=0;i<zdmc.length;i++){
       cell = row.createCell(i); //创建第一列
       cell.setCellValue(new HSSFRichTextString(zdmc[i]));
      }
    
    if(list!=null&&list.size()>0){
        for (int i = 0; i < list.size(); i++) {
            Object[] obj= (Object[]) list.get(i);
           row=sheet.createRow(i+1);//创建第i+1行                                     
            for(int j=0;j<obj.length;j++){
                cell=row.createCell(j);//创建第一列
                if(obj[j]!=null){
                    cell.setCellValue(obj[j].toString());  
                }else{
                    cell.setCellValue("");
                }      
            }                  
        }
    
    }
    
    return workbook;
    

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值