EasyExcel
EasyExcel是阿里巴巴基于Java语言的读写Excel文件的开源项目.
EasyExcel导入的依赖
导入依赖所添加的jar包
EasyExcel对文件的操作
新建Java常规类,写Excel文件,是根据该类对象的数据写文件,读文件,是将文件数据生成的该类的对象
本例题,是Student类
以下代码使用的注解:
@ColumnWidth(所有列宽)
@HeadRowHeight(表头高)
@ContentRowHeight(行高)
@ContentFontStyle(color = 10)
10是颜色的枚举值 表示红色 内容文本颜色
@ExcelProperty("Excel表中间的表头列名")
属性的注解
@ExcelIgnore //忽略该列 该列数据不写往Excel文件 属性注解
package com.wanmait.demo; import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ContentFontStyle; import com.alibaba.excel.annotation.write.style.ContentRowHeight; //@ColumnWidth(所有列宽) //@HeadRowHeight(表头高) //@ContentRowHeight(行高) @ContentRowHeight(40) @ContentFontStyle(color = 10) public class Student { //@ExcelProperty("Excel表中间的列名") @ExcelProperty("姓名") private String name; @ExcelProperty("年龄") private int age; //@ColumnWidth(列宽) @ColumnWidth(15) private String sex; @ExcelIgnore //忽略该列 private double height; public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return name+":"+age+":"+sex; } }
写文件
ExcelOpr类 write方法
Student.java代码
package com.wanmait.demo; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.read.builder.ExcelReaderBuilder; import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder; import com.alibaba.excel.write.builder.ExcelWriterBuilder; import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; import java.util.List; public class ExcelOpr { public void write(String filename, List<Student> studentList) { ExcelWriterBuilder builder = EasyExcel.write(filename,Student.class); //针对Excel创建工作簿 ExcelWriterSheetBuilder sheetBuilder = builder.sheet(); //builder.sheet("表名") //工作簿创建sheet表 sheetBuilder.doWrite(studentList); //数据写往工作簿 对应的文件 } }
Test类调用write方法写文件:
List<Student> studentList = new ArrayList<>(); Student s1 = new Student(); s1.setName("张三"); s1.setAge(20); s1.setSex("男"); s1.setHeight(172); studentList.add(s1); Student s2 = new Student(); s2.setName("李四"); s2.setAge(25); s2.setSex("女"); s2.setHeight(165); studentList.add(s2); ExcelOpr excelOpr = new ExcelOpr(); excelOpr.write("d:/data/1.xlsx",studentList);
读文件
需要先创建读取文件的监听器类
继承AnalysisEventListener,重写方法
invoke方法,每读取Excel的一行数据,执行一次该方法
invokeHeadMap方法,读取表头执行的方法
package com.wanmait.demo; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import java.util.ArrayList; import java.util.List; import java.util.Map; public class ReadListener extends AnalysisEventListener<Student> { private List<Student> studentList = new ArrayList<>(); public List<Student> getContent() { return studentList; } private Map<Integer,String> headMap; public Map<Integer,String> getHead() { return headMap; } //读取一行数据 执行一次该方法 @Override public void invoke(Student student, AnalysisContext analysisContext) { studentList.add(student); } //读取head 表头 @Override public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { this.headMap = headMap; } //Excel文件读取结束 执行该方法 @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { } }
读文件的方法
package com.wanmait.demo; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.read.builder.ExcelReaderBuilder; import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder; import com.alibaba.excel.write.builder.ExcelWriterBuilder; import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; import java.util.List; public class ExcelOpr { public List<Student> read(String filename) { ReadListener readListener = new ReadListener(); //创建监听器对象 ExcelReaderBuilder builder = EasyExcel.read(filename,Student.class,readListener); //打开文件 ExcelReaderSheetBuilder sheet = builder.sheet(0); //打开文件的表 sheet.doRead(); //读取数据 return readListener.getContent(); } }
测试读取文件
ExcelOpr excelOpr = new ExcelOpr(); List<Student> studentList = excelOpr.read("d:/data/1.xlsx"); for(Student student:studentList) { System.out.println(student); }
0条评论
点击登录参与评论