java利用EasyPoi实现Excel导出功能
easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 语言(熟悉的表达式语法),完成以前复杂的写法。
具体实现步骤:
1、Maven中添加依赖
<!--easypoi导出excel--> <!--easypoi-base 导入导出的工具包,可以完成Excel导出,导入,Word的导出,Excel的导出功能--> <dependency> <groupId>org.jeecg</groupId> <artifactId>easypoi-base</artifactId> <version>${easypoi-base.version}</version> </dependency> <!--easypoi-web 耦合了spring-mvc 基于AbstractView,极大的简化spring-mvc下的导出功能--> <dependency> <groupId>org.jeecg</groupId> <artifactId>easypoi-web</artifactId> <version>${easypoi-web.version}</version> </dependency> <!--easypoi-annotation 基础注解包,作用与实体对象上,拆分后方便maven多工程的依赖管理--> <dependency> <groupId>org.jeecg</groupId> <artifactId>easypoi-annotation</artifactId> <version>${easypoi-annotation.version}</version> </dependency>
2、POJO中添加注解
/** * <p> * 管理员表 * </p> * * @Excel所含的参数: * name:导入导出字段名称,比如: name = "学生姓名" * replace :替换值,比如:replace = {"男_1","女_2"} * width :宽度,比如:width = 30,默认10 * height :高度(一个设置全局生效), height = 20 ,默认10 * orderNum :排序,默认0 * format :时间格式化 * type :导出字段类型导出类型 1 是文本 2 是图片,3是函数默认是文本,默认1 * imageType :图片类型, 导出类型 1 从file读取 2 是从数据库中读取,默认1 * savePath :图片保存路径,默认upload * */ @ExcelTarget("user") public class User extends Model<User> { private static final long serialVersionUID = 1L; /** * 主键id */ @TableId(value="id", type= IdType.AUTO) private Integer id; /** * 头像 */ private String avatar; /** * 账号 */ @Excel(name = "账号", orderNum = "1", mergeVertical = true, isImportField = "account") private String account; /** * 密码 */ private String password; /** * md5密码盐 */ private String salt; /** * 名字 */ @Excel(name = "姓名", orderNum = "2", mergeVertical = true, isImportField = "name") private String name; /** * 生日 */ private Date birthday; /** * 性别(1:男 2:女) */ private Integer sex; /** * 电子邮件 */ @Excel(name = "邮箱", orderNum = "6", mergeVertical = true, isImportField = "email", width = 30) private String email; /** * 电话 */ @Excel(name = "电话", orderNum = "7", mergeVertical = true, isImportField = "phone",width = 30) private String phone; /** * 角色id */ private String roleid; /** * 部门id */ private Integer deptid; /** * 状态(1:启用 2:冻结 3:删除) */ private Integer status; /** * 创建时间 */ @Excel(name = "创建时间", orderNum = "8", mergeVertical = true, isImportField = "createtime" ,format="yyyy-MM-dd hh:mm:ss",width = 30) private Date createtime; /** * 保留字段 */ private Integer version; /** * 用于EXCEL导出的相关字段,与Warpper中翻译字段对应 * transient:去序列化,防止将这些字段加入到sql语句中导致sql报错 */ @Excel(name = "性别", orderNum = "3", mergeVertical = true, isImportField = "sexName") private transient String sexName; @Excel(name = "角色", orderNum = "4", mergeVertical = true, isImportField = "roleName" , width = 20) private transient String roleName; @Excel(name = "部门", orderNum = "5", mergeVertical = true, isImportField = "deptName") private transient String deptName; @Excel(name = "状态", orderNum = "9", mergeVertical = true, isImportField = "statusName") private transient String statusName; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAvatar() { return avatar; } public void setAvatar(String avatar) { this.avatar = avatar; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSalt() { return salt; } public void setSalt(String salt) { this.salt = salt; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getRoleid() { return roleid; } public void setRoleid(String roleid) { this.roleid = roleid; } public Integer getDeptid() { return deptid; } public void setDeptid(Integer deptid) { this.deptid = deptid; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public Integer getVersion() { return version; } public void setVersion(Integer version) { this.version = version; } @Override protected Serializable pkVal() { return this.id; } public String getSexName() { return sexName; } public void setSexName(String sexName) { this.sexName = sexName; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public String getStatusName() { return statusName; } public void setStatusName(String statusName) { this.statusName = statusName; } }
3、Controller中请求
// 下载execl文档 @RequestMapping("/downloadExcel") public void download(HttpServletRequest request, HttpServletResponse response) throws Exception { // 告诉浏览器用什么软件可以打开此文件 response.setHeader("content-Type", "application/vnd.ms-excel"); // 下载文件的默认名称 response.setHeader("Content-Disposition", "attachment;filename=" + new String("用户数据表".getBytes("gbk"), "iso8859-1")+".xls"); //编码 response.setCharacterEncoding("UTF-8"); List<User> list = userService.findAll(); Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), User.class, list); workbook.write(response.getOutputStream()); }
4、前端页面添加Button
<input type="button" value="导出为Excel" onclick="window.open('/downloadExcel');"/>
导出功能实现!