EXCEL 資料匯出
request.setAttribute("ContentType", "text/xml;charset=utf-8"); String parklotsName = request.getParameter("parklotsName"); parklotsName=new String(parklotsName.getBytes("iso8859-1"),"UTF-8"); String inTime = request.getParameter("inTime"); String toTime=request.getParameter("toTime"); String payType = request.getParameter("payType"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Long time = 0L; Long endToTime=0L; //截止時間 try { if(!"".equals(inTime)) { time=sdf.parse(inTime).getTime();//86400000 } if(!"".equals(toTime)){ endToTime=sdf.parse(toTime).getTime()+86400000; }else{ endToTime = time+86400000; } } catch (ParseException e1) { e1.printStackTrace(); } pdaOrderMng service = new pdaOrderMng(); List<PdaOrder> orderList = new ArrayList<PdaOrder>(); orderList=service.queryPdaOrder(parklotsName,time,endToTime,payType); GetAppUserMng mng = new GetAppUserMng(); HSSFWorkbook wb = new HSSFWorkbook(); // 2.在workbook中新增一個sheet,對應Excel中的一個sheet HSSFSheet sheet = wb.createSheet("交易明細"); // 3.在sheet中新增表頭第0行,老版本poi對excel行數列數有限制short HSSFRow row = sheet.createRow((int) 0); // 4.建立單元格,設定值表頭,設定表頭居中 HSSFCellStyle style = wb.createCellStyle(); // 居中格式 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); row.setRowStyle(style); // 設定表頭 HSSFCell cell = row.createCell(0); cell.setCellValue("#"); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue("訂單編號"); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue("停車場名稱"); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue("泊位編號"); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue("車牌號"); cell.setCellStyle(style); cell = row.createCell(5); cell.setCellValue("進入時間"); cell.setCellStyle(style); cell = row.createCell(6); cell.setCellValue("離開時間"); cell.setCellStyle(style); cell = row.createCell(7); cell.setCellValue("支付方式"); cell.setCellStyle(style); cell = row.createCell(8); cell.setCellValue("應收金額"); cell.setCellStyle(style); cell = row.createCell(9); cell.setCellValue("實收金額"); cell.setCellStyle(style); cell = row.createCell(10); cell.setCellValue("員工編號"); cell.setCellStyle(style); int i=0; for(PdaOrder bean:orderList){ row = sheet.createRow((int) i + 1); row.createCell(0).setCellValue(++i); row.createCell(1).setCellValue(bean.getOrderNo()); row.createCell(2).setCellValue(bean.getParklots()); row.createCell(3).setCellValue(bean.getBoWei()); row.createCell(4).setCellValue(bean.getCarNo()); row.createCell(5).setCellValue(bean.getStartTime()); row.createCell(6).setCellValue(bean.getEndTime()); if(bean.getTrade_way()==null){ row.createCell(7).setCellValue( ""); }else if(bean.getTrade_way().equals("0")){ row.createCell(7).setCellValue("支付寶"); }else if(bean.getTrade_way().equals("2")){ row.createCell(7).setCellValue( "微信"); }else if(bean.getTrade_way().equals("3")){ row.createCell(7).setCellValue("現金繳費"); }else if(bean.getTrade_way().equals("4")){ row.createCell(7).setCellValue("琴島通卡"); }else if(bean.getTrade_way().equals("5")){ row.createCell(7).setCellValue( "逃單"); }else if(bean.getTrade_way().equals("6")){ row.createCell(7).setCellValue("免費停車"); }else if(bean.getTrade_way().equals("7")){ row.createCell(7).setCellValue( "微信APP"); } row.createCell(8).setCellValue(bean.getMoney()); row.createCell(9).setCellValue(bean.getMoney()); row.createCell(10).setCellValue(bean.getUserNO()); } Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");//設定日期格式 String fileName = "交易明細"+sf.format(date); ByteArrayOutputStream os = new ByteArrayOutputStream(); wb.write(os); byte[] content = os.toByteArray(); InputStream is = new ByteArrayInputStream(content); // 設定response引數,可以開啟下載頁面 response.reset(); response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1")); ServletOutputStream out = response.getOutputStream(); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(is); bos = new BufferedOutputStream(out); byte[] buff = new byte[2048]; int bytesRead; // Simple read/write loop. while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { if (bis != null) bis.close(); if (bos != null) bos.close(); }