您的位置:首页 > 其它

poi导出excel

2015-08-20 19:18 176 查看

public String getListExcel() throws Exception {

  HttpServletResponse response = ServletActionContext.getResponse();

  HttpServletRequest request = ServletActionContext.getRequest();

  authenticationCount = new AuthenticationCount();

  authenticationCount.setStaticsIndex(request.getParameter("staticsIndex"));

  try {

   List<AuthenticationCount> authList = (List<AuthenticationCount>) getWhiteListsMap()

     .get("dataList");

   if (authList != null && authList.size() > 0) {

    // 第一步,创建一个webbook,对应一个Excel文件

    HSSFWorkbook wb = new HSSFWorkbook();

    // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet

    HSSFSheet sheet = wb.createSheet("导出表");

    sheet.setDefaultColumnWidth(40); 

    sheet.setDefaultRowHeightInPoints(20); 

    // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short

    HSSFRow row = sheet.createRow((int) 0);

    // 第四步,创建单元格,并设置值表头 设置表头居中

    HSSFCellStyle style = wb.createCellStyle();

    style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

    HSSFCell cell = row.createCell((short) 0);

    cell.setCellValue("地市");

    cell.setCellStyle(style);

    cell = row.createCell((short) 1);

    cell.setCellValue("组名");

    cell.setCellStyle(style);

    cell = row.createCell((short) 2);

    cell.setCellValue("用户手机号");

    cell.setCellStyle(style);

    cell = row.createCell((short) 3);

    cell.setCellValue("统计结果");

    cell.setCellStyle(style);

    cell = row.createCell((short) 4);

    for (int i = 0; i < authList.size(); i++) {

     row = sheet.createRow((int) i + 1);

     AuthenticationCount authCount = (AuthenticationCount) authList

       .get(i);

     // 第四步,创建单元格,并设置值

     

     row.createCell(0).setCellValue(authCount.getCompany());

    

     row.createCell(1).setCellValue(authCount.getGroupName());

     

     row.createCell(2).setCellValue(authCount.getMobilePhone());

     

     row.createCell(3).setCellValue(authCount.getCount());

     

    }

   // response.addHeader("Content-Disposition", "attachment;filename=\"" + resourcefile.getName()); 

    response.setContentType("application/vnd.ms-excel");

    String dataTableName = DateUtils.format(new Date(),"yyyyMM")+"01"+new Date().getTime();

    response.addHeader("Content-disposition","attachment;filename=" + dataTableName + ".xls");

    OutputStream ouputStream = response.getOutputStream(); //new FileOutputStream("e:\\d.xls");

    wb.write(ouputStream);

    ouputStream.flush();

    ouputStream.close();

   }

  } catch (Exception e) {

   e.printStackTrace();

  }

  return null;

 }

 <script>

  var globalCxtPath = "<%=basePath%>";

  var staticIndex = $('')

  function exportExcel(){

      var staticsIndex = $('input[name="authenticationCount.staticsIndex"]:checked').val();

      alert(staticsIndex);

      var url = "portalStatics_getListExcel.action?staticsIndex="+staticsIndex;

      window.location.href = globalCxtPath+url; 

     

  }

 </script>

所用包 poi-3.13-beta1-20150723.jar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: