下载poi-3.6-20091214.jar。下载地址例如以下:
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(); } }