阿里easyExcel简单应用

贴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第几列和该字段映射,位置不固定,但表头的内容知道的情况。

不知道为何,原文中写的,读取的时候并不好使.亲测.可能后续版本会好吧.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值