EXCEL 后台导出

来源:互联网 发布:暮光女 出柜 知乎 编辑:程序博客网 时间:2024/05/17 15:41
    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();          }
原创粉丝点击