贴easyExcel github地址源码例子上边都有:https://github.com/alibaba/easyexcel
最新pom地址:https://mvnrepository.com/artifact/com.alibaba/easyexcel
简单使用:
先是项目中引入:
<!-- 阿里easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>1.1.2-beat1</version>
</dependency>
封装utils:
/**
* @Auther: liuhy
* @Date: 2019/7/25 16:46
*/
@Slf4j
public class ExcelUtil {
/**
* 无module excel读数据量少于1千行数据
* @param inputStream
* @param headNum 表头行数(例:两行合并一行的算两行)从1开始.
* @return
*/
public static List<List<String>> simpleReadListString(InputStream inputStream,Integer headNum){
try {
List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(1, headNum));
List<List<String>> collect = data.stream().map(k -> (List<String>) k).collect(Collectors.toList());
return collect;
} catch (Exception e) {
log.error("excel导入失败",e);
} finally {
try {
inputStream.close();
} catch (IOException e) {
log.error("excel导入失败",e);
}
}
return null;
}
/**
* 无module excel读数据量少于1千行数据
* @param inputStream
* @param headNum 表头行数(例:两行合并一行的算两行)
* @param module Module类对象
* @return
*/
public static List<Object> simpleReadJavaModel(InputStream inputStream,Integer headNum,Class module){
try {
List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(1, headNum, module));
return data;
} catch (Exception e) {
log.error("excel导入失败",e);
} finally {
try {
inputStream.close();
} catch (IOException e) {
log.error("excel导入失败",e);
}
}
return null;
}
}
目前只封装了,读excel,分为module,和无module.如上.
module实例:
@Data
public class ReadModel2 extends BaseRowModel {
@ExcelProperty(index = 0)
private String str;
@ExcelProperty(index = 1)
private Float ff;
@ExcelProperty(index = 2)
private Integer mm;
@ExcelProperty(index = 3)
private BigDecimal money;
@ExcelProperty(index = 4)
private Long times;
@ExcelProperty(index = 5)
private Double activityCode;
@ExcelProperty(index = 6,format = "yyyy-MM-dd")
private Date date;
}
注意: @ExcelProperty(index = 0)注解用来匹配,excel列与属性.index表示excel表格中列下标.
github上原文:@ExcelProperty(index = 3)数字代表该字段与excel对应列号做映射,也可以采用 @ExcelProperty(value = {"一级表头","二级表头"})用于解决不确切知道excel第几列和该字段映射,位置不固定,但表头的内容知道的情况。
不知道为何,原文中写的,读取的时候并不好使.亲测.可能后续版本会好吧.