//将jsp页面上的列表导出到Excel
//首先来看第一个jsp,也就是要导出Excel的那个jsp
<%@ page language="java" contentType="text/html; import="java.util.*" charset=UTF-8" pageEncoding="UTF-8"%>
<%
String basePath = request.getContextPath();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<SCRIPT language="javascript">
function showView(button){
}
function sendUrgency(workitemId) {
}
function init() {
}
function exportExcel() {//导出Excel的js
var win = window.open(window.location.href + "&export=true");
window.setTimeout(function() {
},1000);
}
</SCRIPT>
<title>人员信息</title>
</head>
<body οnlοad="init();">
// 下面是一个打印按纽,就可以将本页面的列表打印出来 -->
<input type="button" οnclick="window.print()" value="打印"/>
// 下面这个按纽就是将本页面的列表导出到一个Excel上面去 -->
<input type="button" οnclick="exportExcel();" value="导出到Excel"/>
<TABLE id=tblSort width="100%" border="1" cellpadding="3" cellspacing="0" borderColor="#cccccc" borderColorDark="white" align=center>
<tr bgcolor="#c6c6c6" class="popupmenu_option" align="center">
<th width="13%" align="center" valign="middle">
<span>姓名 </span>
</th>
<th width="10%" align="center">
<span>年龄 </span>
</th>
<th width="10%" align="center" style="cursor: pointer">
<span>地址 </span>
</th>
<th width="12%" align="center" style="cursor: pointer">
<span>电话 </span>
</th>
<th width="12%" align="center" style="cursor: pointer">
<span>出生日期/span>
</th>
<th width="12%" align="center" valign="middle">
<span>所在部门 </span>
</th>
</tr>
<tbody>
// 这里面就是一些数据啦 这里也就不再写出来啦 -->
</tbody>
</table>
</body>
</html>
//通过上面页面的exportExcel()方法我们又要转到进入这个页面的action
//在action里面加上下面的代码足可
if (null != export && "true".equals(export)) {
return new ActionForward("/workflow/exportshowWorkFlowComment.jsp");
} else {
return mapping.findForward("success");
}
//可以看到如果export传过来的值是true的话我们就去真正导出的那个页面exportComment.jsp
//其实要导出的jsp和上面的jsp几乎是一摸一样,只不过有少许地方做了改变
//下面的一行代码加入了contentType="application/vnd.ms-excel; charset=UTF-8",这就是导出Excel的关键所在,当然换一下后缀名的话也可以导出到word等
<%@ page language="java" contentType="application/vnd.ms-excel; charset=UTF-8" import="java.util.*" pageEncoding="UTF-8"%>
<%
String basePath = request.getContextPath();
//这里下面加入了两行代码,还包括要导出的Excel的名字
String mayUrgent = "1";
response.setHeader("Content-Disposition","attachment;filename=userInfo.xls");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<SCRIPT language="javascript">
function showView(button){
}
function sendUrgency(workitemId) {
}
function init() {
}
</SCRIPT>
<title>人员信息</title>
</head>
<body οnlοad="init();">
<TABLE id=tblSort width="100%" border="1" cellpadding="3" cellspacing="0" borderColor="#cccccc" borderColorDark="white" align=center>
<tr bgcolor="#c6c6c6" class="popupmenu_option" align="center">
<th width="13%" align="center" valign="middle">
<span>姓名 </span>
</th>
<th width="10%" align="center">
<span>年龄 </span>
</th>
<th width="10%" align="center" style="cursor: pointer">
<span>地址 </span>
</th>
<th width="12%" align="center" style="cursor: pointer">
<span>电话 </span>
</th>
<th width="12%" align="center" style="cursor: pointer">
<span>出生日期/span>
</th>
<th width="12%" align="center" valign="middle">
<span>所在部门 </span>
</th>
</tr>
<tbody>
<!-- 这里面就是一些数据啦 这里也就不再写出来啦 -->
</tbody>
</table>
</body>
</html>
//好了 这样一个把jsp页面的列表导出到Excel的功能就差不多已经实现了