Chinaunix首页 | 论坛 | 博客
  • 博客访问: 176541
  • 博文数量: 33
  • 博客积分: 761
  • 博客等级: 上士
  • 技术积分: 364
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-15 23:56
个人简介

梦想主义者

文章分类

全部博文(33)

文章存档

2012年(33)

分类: 系统运维

2012-02-03 08:38:29

首先在页面上设置导出excel的按钮,代码如下:
  1. <input type="button" value=" 导 出 excel " class="button" onFocus="this.blur();" onclick="toExcel()">

  2. //导出excel
  3.     function toExcel(){
  4.         
  5.         location.href = "<%=request.getContextPath()%>/checkedman.do?operFlag=export";

  6.     }
此处使用的是struts1,在action中处理业务。具体代码如下:
  1. //此处为判断转向
  2. if("export".equals(operFlag)) {
  3.             forward = exportData(request, response);
  4. //下面是exportData方法
  5. public String exportData(HttpServletRequest request,
  6.             HttpServletResponse response) {
  7.         //通过BO层找到获取查询的list
  8.         ArrayList<CheckedmanPO> list = CheckedmanBO.exportExcel(request, response);
  9.         
  10.         if (list == null || list.size() <= 0) {

  11.         } else {
  12.             //HttpServletResponse response = FacesUtils.getServletResponse();
  13.             String msg = "";
  14.             response.reset();
  15.             OutputStream os = null;
  16.             try {
  17.                 os = response.getOutputStream();

  18.             } catch (IOException e1) {
  19.                 msg = e1.getMessage();
  20.             }

  21.             response.setContentType("application/vnd.ms-excel;charset=utf-8");
  22.             response.setCharacterEncoding("utf-8");

  23.             try {
  24.                 response.setHeader("Content-disposition",
  25.                         "attachment; filename="
  26.                                 + new String(
  27.                                         "受检者信息.xls".getBytes(),
  28.                                         "ISO8859_1"));
  29.             } catch (Exception e1) {
  30.                 // TODO 自动生成 catch 块
  31.                 e1.printStackTrace();
  32.             }

  33.             HSSFWorkbook wb = new HSSFWorkbook();
  34.             HSSFSheet hs = wb.createSheet();
  35.             
  36.                     
  37.             HSSFRow firstRow = hs.createRow(0);
  38.             
  39.             HSSFCell firstCell0 = firstRow.createCell((short) 0);
  40.             HSSFCell firstCell1 = firstRow.createCell((short) 1);
  41.             HSSFCell firstCell2 = firstRow.createCell((short) 2);
  42.             HSSFCell firstCell3 = firstRow.createCell((short) 3);
  43.             HSSFCell firstCell4 = firstRow.createCell((short) 4);
  44.             HSSFCell firstCell5 = firstRow.createCell((short) 5);
  45.             HSSFCell firstCell6 = firstRow.createCell((short) 6);
  46.             HSSFCell firstCell7 = firstRow.createCell((short) 7);
  47.             HSSFCell firstCell8 = firstRow.createCell((short) 8);
  48.             HSSFCell firstCell9 = firstRow.createCell((short) 9);
  49.             HSSFCell firstCell10 = firstRow.createCell((short) 10);
  50.             HSSFCell firstCell11 = firstRow.createCell((short) 11);
  51.             HSSFCell firstCell12 = firstRow.createCell((short) 12);
  52.             //以下代码处理数据的中文乱码
  53.             firstCell0.setEncoding(HSSFCell.ENCODING_UTF_16);
  54.             firstCell1.setEncoding(HSSFCell.ENCODING_UTF_16);
  55.             firstCell2.setEncoding(HSSFCell.ENCODING_UTF_16);
  56.             firstCell3.setEncoding(HSSFCell.ENCODING_UTF_16);
  57.             firstCell4.setEncoding(HSSFCell.ENCODING_UTF_16);
  58.             firstCell5.setEncoding(HSSFCell.ENCODING_UTF_16);
  59.             firstCell6.setEncoding(HSSFCell.ENCODING_UTF_16);
  60.             firstCell7.setEncoding(HSSFCell.ENCODING_UTF_16);
  61.             firstCell8.setEncoding(HSSFCell.ENCODING_UTF_16);
  62.             firstCell9.setEncoding(HSSFCell.ENCODING_UTF_16);
  63.             firstCell10.setEncoding(HSSFCell.ENCODING_UTF_16);
  64.             firstCell11.setEncoding(HSSFCell.ENCODING_UTF_16);
  65.             firstCell12.setEncoding(HSSFCell.ENCODING_UTF_16);
  66.         
  67.             firstCell0.setCellValue("客户姓名");
  68.             firstCell1.setCellValue("性别");
  69.             firstCell2.setCellValue("民族");
  70.             firstCell3.setCellValue("证件类别");
  71.             firstCell4.setCellValue("证件号码");
  72.             firstCell5.setCellValue("出生日期");
  73.             firstCell6.setCellValue("婚姻状况");
  74.             firstCell7.setCellValue("手机号码");
  75.             firstCell8.setCellValue("家族病史");
  76.             firstCell9.setCellValue("个人病史");
  77.             firstCell10.setCellValue("药物过敏史");
  78.             firstCell11.setCellValue("主要体质");
  79.             firstCell12.setCellValue("辨识日期");


  80.             for (int i = 0; i < list.size() ; i++) {
  81.                 HSSFRow oneRow = hs.createRow(i+1);
  82.                 HSSFCell cell0 = oneRow.createCell((short) 0);
  83.                 HSSFCell cell1 = oneRow.createCell((short) 1);
  84.                 HSSFCell cell2 = oneRow.createCell((short) 2);
  85.                 HSSFCell cell3 = oneRow.createCell((short) 3);
  86.                 HSSFCell cell4 = oneRow.createCell((short) 4);
  87.                 HSSFCell cell5 = oneRow.createCell((short) 5);
  88.                 HSSFCell cell6 = oneRow.createCell((short) 6);
  89.                 HSSFCell cell7 = oneRow.createCell((short) 7);
  90.                 HSSFCell cell8 = oneRow.createCell((short) 8);
  91.                 HSSFCell cell9 = oneRow.createCell((short) 9);
  92.                 HSSFCell cell10 = oneRow.createCell((short) 10);
  93.                 HSSFCell cell11 = oneRow.createCell((short) 11);
  94.                 HSSFCell cell12 = oneRow.createCell((short) 12);

  95.                 cell0.setEncoding(HSSFCell.ENCODING_UTF_16);
  96.                 cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
  97.                 cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
  98.                 cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
  99.                 cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
  100.                 cell5.setEncoding(HSSFCell.ENCODING_UTF_16);
  101.                 cell6.setEncoding(HSSFCell.ENCODING_UTF_16);
  102.                 cell7.setEncoding(HSSFCell.ENCODING_UTF_16);
  103.                 cell8.setEncoding(HSSFCell.ENCODING_UTF_16);
  104.                 cell9.setEncoding(HSSFCell.ENCODING_UTF_16);
  105.                 cell10.setEncoding(HSSFCell.ENCODING_UTF_16);
  106.                 cell11.setEncoding(HSSFCell.ENCODING_UTF_16);
  107.                 cell12.setEncoding(HSSFCell.ENCODING_UTF_16);


  108.                 CheckedmanPO po = (CheckedmanPO) list.get(i);

  109.                 cell0.setCellValue(po.getCName());
  110.                 cell1.setCellValue(po.get***());
  111.                 cell2.setCellValue(po.getNationName());
  112.                 cell3.setCellValue(po.getCCardTypeName());
  113.                 cell4.setCellValue(po.getUCardNum());
  114.                 cell5.setCellValue(po.getBirth());
  115.                 cell6.setCellValue(po.getMarriage());
  116.                 cell7.setCellValue(po.getMobileTel());
  117.                 cell8.setCellValue(po.getFamilyIllnessIdList());
  118.                 cell9.setCellValue(po.getIndividualIllnessIdList());
  119.                 cell10.setCellValue(po.getMedicineSensitiveIdList());
  120.                 cell11.setCellValue(po.getMainPhysical());
  121.                 cell12.setCellValue(po.getTestDate());
  122.             }
  123.             try {
  124.                 wb.write(os);
  125.                 os.flush();
  126.                 //FacesContext.getCurrentInstance().responseComplete();
  127.             } catch (IOException e) {
  128.                 msg = msg + e.getMessage();
  129.             }
  130.             if (!"".equals(msg)) {
  131.             }
  132.         }
  133.         return null;
  134.     }
写到此处,导出功能基本已完成。根据不同的情况,获取不同的数据封装进list就可以了。
关于导出excel,这里使用的是jxl。
阅读(1904) | 评论(0) | 转发(0) |
0

上一篇:网站小结

下一篇:通过js改变字体大小

给主人留下些什么吧!~~