用户信息批量导出至EXCEL

该博客介绍了如何使用Java批量导出用户信息到EXCEL文件,包括引入POI库,建立users表并插入测试数据,创建JavaBean,设计前端查询页面,实现查询数据的Servlet及导出功能。
摘要由CSDN通过智能技术生成

用户信息批量导出至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" 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值