使用EasyExcel读取文件并输出

使用EasyExcel读取文件并输出

1.maven导坐标


            com.alibaba
            easyexcel
            3.1.1
        

2.写实体

package com.leo23.entity;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ColumnWidth(value = 20)
@ContentRowHeight(30)
public class Student {
    @ExcelProperty(value = "名字", index = 1)
    private String name;
    @ExcelProperty(value = "性别", index = 2)
    private String gender;
    @ExcelProperty(value = "生日", index = 3)
    @DateTimeFormat("yyyy-MM-dd")
    private Date birthday;
    @ExcelProperty(value = "序号", index = 0)
    private String id;
}

  1. 编写测试用例
@Test
    void test03() {
        String template = "读取文件.xlsx";
        //工作薄对象
        ExcelWriter workBook = EasyExcel.write("输出文件.xlsx").withTemplate(template).build();
        //工作区对象
        WriteSheet sheet = EasyExcel.writerSheet().build();

        List students = initData();

        // 插入时候换行
        FillConfig build = FillConfig.builder().forceNewRow(true).build();

        Map map = new HashMap<>();
        //填充单个数据
        map.put("factory", "工厂名称");
        map.put("contact", "联系人");
        map.put("phone", "183xxx");
        map.put("address", "地址");

        workBook.fill(map, build, sheet);
        //使用工作薄对象填充数据
        workBook.fill(students, build, sheet);
        workBook.finish();

    }

    private static List initData() {
        ArrayList students = new ArrayList<>();
        Student student = new Student();
        for (int i = 0; i < 3; i++) {
            student.setId(String.valueOf(i));
            student.setName("学号00" + i);
            student.setBirthday(new Date());
            student.setGender("男");
            students.add(student);
        }
        return students;
    }

你可能感兴趣的:(使用EasyExcel读取文件并输出)