下载并导出数据到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 = { "訂單日期", "訂單號", "商品名稱", "商品屬性", "渠道名稱", "支付銀行卡","支付賬號", "成本價", "賣出價", "賣出收入", "發(fā)貨日期", "外訂單審核日期", "退貨日期", "取消日期" };
??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){
??// 第一步。創(chuàng)建一個webbook,相應一個Excel文件??
??????? HSSFWorkbook wb = new HSSFWorkbook();?
?????? // 第二步。在webbook中加入一個sheet,相應Excel文件里的sheet??
??????? HSSFSheet sheet = wb.createSheet("第三方退貨訂單明細");?
?????? // 第三步,在sheet中加入表頭第0行,注意老版本號poi對Excel的行數(shù)列數(shù)有限制short??
??????? HSSFRow row = sheet.createRow((int) 0);?
???? // 第四步。創(chuàng)建單元格,并設置值表頭 設置表頭居中??
???? HSSFCellStyle style = wb.createCellStyle();?
???? style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 創(chuàng)建一個居中格式??
???? HSSFCell cell=null;
???????? for(int h=0;h<heads.length;h++){
??????? ? cell = row.createCell((short) h);?
??????? ? cell.setCellValue(heads[h]);?
?????? cell.setCellStyle(style);?
???????? }
???? // 第五步,寫入實體數(shù)據(jù) 實際應用中這些數(shù)據(jù)從數(shù)據(jù)庫得到,??
??????? for (int i = 0; i < returnOrders.size(); i++)?
?????? {?
??????????? row = sheet.createRow((int) i + 1);?
??????????? FenqiGoodsOrder goodsOrder = (FenqiGoodsOrder) returnOrders.get(i);?
?????????? // 第四步,創(chuàng)建單元格,并設置值??
??????????? 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
總結
以上是生活随笔為你收集整理的下载并导出数据到execl中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 中查看内存的使用情况集经
- 下一篇: Linux的经常使用命令(1) - 指定