用户信息批量导出至EXCEL
说明:批量导出信息就是将页面查询到的结果,写出到EXCEL文件中。
操作步骤:
一、预备工作
0、引入包
XLS文件解析:poi-3.7-20101029.jar
1、建表users
create table users(
id int primary key auto_increment,
name varchar(20) not null,
password varchar(32) not null,
sex varchar(2) not null,
birthday date not null,
hobby varchar(50) not null,
telephone varchar(15) not null,
address varchar(50) not null,
type int not null -- 1 admin 2 common user
);
插入测试数据:
insert into users values(1,'admin','202CB962AC59075B964B07152D234B70','男',
'1980-1-1','体育,旅游','13953311099','山东淄博',1);
insert into users values(2,'abc','202CB962AC59075B964B07152D234B70','女',
'1990-1-1','体育,音乐','1895330000','山东济南',2);
…………
效果如下:
2、建立相应的JavaBean:
package cn.sdut.po;
public class User {
private int id;
private String name;
private String password;
private String sex;
private String birthday;
private String hobby;
private String telephone;
private String address;
private int type;
public User() {
super();
}
public User(int id, String name, String password, String sex,
String birthday, String hobby, String telephone, String address,
int type) {
super();
this.id = id;
this.name = name;
this.password = password;
this.sex = sex;
this.birthday = birthday;
this.hobby = hobby;
this.telephone = telephone;
this.address = address;
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password
+ ", sex=" + sex + ", birthday=" + birthday + ", hobby="
+ hobby + ", telephone=" + telephone + ", address=" + address
+ ", type=" + type + "]";
}
}
二、查询用户信息并写出至EXCEL文件中
1、准备前端页面——userMain.jsp,内容如下。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
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%>">
<title>My JSP 'userMain.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script src="js/calendar.js"></script>
<script>
function selectUser()
{
form1.action="<%=path%>/UserServlet?method=bycond";
form1.submit();
}
function exportexcel()
{
form1.action="<%=path%>/UserServlet?method=exportExcel";
form1.submit();
}
</script>
</head>
<body>
<center>
<h1>用 户 管 理</h1>
</center>
<br>
<form action="" method="post" name="form1">
<p>
查询条件: <input type="text" name="condition"