场景:
在日常工作中,excel导入导出,是十分常见的,有两种主流的技术,一种是jxl,另一种是poi,而easypoi就是对poi进行了封装,使得导入导出变得更加的简单,阿里巴巴也有封装的工具名叫EasyExcel有兴趣的小伙伴可以研究一下,这里讲我比较喜欢的easypoi。
easypoi不适用于那种花里胡哨的excel表格。
maven导包:
pom文件配置如下:
cn.afterturn
easypoi-base
3.2.0
cn.afterturn
easypoi-web
3.2.0
cn.afterturn
easypoi-annotation
3.2.0
封装的easypoi工具类:
package com.meeno.trainsys.util.easypoi.utils;
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;
/**
* @description: easypoiUtils
* @author: Wzq
* @create: 2019-11-08 14:54
*/
public class ExcelUtiles {
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
excel对应模板的java实体类:
package com.meeno.trainsys.util.easypoi.model;
import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;
/**
* @description: 用户ExcelModel
* @author: Wzq
* @create: 2019-11-08 14:56
*/
@Data
public class UserExcelModel {
@Excel(name = "序号",orderNum = "0")
private String number;
@Excel(name = "名称",orderNum = "1")
private String name;
@Excel(name = "电话",orderNum = "2")
private String phone;
}
excel文件:
导入excel:
List userExcelModels = ExcelUtiles.importExcel("E:\\test\\2.xlsx", 0, 1, UserExcelModel.class);
System.out.println(userExcelModels.toString());
内置导出工具方法,傻瓜式调用即可!
ExcelUtiles.exportExce(...);
技术支持:高岳峰,闫宇峰,施凯雷,杨珂
他的个人博客地址:http://120.78.93.197/
这是我的公众号 有最新的it咨询,和个人工作的记录:
这是我的个人微信遇到问题欢迎,提问:
最后加上高质量的淘宝店:如有质量问题随时滴滴我,童叟无欺!