【JSP页面导出数据到Excel表格】
jsp导出Excel的核心部分代码是:
response.setContentType("application/msexcel");
response.setHeader("Content-Disposition", "inline;
filename="+new String("员工基本信息表.xls".getBytes("gb2312"), "ISO8859-1"));
1、效果图
导出到Excel
2、源代码部分
1)VO部分
public class UserVO {
private String id;
private String name;
private int age;
private String address;
public UserVO(){};
public UserVO(String id, String name, int age, String address) {
super();
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
}
2)列表list部分
<%@ page language="java" import="java.util.*,vo.UserVO" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style>
table{
border: solid 1px #B4B4B4;
border-collapse: collapse; --折叠样式.
}
tr th{
background:url("../../images/gray/bg_table_th.gif") repeat;
padding-left:4px;
height:27px;
border: solid 1px #B4B4B4;
}
tr td{
height:25px;
padding:4px;
border: solid 1px #B4B4B4;
}
</style>
</head>
<body>
<table>
<caption style="bold;">员工基本信息表</caption>
<a href="<%=basePath%>servlet/UserListServlet?type=export">导出数据</a>
<tr>
<td style="solid">id</td>
<td>name</td>
<td>Age</td>
<td>Address</td>
</tr>
<%
List<vo.UserVO> list = (List<vo.UserVO>)request.getAttribute("list");
//out.println(list);
for(vo.UserVO tl:list) {%>
<tr>
<td><%=tl.getId() %></td>
<td><%=tl.getName() %></td>
<td><%=tl.getAge() %></td>
<td><%=tl.getAddress() %></td>
</tr>
<%}%>
</table>
</body>
</html>
3)导出部分
<%@ page language="java" import="java.util.*,vo.UserVO" pageEncoding="UTF-8" contentType="application/msexcel" %>
<%
response.setContentType("application/msexcel");
response.setHeader("Content-Disposition", "inline; filename="+new String("员工基本信息表.xls".getBytes("gb2312"), "ISO8859-1"));
%>
<html>
<head>
<base href="<%=basePath%>">
<style>
table, th, td {
border: 1px solid #000;
}
td {
text-align: center;
vertical-align: middle;
}
/*为了解决导出身份证时候,显示科学记数形式*/
td {
mso-number-format: '\@';
}
</style>
</head>
<body width="100%">
<table border=2 width="70%">
<caption style="bold;">员工基本信息表</caption>
<tr bgcolor="lightgreen">
<td >id</td>
<td>name</td>
<td>Age</td>
<td>Address</td>
</tr>
<%
List<vo.UserVO> list = (List<vo.UserVO>)request.getAttribute("list");
//out.println(list);
for(vo.UserVO tl:list) {%>
<tr>
<td><%=tl.getId() %></td>
<td><%=tl.getName() %></td>
<td style="vnd.ms-excel.numberformat:@" nowrap><%=tl.getAge() %></td>
<td><%=tl.getAddress() %></td>
</tr>
<%}%>
</table>
</body>
</html>
4)Servlet控制部分
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<UserVO> list = new ArrayList<UserVO>();
for(int i=0;i<15;i++){
list.add( new UserVO("00"+i, "gaojigsong", i, "广东深圳"+i));
}
String type = request.getParameter("type");
if("list".equals(type)){
//list
System.out.println(list);
request.setAttribute("list", list);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}else{
//export
request.setAttribute("list", list);
request.getRequestDispatcher("/export.jsp").forward(request, response);
}
}