论坛首页 Java企业应用论坛

java导出excel

浏览 4175 次
锁定老帖子 主题:java导出excel
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-12-13   最后修改:2008-12-13

-----项目中可选择导出哪些字段的导出方法

public void toexcel(HashMap hashmap,HttpServletRequest request,HttpServletResponse response) throws Exception{
  String par=request.getParameter("cloumn");
  String[] pars=par.split(",");
  int filei=0;
  String fileName = System.getProperty("java.io.tmpdir")+"\\xls"+(new java.util.Date().getTime())+(filei++);
  jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(fileName));
  jxl.write.WritableSheet ws = wwb.createSheet("Sheet 1", 0);//sheet1是导出excel分页第1页
     for(int i=0;i<pars.length;i++){
  ws.addCell(new jxl.write.Label(i, 0, pars[i]));//导出后excel中的字段名称,i是列,第一行
     ws.setColumnView(i, 20);
     }
     QfcxActionDao qfcxdao=new QfcxActionDao();
  List list=qfcxdao.creatitems(hashmap,request);
  int j=1;
     for(int i=0;i<list.size();i++){
      QfcxActionPo qfcxactionpo=(QfcxActionPo)list.get(i);
      int h=0;
      for(int k=0;k<pars.length;k++){
       if(pars[k].equals("抄表号")){//判断那些数据需要导出
        ws.addCell(new jxl.write.Label(h,j,qfcxactionpo.getChaobiaohao()));
       }
       else if(pars[k].equals("用户号")){
        ws.addCell(new jxl.write.Label(h,j,qfcxactionpo.getYonghuhao()));
       }
       else if(pars[k].equals("用户名称")){
        ws.addCell(new jxl.write.Label(h,j,qfcxactionpo.getUsername()));
       }
       else if(pars[k].equals("装裱地址")){
        ws.addCell(new jxl.write.Label(h,j,qfcxactionpo.getZhuangbiaodizhi()));
       }
       else if(pars[k].equals("表身号")){
        ws.addCell(new jxl.write.Label(h,j,qfcxactionpo.getBiaoshenghao())); 
       }
       else if(pars[k].equals("收费方式")){
        ws.addCell(new jxl.write.Label(h,j,qfcxactionpo.getShoufeifangshi())); 
       }
       else if(pars[k].equals("收费周期")){
        ws.addCell(new jxl.write.Label(h,j,qfcxactionpo.getShoufeizhouqi()));
       }
       else if(pars[k].equals("用户状态")){
        ws.addCell(new jxl.write.Label(h,j,qfcxactionpo.getUserstate()));
       }
       else if(pars[k].equals("生成日期")){
        ws.addCell(new jxl.write.Label(h,j,qfcxactionpo.getShengchengriqi()));
       }
       else if(pars[k].equals("欠费金额")){
        ws.addCell(new jxl.write.Label(h,j,qfcxactionpo.getQianfeijine().toString()));
       }
       else if(pars[k].equals("水量")){
        ws.addCell(new jxl.write.Label(h,j,qfcxactionpo.getShuiliang().toString()));
       }
       else if(pars[k].equals("期数")){
        ws.addCell(new jxl.write.Label(h,j,qfcxactionpo.getQishu().toString()));
       }
       else if(pars[k].equals("联系人")){
        ws.addCell(new jxl.write.Label(h,j,qfcxactionpo.getLinkman()));
       } 
       else if(pars[k].equals("联系电话")){
        ws.addCell(new jxl.write.Label(h,j,qfcxactionpo.getLinkphonto()));
       }
       else if(pars[k].equals("移动电话")){
        ws.addCell(new jxl.write.Label(h,j,qfcxactionpo.getLinkmanmobil()));
       }
       h++;
      }
         j++;
     }
     wwb.write();
     wwb.close();
     response.reset();

     response.setContentType("application/x-msdownload;charset=GBK"); //设置响应类型为下载
     response.setCharacterEncoding("UTF-8");
     String docName=java.net.URLEncoder.encode("欠费用户信息.xls","UTF-8");
     response.setHeader("Content-Disposition", "attachment; filename=" +new String(docName.getBytes("UTF-8"),"GBK"));
        BufferedInputStream br = new BufferedInputStream(new FileInputStream(fileName));
        byte[] buf = new byte[1024];
        int len = 0;
        OutputStream out = response.getOutputStream();
        while((len = br.read(buf)) >0)
         out.write(buf,0,len);
        out.close();
        br.close();
 }

  • jxl.jar (706.1 KB)
  • 下载次数: 379
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics