使用easypoi导入导出excel,SSM和SpringBoot通用代码
easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员
就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板
语言(熟悉的表达式语法),完成以前复杂的写法
官网
功能介绍
Excel自适应xls和xlsx两种格式,word只支持docx模式
1.Excel导入
注解导入
Map导入
大数据量导入sax模式
导入文件保存
文件校验
字段校验
2.Excel导出
注解导出
模板导出
html导出
3.Excel转html
4.word导出
5.pdf导出
使用
如果你使用的Maven就引入以下坐标
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
SpringBoot
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.0.0</version>
</dependency>
如果使用的普通项目,搜索easypoi下载对应jar
创建工具类 FileUtil
package com.zzidc.manager.util;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
/**
* @Author: haohaowang
* @Desc
* @Date: 2019/3/25 10:26
*/
public class FileUtil {
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,boolean isCreateHeader, HttpServletResponse response){
ExportParams exportParams = new ExportParams(title, sheetName);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName, HttpServletResponse response){
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
}
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){
defaultExport(list, fileName, response);
}
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
if (workbook != null);
downLoadExcel(fileName, response, workbook);
}
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
} catch (IOException e) {
// throw new NormalException(e.getMessage());
}
}
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
if (workbook != null);
downLoadExcel(fileName, response, workbook);
}
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass){
if (StringUtils.isBlank(filePath)){
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
}catch (NoSuchElementException e){
// throw new NormalException("模板不能为空");
} catch (Exception e) {
e.printStackTrace();
// throw new NormalException(e.getMessage());
}
return list;
}
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){
if (file == null){
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
}catch (NoSuchElementException e){
//throw new NormalException("excel文件不能为空");
} catch (Exception e) {
// throw new NormalException(e.getMessage());
}
return list;
}
public static <T> List<T> importExcels(MultipartFile file, Class<T> pojoClass){
if (file == null){
return null;
}
ImportParams params = new ImportParams();
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
}catch (NoSuchElementException e){
//throw new NormalException("excel文件不能为空");
} catch (Exception e) {
// throw new NormalException(e.getMessage());
}
return list;
}
}
创建实体 Site
package com.zzidc.manager.dao.bean;
import cn.afterturn.easypoi.excel.annotation.Excel;
import java.util.Date;
public class Site {
private String id;
//name 列名,支持name_id,导入导出excel时的表头的名字
//orderNum 列的排序,支持name_id
//type 导出类型 1 是文本 2 是图片,3 是函数,10 是数字 默认是文本
@Excel(name = "城市名称", orderNum = "0",type=10)
private String cityName;
private String cityType;
@Excel(name = "负责人", orderNum = "0")
private String sitePri;
@Excel(name = "电话", orderNum = "0")
private String phone;
@Excel(name = "办公电话", orderNum = "0")
private String workPhone;
@Excel(name = "邮箱", orderNum = "0")
private String email;
private Date createTime;
@Excel(name = "状态", replace = {"正常_true", "不可用_false"})
private Boolean state;
private Date updateTime;
private String cityCode;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id == null ? null : id.trim();
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName == null ? null : cityName.trim();
}
public String getCityType() {
return cityType;
}
public void setCityType(String cityType) {
this.cityType = cityType == null ? null : cityType.trim();
}
public String getSitePri() {
return sitePri;
}
public void setSitePri(String sitePri) {
this.sitePri = sitePri == null ? null : sitePri.trim();
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
public String getWorkPhone() {
return workPhone;
}
public void setWorkPhone(String workPhone) {
this.workPhone = workPhone == null ? null : workPhone.trim();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Boolean getState() {
return state;
}
public void setState(Boolean state) {
this.state = state;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode == null ? null : cityCode.trim();
}
}
Controller层代码
@RequestMapping("export")
public void export(HttpServletResponse response){
//模拟从数据库获取需要导出的数
ResultUtil sites = siteService.getSites(1, 8, new Site());
List data = (List) sites.getData();
//导出操作 title带表的是表头就是第一行是标题,而不是表头。sheetName指的多sheet时的名字,设置为Null显示为sheet0,
FileUtil.exportExcel(data,null,null,Site.class,"测站信息.xls",true,response);
}
@RequestMapping("importExcel")
public void importExcel(MultipartFile file){
//解析excel,titleRows指的是上面设置的title如果上面设置了,就要写成1代表第一行是标题,
//写成0代表没有标题headerRows代表表头,当没有标题的时候这个数值设置成1代表第一行是表头,数据在第二行
List<Site> siteList = FileUtil.importExcel(file,0,1,Site.class);
for (Site site : siteList) {
//设置id
site.setId(UUIDGenerator.generate());
}
siteService.batSite(siteList);
//TODO 保存数据库
}
页面代码
使用的是layui
<div class="layui-inline">
<a class="layui-btn layui-btn-normal" href="/export">导出</a>
</div>
<button type="button" class="layui-btn" id="uploadExcel"><i class="layui-icon"></i>导入</button>
//js
upload.render({
elem: '#uploadExcel'
,url: '/importExcel'
,accept: 'file' //普通文件
,multiple: true
,done: function(res){
console.log(res);
}
});
注解具体可用,如下
正文到此结束(点击广告是对作者最大的支持)