java分页数据导出excel

来源:互联网 发布:晓风彩票系统源码 编辑:程序博客网 时间:2024/06/15 17:49
  1. ** 
  2.      * 订单导出(用于统计利润) 
  3.      * @return 
  4.      */  
  5.     public String orderExport() throws IOException{  
  6.         if (queryOrderList_currentPage == null || queryOrderList_currentPage <= 0) {  
  7.             queryOrderList_currentPage = 1;  
  8.         }  
  9.         OrderInfo order = new OrderInfo();  
  10.         if (!StringUtil.isNull(queryOrderList_orderStatus)) {  
  11.             order.setOrderStatus(queryOrderList_orderStatus);  
  12.         }  
  13.         if (!StringUtil.isNull(queryOrderList_orderCard)) {  
  14.             order.setOrderCard(queryOrderList_orderCard);  
  15.         }  
  16.         if (!StringUtil.isNull(queryOrderList_memberPhone)) {  
  17.             order.setMemberPhone(queryOrderList_memberPhone);  
  18.         }  
  19.         if (!StringUtil.isNull(queryOrderList_memberContactsPhone)) {  
  20.             order.setMemberContactsPhone(queryOrderList_memberContactsPhone);  
  21.         }  
  22.         if (queryOrderList_businessId != null && queryOrderList_businessId > 0) {  
  23.             order.setBusinessId(queryOrderList_businessId);  
  24.         }  
  25.         if (!StringUtil.isNull(queryOrderList_addressDesc)) {  
  26.             order.setAddressDesc(queryOrderList_addressDesc);  
  27.         }  
  28.           
  29.         if (!StringUtil.isNull(queryOrderList_beginTime) && !StringUtil.isNull(queryOrderList_endTime)) {  
  30.             order.setBeginTime(queryOrderList_beginTime);  
  31.             order.setEndTime(queryOrderList_endTime);  
  32.         }  
  33.           
  34.         try {  
  35.             HttpServletResponse response = ServletActionContext.getResponse();  
  36.             queryOrderList_pageBean = statisticsService.queryOrderListByCompanyConditions(order,queryOrderList_currentPage, queryOrderList_pageSize);  
  37.             int totalPage = queryOrderList_pageBean.getTotalPage();  
  38.             response.reset();   
  39.             SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");  
  40.             String fname= "订单导出"+sdFormat.format(new Date());  
  41.             fname=new String(fname.getBytes("GBK"),"ISO_8859_1");//文件名称{避免出现乱码}   
  42.             response.setHeader("Content-disposition""attachment; filename="+fname+".csv");// 设定输出文件头  
  43.             response.setContentType("text/csv");   
  44.             response.setCharacterEncoding("UTF-8");   
  45.             OutputStream out = response.getOutputStream();  
  46.             String sep = ",";   
  47.             //列名  
  48.             for(int column=0;column<CompanyConfig.ORDER_EXPORT_COLUMN.split(",").length;column++){  
  49.                 out.write(CompanyConfig.ORDER_EXPORT_COLUMN.split(",")[column].getBytes());   
  50.                 out.write(sep.getBytes());   
  51.             }  
  52.             //换行符  
  53.             out.write(System.getProperty("line.separator").getBytes());  
  54.             //数据  
  55.             OrderInfo newOrderInfo =new OrderInfo();  
  56.             if(queryOrderList_pageBean.getList()!=null && queryOrderList_pageBean.getList().size()>0){  
  57.                 for (int i = 0; i < queryOrderList_pageBean.getList().size(); i++) {  
  58.                     newOrderInfo=(OrderInfo)queryOrderList_pageBean.getList().get(i);  
  59.                     getOut(out,newOrderInfo,sep);  
  60.                 }  
  61.             }  
  62.             if(totalPage>1){  
  63.                 for (int m = 2; m <= totalPage; m++) {  
  64.                     queryOrderList_pageBean = statisticsService.queryOrderListByCompanyConditions(order,   
  65.                             queryOrderList_currentPage, queryOrderList_pageSize);  
  66.                     List list =queryOrderList_pageBean.getList();  
  67.                     if(list!=null && list.size()>0){  
  68.                         for (int j = 0; j < list.size(); j++){  
  69.                             OrderInfo nOrderInfo=(OrderInfo)list.get(j);  
  70.                             getOut(out, nOrderInfo, sep);  
  71.                         }   
  72.                     }  
  73.                 }  
  74.             }  
  75.             out.flush();   
  76.             out.close();  
  77.         } catch (Exception e) {  
  78.             e.printStackTrace();  
  79.         }  
  80.         return null;  
  81.     }  
  82.       
  83.     public void getOut(OutputStream out,OrderInfo newOrderInfo,String sep) throws IOException{  
  84.         if(!StringUtil.isNull(newOrderInfo.getOrderCard())){  
  85.             out.write(String.valueOf(newOrderInfo.getOrderCard()).getBytes());   
  86.         }else{  
  87.             out.write(String.valueOf("订单编号为空").getBytes());   
  88.         }  
  89.         out.write((sep).getBytes());  
  90.         if(!StringUtil.isNull(newOrderInfo.getMemberContactsPhone())){  
  91.             out.write(newOrderInfo.getMemberContactsPhone().getBytes());   
  92.         }else{  
  93.             out.write(String.valueOf("用户联系电话为空").getBytes());  
  94.         }  
  95.         out.write((sep).getBytes());   
  96.         if(newOrderInfo.getAddressDesc()!=null){  
  97.             out.write(newOrderInfo.getAddressDesc().getBytes());  
  98.         }else{  
  99.             out.write(String.valueOf("联系地址为空").getBytes());  
  100.         }  
  101.         out.write((sep).getBytes());   
  102.         if(newOrderInfo.getGenerateTime()!=null){  
  103.             out.write(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newOrderInfo.getGenerateTime()).getBytes());  
  104.         }else{  
  105.             out.write(String.valueOf("下单时间为空").getBytes());  
  106.         }  
  107.         out.write(System.getProperty("line.separator").getBytes());  
  108.     } 
0 0
原创粉丝点击