easypoi 导入excel
官方文档:http://doc.wupaas.com/docs/easypoi/easypoi-1c0u9d9lcruu2
maven
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.2.0</version>
</dependency>`
工具类 (这里注意 TITLE_ROWS 以及HEAD_ROWS 代表你的标题和表头分别占用了多少行单元格)
public class ExcelUtils {
private static ExcelUtils excelUtils;
//-----------------------导入---------------------
//表格标题行数,默认0
private static Integer TITLE_ROWS;
//表头行数,默认1
private static Integer HEAD_ROWS;
private static boolean HEED_VERIFY = true;
public ExcelUtils() {
}
public static ExcelUtils getExcelUtils() {
return ExcelUtils.excelUtilsModel(0, 1, null);
}
/**
* 获取导入工具对象
*
* @param titleRows
* @param headRows
* @return
*/
public static ExcelUtils getExcelUtils(Integer titleRows, Integer headRows) {
return ExcelUtils.excelUtilsModel(titleRows, headRows, null);
}
public static ExcelUtils getExcelUtils(Integer titleRows, Integer headRows, Boolean needVerify) {
return ExcelUtils.excelUtilsModel(titleRows, headRows, needVerify);
}
private static ExcelUtils excelUtilsModel(Integer titleRows, Integer headRows, Boolean needVerify) {
synchronized (ExcelUtils.class) {
if (null != titleRows) {
TITLE_ROWS = titleRows;
}
if (null != headRows) {
HEAD_ROWS = headRows;
}
if (null != needVerify) {
HEED_VERIFY = needVerify;
}
if (null == excelUtils) {
return new ExcelUtils();
}
}
return excelUtils;
}
/**
* 导入
*
* @param file 文件
* @param tClass 需要转换的类
* @param <T>
* @return
* @throws Exception
*/
public <T> List<T> importExcel(MultipartFile file, Class<T> tClass) throws Exception {
if (file == null) {
throw new BusinessException("上传文件不能为空");
}
ImportParams importParams = new ImportParams();
// 数据处理
//表格标题行数,默认0
importParams.setTitleRows(TITLE_ROWS);
//表头行数,默认1
importParams.setHeadRows(HEAD_ROWS);
//是否需要校验上传的Excel,默认false
importParams.setNeedVerify(HEED_VERIFY);
ExcelImportResult<T> resultModel = ExcelImportUtil.importExcelMore(file.getInputStream(), tClass, importParams);
if (null == resultModel)
throw new RuntimeException("Excel读取失败");
return resultModel.getList();
}
//-----------------导出-------------------------
private static String fileName;
private static HttpServletResponse response;
/**
* 导出设置文件名称
*
* @param name 文件名称
* @param httpServletResponse
* @return
*/
public static ExcelUtils getExcelUtils(String name, HttpServletResponse httpServletResponse) {
fileName = name;
response = httpServletResponse;
return ExcelUtils.excelUtilsModel(null, null, null);
}
private void responseInit() {
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("utf-8");
try {
response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".xls");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public void excelOut(ExportParams exportParams, List<?> dataList, Class<?> tClass) throws Exception {
this.responseInit();
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, tClass, dataList);
setExportExcelFormat(workbook);
}
/**
* excel导出时
* 返回头设置下载,并设置文件名
* 注意:以下设置编码格式是为了ie,当前项目是ie下使用的。360或者谷歌可能会文件名会乱码。根据自己需要调整编码。或者不用设置这么多,直接outStream输出得了。
*
* @param workbook
* @throws Exception
*/
private void setExportExcelFormat(Workbook workbook) throws Exception {
ServletOutputStream outStream = null;
try {
outStream = response.getOutputStream();
workbook.write(outStream);
} finally {
outStream.close();
}
}
Excel实体类(注意这里注解中的值对应 Excel表格中列名)
/**
* 几个参数,
* name :表格标头
* orderNum :排序
* groupName :合并分组名称
* replace :配置字段结果映射,格式为 {"映射后内容1_字段值1", "映射后内容2_字段值2", "映射后内容3_字段值3"})
*/
@Data
public class ExcelOrganDto implements Serializable {
private static final long serialVersionUID = 7451809392043549314L;
@Excel(name = "所属城市", orderNum = "10")
private String city;
@Excel(name = "所属区县", orderNum = "20")
private String county;
@Excel(name = "儿童姓名", orderNum = "60", groupName = "基本情况")
private String name;
@Excel(name = "儿童性别", orderNum = "70", groupName = "基本情况")
private String sex;
@Excel(name = "儿童目前由谁照看(看护人)", orderNum = "310", groupName = "基本情况试题", replace = {"父母_0", "祖父母/外祖父母_1", "其他_2"})
private String question1Basic;
@Excel(name = "您孩子是否为早产儿?", orderNum = "320", groupName = "基本情况试题", replace = {"足月儿_0", "早产儿_1", "不清楚_2"})
private String question2Basic;
调用样例
MultipartFile file = null;
//返回的为读取到的信息 这里注意ExcelOrganDto 中的注解
//这里.getExcelUtils(1, 1) 中 参数为标题以及列名所在的行数 这里根据表格自动调整
List<ExcelOrganDto> resultList = ExcelUtils.getExcelUtils(1, 1).importExcel(file, ExcelOrganDto.class);