下載並匯出資料到execl中,下載匯出資料execl

來源:互聯網
上載者:User

下載並匯出資料到execl中,下載匯出資料execl

下載poi-3.6-20091214.jar,如下:

http://download.csdn.net/detail/evangel_z/3895051

1.jsp

<button type="button" class="btn btn-mini" onClick="location.href='<%=basePath%>/bankcard/exportEffectThirdData?begintime=${begintime}&endtime=${endtime}&page=1'">匯出有效訂單</button>

2.後台代碼處理:

 controller 處理:

@Get("exportReturnThirdData")
 public  void  exportReturnThirdListData(Invocation inv,@Param("begintime") String startTime,
   @Param("endtime") String endTime){
  HttpServletResponse response = inv.getResponse();
  // response.setContentType("application/xls");
  response.setContentType("application/x-download");
  response.reset();
  response.setContentType("bin");
  String header = "attachment;filename=returnThirdData.xls";
  response.addHeader("Content-Disposition", header);

  String[] heads = { "訂單日期", "訂單號", "商品名稱", "商品屬性", "渠道名稱", "支付銀行卡","支付帳號", "成本價", "賣出價", "賣出收入", "發貨日期", "外訂單審核日期", "退貨日期", "取消日期" };
  List<FenqiGoodsOrder> returnOrders = fenqiGoodsOrderListService
    .exportReturnThirdList(startTime, endTime);
  String path = "";

  OrderDetailExportExeclUtil.exeportListData(heads, returnOrders,response);
 }

匯出到execl處理邏輯:

public static void  exeportListData(String[] heads,List<FenqiGoodsOrder> returnOrders,HttpServletResponse response){
  // 第一步,建立一個webbook,對應一個Excel檔案  
        HSSFWorkbook wb = new HSSFWorkbook(); 
       // 第二步,在webbook中添加一個sheet,對應Excel檔案中的sheet  
        HSSFSheet sheet = wb.createSheet("第三方退貨訂單明細"); 
       // 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制short  
        HSSFRow row = sheet.createRow((int) 0); 
     // 第四步,建立單元格,並設定值表頭 設定表頭置中  
     HSSFCellStyle style = wb.createCellStyle(); 
     style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 建立一個置中格式  

     HSSFCell cell=null;
         for(int h=0;h<heads.length;h++){
          cell = row.createCell((short) h); 
          cell.setCellValue(heads[h]); 
       cell.setCellStyle(style); 
         }
     // 第五步,寫入實體資料 實際應用中這些資料從資料庫得到,  
        for (int i = 0; i < returnOrders.size(); i++) 
       { 
            row = sheet.createRow((int) i + 1); 
            FenqiGoodsOrder goodsOrder = (FenqiGoodsOrder) returnOrders.get(i); 
           // 第四步,建立單元格,並設定值  
            row.createCell((short) 0).setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(goodsOrder.getCreateTime())); 
            row.createCell((short) 1).setCellValue(goodsOrder.getOrderNo()); 
            row.createCell((short) 2).setCellValue(goodsOrder.getGoodName()); 
            row.createCell((short) 3).setCellValue(goodsOrder.getGoodType());
            row.createCell((short) 4).setCellValue(goodsOrder.getChannelName()); 
            row.createCell((short) 5).setCellValue(goodsOrder.getCardNo()); 
            row.createCell((short) 6).setCellValue(goodsOrder.getAccountNo()); 
            row.createCell((short) 7).setCellValue(goodsOrder.getPurchasePrice());//成本價
            row.createCell((short) 8).setCellValue(goodsOrder.getSellPrice());//賣出價
            row.createCell((short) 9).setCellValue(goodsOrder.getSellEarning());//賣出收入 
            if(goodsOrder.getDeliveryTime() !=null && !goodsOrder.getDeliveryTime().equals("")){
             row.createCell((short) 10).setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(goodsOrder.getDeliveryTime())); 
            }
            if(goodsOrder.getAuditTime() !=null && !goodsOrder.getAuditTime().equals("")){
             row.createCell((short) 11).setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(goodsOrder.getAuditTime()));
            }
            if(goodsOrder.getReturnTime() !=null && !goodsOrder.getReturnTime().equals("")){
             row.createCell((short) 12).setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(goodsOrder.getReturnTime())); 
            }
            if(goodsOrder.getCancelDate() !=null && !goodsOrder.getCancelDate().equals("")){
             row.createCell((short) 13).setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(goodsOrder.getCancelDate()));
            }
         } 
         // 第六步,將檔案存到指定位置  
        try 
        { 
         wb.write(response.getOutputStream()); 
        } 
       catch (Exception e) 
       { 
            e.printStackTrace(); 
        } 
  
 }

 

 

 http://www.cnblogs.com/bmbm/archive/2011/12/08/2342261.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.