easyExcel 比poi 更加简单的 excel 解析工具

 

EasyExcel 写 excel 文件

 

easyexcel 文档地址: https://easyexcel.opensource.alibaba.com/ 

 

1 excel 的简单解析  

  ExcelRow 是自定义的用于接受一行数据的类,对应这一行数据的没一个字段或者说列

    @Test
    public void read()  {
        List<ExcelRow> list  = new ArrayList<>();

        EasyExcel.read(new File("C:\\Users\\ZHANGYUKUN\\Desktop\\excel文件.xlsx"),ExcelRow.class, new ReadListener<ExcelRow>() {
            @Override
            public void invoke(ExcelRow o, AnalysisContext analysisContext) {
                list.add(o);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }).headRowNumber(0).doReadAll();

        System.out.println(JSONObject.toJSONString( list ));

    }

  

 

 

2 excel 的简单导出

 

    /**
     * 写无头的文件
     */
    @Test
    public void write() {
        ExcelWriterSheetBuilder sheet = EasyExcel.write(new File("C:\\Users\\ZHANGYUKUN\\Desktop\\excel文件.xlsx")).sheet();
        sheet.doWrite( getData() );
    }

  

 

    /**
     * 写有头的文件
     */
    @Test
    public void write2()  {
        //ExcelRow 字段默认作为 head的 名字
        ExcelWriterSheetBuilder sheet = EasyExcel.write(new File("C:\\Users\\ZHANGYUKUN\\Desktop\\excel文件.xlsx"),ExcelRow.class).sheet();
        sheet.doWrite( getData() );
    }

  

 

3 excel 的填充

  1 单行填充

    使用 {变量名字}

  2 多行填充

    垂直填充:FillConfig.builder().direction(WriteDirectionEnum.VERTICAL) 默认

    水平填充: FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL)

     使用 {.变量名字}

  3 复合填充

    强制换行:FillConfig.builder().forceNewRow(true).build() 

    /**
     * 写有头的文件
     */
    @Test
    public void write3() throws FileNotFoundException {
        File file = ResourceUtils.getFile("classpath:easyExcelTemple.xlsx");

        //ExcelRow 字段默认作为 head的 名字
        ExcelWriter excelWriter = EasyExcel.write( new File("C:\\Users\\ZHANGYUKUN\\Desktop\\excel文件.xlsx") ).withTemplate( file ).build();
        WriteSheet writeSheet = EasyExcel.writerSheet(0).build();

        HashMap<String, String> map = new HashMap<>();
        map.put("avg","avg80");
        map.put("total","total99");

        excelWriter.fill( getData(), FillConfig.builder().forceNewRow(true).build()  ,writeSheet );
        excelWriter.fill( map ,writeSheet );
        excelWriter.finish();
        
    }

 

数据:

    public List<ExcelRow> getData(){
        List<ExcelRow> list = new ArrayList<>();

        ExcelRow excelRow = new ExcelRow();
        excelRow.setId("1");
        excelRow.setName("name1");
        excelRow.setAge("age1");
        excelRow.setHeadImage("headImage1");
        list.add(excelRow);

        ExcelRow excelRow2 = new ExcelRow();
        excelRow2.setId("1");
        excelRow2.setName("name1");
        excelRow2.setAge("age1");
        excelRow2.setHeadImage("headImage1");
        list.add(excelRow2);


        ExcelRow excelRow3 = new ExcelRow();
        excelRow3.setId("1");
        excelRow3.setName("name1");
        excelRow3.setAge("age1");
        excelRow3.setHeadImage("headImage1");
        list.add(excelRow3);

        return list;
    }

  

模型:

public class ExcelRow {

    @ExcelProperty(index = 0,value="Id")
    private String id;

    @ExcelProperty(index = 1,value="名字")
    private String name;

    @ExcelProperty(index = 2,value="年龄")
    private String age;

    @ExcelProperty(index = 3,value="头像")
    private String headImage;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getHeadImage() {
        return headImage;
    }

    public void setHeadImage(String headImage) {
        this.headImage = headImage;
    }
}

  

 

 

excel模板样式:

 

 

 

 

4 单元格合并和样式修改

 

 

 

 

 

POI 读写 excel文件

  1 三个版本的excel 文件对象

    03,

    07,

    07优化版

 

  2 读取

 

 

  3 写入

 

 

  4 excel样式修改

 

  

 

posted on 2022-08-02 14:31  zhangyukun  阅读(439)  评论(0编辑  收藏  举报

导航