1. pom.xml SSM搭建的dependency不贴了
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
2.JSP 片段 <!-- 就是这么个意思,发送请求export-->
<a href=${pageContext.request.contextPath }/export>导出用户信息</a>
3.controller Usercontroller
/**
* 写一个导出excel的功能
*/
@RequestMapping("/export")
@ResponseBody
public void export(HttpServletResponse response) {
response.setContentType("application/binary;charset=UTF-8");
String[] titles = { "用户编号", "用户姓名", "用户密码", "用户邮箱","用户手机" };
userService.export(titles);
}
4.service UserService
void export(String[] titles);//导出excel
5.serviceImpl UserServiceImpl
// 页面展示excel或者是导出excel
public void export(String[] titles) {
try {
// 创建excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
// 建立新的sheet对象
HSSFSheet hssfSheet = workbook.createSheet("sheet1");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow hssfRow = hssfSheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
// 居中样式
hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell hssfCell = null;
for (int i = 0; i < titles.length; i++) {
hssfCell = hssfRow.createCell(i);// 列索引从0开始
hssfCell.setCellValue(titles[i]);// 列名1
hssfCell.setCellStyle(hssfCellStyle);// 列居中显示
}
// 第五步,写入实体数据
List<User> users = userDao.queryUser();
if (users != null && !users.isEmpty()) {
for (int i = 0; i < users.size(); i++) {
hssfRow = hssfSheet.createRow(i + 1);
User user = users.get(i);
// 第六步,创建单元格,并设置值
int userid = 0;
if (user.getId() != 0) {
userid = user.getId();
}
hssfRow.createCell(0).setCellValue(userid);
String userName = "";
if (user.getUserName() != null) {
System.out.println(user.getUserName());
userName = user.getUserName();
}
hssfRow.createCell(1).setCellValue(userName);
String password = "";
if (user.getPassword() != null) {
password = user.getPassword();
}
hssfRow.createCell(2).setCellValue(password);
String email = "";
if (user.getEmail() != null) {
email = user.getEmail();
}
hssfRow.createCell(3).setCellValue(email);
int phoneNumber = 0;
if (user.getPhoneNumber() != 0) {
phoneNumber = user.getPhoneNumber();
}
hssfRow.createCell(4).setCellValue(phoneNumber);
}
}
// 第七步,将文件输出到客户端浏览器 || 使用下载
try {
FileOutputStream fileOutputStream = new FileOutputStream("d:\\2.xls");// 指定路径与名字和格式
workbook.write(fileOutputStream);// 将数据写出去
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
6.Dao接口 UserDao
List <User> queryUser();//查询用户
7.xxxMapper.xml 自己写个名字就行了 demo:UserMapper.xml
<resultMap id="BaseResultMap" type="com.cn.ning.pojo.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="email" property="email" jdbcType="VARCHAR" />
<result column="phoneNumber" property="phoneNumber" jdbcType="INTEGER" />
</resultMap>
<!-- 查询 用户信息 -->
<select id="queryUser" resultMap="BaseResultMap">
select
id,
user_name as userName,
password,
email,
phoneNumber
from
user_t
</select>
看个人心情,查询的字段可以抽出来写到<sql></sql>中,然后使用的时候<include>过来就行了
resultMap和resultType的区别请自行百度。
8这个建表语句 贴出来吧 也不费事
CREATE TABLE user_t(
id int(11) NOT NULL AUTO_INCREMENT,
user_name varchar(40) NOT NULL,
password varchar(255) NOT NULL,
email varchar(40) NOT NULL,
phoneNumber int(50) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
insert 略
9.用户的实体类
public class User {
private Integer id;
private String userName;
private String password;
private String email;
private Integer phoneNumber;
get set方法略
不足:不足的地方就是生成的excel单元格之间因为数据量有的长的比如密码我用的MD5加盐存储,所以长,就是生成的excel的单元格格式有待调整。
good luck!