把jsp页面上的列表导出到Excel,并加打印功能

//将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的功能就差不多已经实现了

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值