导出excel (返回页面方式)

说明:不需要poi直接导出需要数据为 Excel (ssh框架实例)

  • struts2配置文件:
    <!-- 导出功能 -->
            <action name="downLoadDetail" class="employeeAction" method="downLoadDetail">
                <result name="success">/pages/sales/feeManage/AgentDowloadDetail.jsp</result>
            </action>

     

  • Action代码:
    public String downLoadDetail() {
    //导出表格名称: String fileNameTemp
    = "导出明细"; try {
    //转码(避免中文乱码) String fileName
    = java.net.URLEncoder.encode(fileNameTemp, "UTF-8"); getRequest().setAttribute("fileName", fileName);
    //查询需要导出的数据: List
    <PrpCommission> prpComList = employeeService.findPrpCommission(downAgentcode, paramYearMonth); ActionContext.getContext().put("prpComList", prpComList); } catch (Exception e) { e.printStackTrace(); throw new BusinessException("导出时发生异常", false); } return SUCCESS; }

     

  • jsp页面:
    <%@page contentType="text/html;charset=UTF-8"%>
    <html>
    <head>
        <title>导出</title>
    /* 重点:加如该头文件 */ <% response.reset(); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", request.getAttribute("fileName").toString()+".xls")); %> </head> <body id="all_title"> <s:form action="" namespace="" method="post"> <table border="1" cellpadding="2" cellspacing="0"> <tr align="center"> <td>***</td> <td>***</td> <td>***</td> <td>***</td> <td>***</td> <td>***</td> <td>***</td> <td>***</td> <td>***</td> <td>***</td> <td>***</td> </tr> <s:iterator value="prpComList" id="commission"> <tr class="sort" align="center"> <td><s:property value="#commission.prpSmain.policyNo"/></td> <td><s:property value="#commission.realpolicyid"/></td> <td><s:property value="#commission.prpJpaymentBill.commNo"/></td> <td><s:property value="#commission.companyCode"/></td> <td><s:property value="#commission.companyName"/></td> <td><s:property value="#commission.comCode"/></td> <td><s:property value="#commission.agentCode"/></td> <td><s:property value="#commission.agentName"/></td> <td><s:date name="#commission.generateTime" format="yyyy-MM-dd"/></td> <td><s:property value="#commission.termNum"/></td> <td><s:property value="#commission.agentfee"/></td> </tr> </s:iterator> </table> </s:form> </body> </html>

     

 

posted on 2017-05-10 17:06  forever_2h  阅读(518)  评论(0编辑  收藏  举报

导航