struts2开发导出excel表格功能

struts2中有对导出excel表格的支持,所以开发起来比较容易,主要的步骤有三点:

1.配置文件:

   <action name="excel" class="userAction" method="excel" >
         <result name="excel" type="stream">

               <!-- 注意这里的ContentType -->
               <param name="contentType">application/vnd.ms-excel</param> 

                <!-- 这里需要和Action里的变量名一致 -->  

               <param name="inputName">excelStream</param>                                     

               <param name="contentDisposition">filename="user.xls"</param>
               <param name="bufferSize">1024</param>
          </result>
   </action>

2.action中的函数为excel如下:

   public String excel() throws Exception {
        StringBuffer excelBuf = new StringBuffer();
        excelBuf.append("编号").append("\t").append("登录名称").append("\t").append("联系人").append("\t").append("电话").append("\t").append("Email").append("\t").append("所属单位").append("\n");
        List list1 = mobjUserService.findAllUser();
        for (int i = 0; i < list1.size(); i++) {
         User user = (User) list1.get(i);
         excelBuf.append(user.getUserId()).append("\t").append(user.getLoginName()).append("\t").append(user.getContactPerson()).append("\t").append(user.getPhone()).append("\t").append(user.getEmail()).append("\t").append(user.getRecommendCompany().getCompanyName()).append("\n");
  }
        //excelBuf.append("Thinking in Java").append("\t").append("2001").append("\t").append("Eckel").append("\n");
        //excelBuf.append("Spring in action").append("\t").append("2005").append("\t").append("Rod").append("\n");
        String excelString = excelBuf.toString();
        //logger.debug("result excel String: " + excelString);
        excelStream = new ByteArrayInputStream(excelString.getBytes(), 0, excelString.length());
        return "excel";
    }

3.jsp中对应:

      <td width="10%">
      <input name="Submit" type="button" class="button"
      onClick="javaScript:sysReturn('<c:out value="${ctx}"/>/excel.action');"
      value="<s:text name="导出excel"/>">&nbsp;
     </td>

  比较简单好用就不详细描述了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值