easyExcel的简单实用

依赖

>
            >org.apache.poi>
            >poi-ooxml>
            >3.17>
        >

        >
            >com.alibaba>
            >easyexcel>
            >2.1.1>
        >

1,读文件

通过ExcelListener监听器读文件

		String fileName = "F:\\out.xlsx";
        ExcelReaderBuilder read = EasyExcel.read(fileName, Excel.class, new ExcelListener());
        ExcelReaderSheetBuilder sheet = read.sheet();
        sheet.doRead();

ExcelListener代码如下:

public class ExcelListener extends AnalysisEventListener<Excel> {
    //一行一行读取
    @Override
    public void invoke(Excel excel, AnalysisContext analysisContext) {
        System.out.println(excel);
    }

    //完成之后做的事情
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
    //读取表头
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        super.invokeHeadMap(headMap, context);
        System.out.println("表头");
    }
}

easyExcel的简单实用_第1张图片

2,写文件

 //写操作
  		String fileName = "F:\\out.xlsx";
        List<Excel> list =new ArrayList<>();
        for (int i=0;i<5;i++){
            Excel excel = new Excel();
            excel.setId(i);
            excel.setName("xiaoming"+i);
            list.add(excel);
        }
        ExcelWriterSheetBuilder sheet = EasyExcel.write(fileName, Excel.class).sheet("学生列表");//sheet设置页码
        sheet.doWrite(list);

excel的实体类代码如下:

@Data
public class Excel  {
    @ExcelProperty(value = "id",index = 0)
    private Integer id;
    @ExcelProperty(value = "名称",index = 1)
    private String name;
}

你可能感兴趣的:(#,ssm)