项目中经常遇到导出的页面,之前公司有封装好的方法可以调用
目前的公司没有封装的方法,完全凭借自己去发挥
网上可以搜索出很多的方法去实现
个人认为如下的方式比较方便快捷
具体做法如下,仅供参考:
1.在需要被导出的页面做个功能按钮,跳转到对应的Java类
2.将已经显示出来的数据的主键id拼接起来作为参数传到Java类里面
3.根据主键查询出数据列表,这个操作会影响到效率问题,因为重新请求了数据库
4.查询出来的结果,return到一个页面
页面头的写法就是导出的方法:
<%@ page contentType="application/msexcel;charset=GBK" %>
<%
response.setHeader("Content-disposition","attachment; filename=download.xls");
response.setHeader("Pragma", "");
response.setHeader("Cache-Control", "");
%>
注意:filename不能是中文
页面头的下方正常写需要导出那个页面的代码
例如:
<%@ page contentType="application/msexcel;charset=GBK" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
response.setHeader("Content-disposition","attachment; filename=download.xls");
response.setHeader("Pragma", "");
response.setHeader("Cache-Control", "");
%>
<!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<link rel="StyleSheet" href="<s:url value='/skin!loadCss.do?id=blue'/>" type="text/css" />
<script type="text/javascript" src="<s:url value='/skin!loadJs.do?id=tdcolor'/>"></script>
</head>
<body>
<table>
<tr>
<th nowrap>产品名称</th>
<th nowrap>生产企业</th>
<th nowrap>注销日期</th>
</tr>
<s:iterator value="qbpropr.data">
<tr>
<td align="center"><s:property value="genericName" /></td>
<td align="center"><s:property value="entName" /></td>
<td><s:date name="proStatusDate" format="yyyy-MM-dd" /></td>
</tr>
</s:iterator>
</table>
</body>
</html>