基于struts2及poi的导出功能

前端页面:

<input type="button" id="btnExp" class="grn_btn_l" value="导出"/>

$("#btnExp").click(function(){
    location.href="/system/black!expBlackLists.action";
   });

action的配置:

<action name="black" class="com.yicomm.system.web.action.blackListAction">
      <result name="resultList">/system/blacklist/blacklist_index.jsp</result>
      <result name ="editSuc">/system/blacklist/blacklist_edit.jsp</result>
      <result name ="blackInfo">/system/blacklist/blacklist_edit.jsp</result>
      <result name="delSuc" type="json"></result>
      <result name="impSuc">/system/blacklist/blacklist_import.jsp</result>
      <result name="expSuc" type="stream">
       <param name="contentType">application/vnd.ms-excel</param>
       <param name="contentDisposition">filename="blackLists.xls"</param>
       <param name="inputName">downloadFile</param>
       <param name="bufferSize">4096</param>
      </result>
     </action>

后台action的实现:

// 信息导出
 public String expBlackLists(){
  
  return "expSuc";
 }
 // 信息下载
 public InputStream getDownloadFile() throws IOException{
  Workbook book = new HSSFWorkbook();
  Sheet sheet = book.createSheet("黑名单信息");
  Row row = sheet.createRow(0);
  row.createCell(0).setCellValue("序号");
  row.createCell(1).setCellValue("类型");
  row.createCell(2).setCellValue("昵称");
  row.createCell(3).setCellValue("备注");
  row.createCell(4).setCellValue("创建日期");
  CellStyle sty = book.createCellStyle();
  List<Blacklist> list = EJBFactory4Sys.getBlacklistFacadeLocal().findByProperty(null, null);
  for(int i = 1;i<=list.size(); i++){
   Blacklist blacklist = list.get(i-1);
   row = sheet.createRow(i);
   row.createCell(0).setCellValue(i);
   if(blacklist.getType().equals("00")){
    row.createCell(1).setCellValue("千牛");
   }
   else if(blacklist.getType().equals("01")){
    row.createCell(1).setCellValue("ISV");
   }
   else if(blacklist.getType().equals("02")){
    row.createCell(1).setCellValue("用户");
   }
   else{
    row.createCell(1).setCellValue("其他");
   }
   row.createCell(2).setCellValue(blacklist.getNick());
   row.createCell(3).setCellValue(blacklist.getRemark());
   DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   row.createCell(4).setCellValue(format.format(blacklist.getOpDate()));
  }
  File file = new File("export.xls");
  OutputStream os =new FileOutputStream(file);
  book.write(os);
  os.close();
  InputStream is = new FileInputStream(file);
  return is;
 }

 

你可能感兴趣的:(struts2,poi)