用java实现Excel导入导出(easyExcel)

前几个月系统主要功能完结撒花,只剩下后台系统的报表。为满足客户的需求,我需要将用户给的excel表数据导入到系统中,开始我没有怎么关注的这个业务,我却花费了很久很久的时间。客户的需求一直在变,excel表格也一直在变。我原本使用的是poi,从一开始的莫名其妙的报错,数据类型的转换错误,到各种的异常抓取,我的代码也越来越长。这时候我寻找到了一个很方便且简单的工具**easyExcel**。

测试前准备

首先映入excel工具的maven依赖

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.0.1</version>
        </dependency>

这里先准备下实体对象方便后面操作,这里写两个字节码去接受,是我自己的使用感受。导出的数据是从系统里来,我们能保持系统内的数据的正确性,但是要导入的数据是来自用户,我们不能保证其正确性,所以统一拿字符串接受,这里就不会存在数字等转换异常了。

//导出使用的实体
@Data
public class Demo {
    @ExcelProperty(value = "用户名")
    private String username;
    @ExcelProperty(value = "密码")
    private String password;
    @ExcelProperty(value = "年龄")
    private Integer age;
    @ExcelProperty(value = "性别")
    private String gender;
}

//导入使用的实体
@Data
public class Demo {
    private String username;
    private String password;
    private String age;
    private String gender;
}

一、Excel导入

//    普通导入
    public static void main(String[] args) throws FileNotFoundException {
        List<Demo> ls = EasyExcel.read(new FileInputStream("./demo.xlsx"), Demo.class, new SyncReadListener()).sheet(0).doReadSync();
        for (Demo l : ls) {
            //操作数据
            System.out.println(l);
        }
    }

二、Excel导出

1、普通导出

    //普通导出
    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<Demo> ls = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Demo demo = new Demo();
            demo.setUsername("name"+i);
            demo.setPassword("password"+i);
            demo.setAge(i);
            demo.setGender((i%10==0)?"男":"女");

            ls.add(demo);
        }
        EasyExcel.write(new FileOutputStream("./demo.xlsx"),Demo.class).sheet(0).doWrite(ls);
    }

2、模板导出

这里注意以下操作后面的方法,withTemplate代表着使用模板,needHead代表是否需要表头,useDefaultStyle意思为是否使用默认样式,也就是灰底白字的样式。

//    模板导出
        public static void main(String[] args) throws FileNotFoundException {
        ArrayList<Demo> ls = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Demo demo = new Demo();
            demo.setUsername("name"+i);
            demo.setPassword("password"+i);
            demo.setAge(i);
            demo.setGender((i%10==0)?"男":"女");
            ls.add(demo);
        }
        EasyExcel.write(new FileOutputStream("./demo1.xlsx"),Demo.class).withTemplate("./demo.xlsx").needHead(false).useDefaultStyle(false).sheet(0).doWrite(ls);
    }

三、拓展提高

​ 这里主要介绍下,通过该工具的其他注解,来操作excel的样式!

@HeadRowHeight   //设置表头高度
@ContentRowHeight  //设置内容高度
@ColumnWidth   //设置列宽
@HeadFontStyle  //设置表头字体样式
@HeadStyle    //设置表头表格样式
@ContentStyle    //设置内容表格样式
@ContentFontStyle    //设置内容字体样式

@DateTimeFormate  //时间格式转换

跨行表头如下

@Data
public class ComplexHeadData {
    @ExcelProperty({"主标题", "字符串标题"})
    private String string;
    @ExcelProperty({"主标题", "日期标题"})
    private Date date;
    @ExcelProperty({"主标题", "数字标题"})
    private Double doubleData;
}

//显示类似于
//    |             主标题              |
//    |--------------------------------|
//    |字符串标题 |  日期标题  |  数字标题  |  

以上功能相比能解决你遇到的许多的问题。如果不能的话,请点击官网参考了解吧。

posted @ 2022-02-22 20:58  站在巨人肩上的人  阅读(2615)  评论(0编辑  收藏  举报