https://github.com/alibaba/easyexcel
文章目录
环境搭建
读取excel文件
默认读取指定读取默认读取指定读取小于1000行数据大于1000行数据
导出excle
无模型映射导出模型映射导出单个Sheet导出多个Sheet导出
工具类
测试类
环境搭建easyexcel 依赖(必须)springboot (不是必须)lombok (不是必须)<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>1.1.2-beat1</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.2</version></dependency>
Spring Boot 基础就不介绍了,推荐下这个实战教程:
https://github.com/javastacks/spring-boot-best-practice
读取excel文件小于1000行数据默认读取
读取Sheet1的全部数据
StringfilePath=”/home/chenmingjian/Downloads/学生表.xlsx”;List<Object>objects=ExcelUtil.readLessThan1000Row(filePath);指定读取
下面是学生表.xlsx中Sheet1,Sheet2的数据
获取Sheet1表头以下的信息
StringfilePath=”/home/chenmingjian/Downloads/学生表.xlsx”;//第一个1代表sheet1,第二个1代表从第几行开始读取数据,行号最小值为0Sheetsheet=newSheet(1,1);List<Object>objects=ExcelUtil.readLessThan1000Row(filePath,sheet);
获取Sheet2的所有信息
StringfilePath=”/home/chenmingjian/Downloads/学生表.xlsx”;Sheetsheet=newSheet(2,0);List<Object>objects=ExcelUtil.readLessThan1000Row(filePath,sheet);大于1000行数据默认读取StringfilePath=”/home/chenmingjian/Downloads/学生表.xlsx”;List<Object>objects=ExcelUtil.readMoreThan1000Row(filePath);指定读取StringfilePath=”/home/chenmingjian/Downloads/学生表.xlsx”;Sheetsheet=newSheet(1,2);List<Object>objects=ExcelUtil.readMoreThan1000Row(filePath,sheet);导出excle单个Sheet导出无模型映射导出StringfilePath=”/home/chenmingjian/Downloads/测试.xlsx”;List<List<Object>>data=newArrayList<>();data.add(Arrays.asList(“111″,”222″,”333”));data.add(Arrays.asList(“111″,”222″,”333”));data.add(Arrays.asList(“111″,”222″,”333”));List<String>head=Arrays.asList(“表头1″,”表头2″,”表头3”);ExcelUtil.writeBySimple(filePath,data,head);
结果
模型映射导出
1、定义好模型对象
packagecom.springboot.utils.excel.test;importcom.alibaba.excel.annotation.ExcelProperty;importcom.alibaba.excel.metadata.BaseRowModel;importlombok.Data;importlombok.EqualsAndHashCode;/***@description:*@author:chenmingjian*@date:19-4-314:44*/@EqualsAndHashCode(callSuper=true)@DatapublicclassTableHeaderExcelPropertyextendsBaseRowModel{/***value:表头名称*index:列的号,0表示第一列*/@ExcelProperty(value=”姓名”,index=0)privateStringname;@ExcelProperty(value=”年龄”,index=1)privateintage;@ExcelProperty(value=”学校”,index=2)privateStringschool;}
2、调用方法
StringfilePath=”/home/chenmingjian/Downloads/测试.xlsx”;ArrayList<TableHeaderExcelProperty>data=newArrayList<>();for(inti=0;i<4;i ){TableHeaderExcelPropertytableHeaderExcelProperty=newTableHeaderExcelProperty();tableHeaderExcelProperty.setName(“cmj” i);tableHeaderExcelProperty.setAge(22 i);tableHeaderExcelProperty.setSchool(“清华大学” i);data.add(tableHeaderExcelProperty);}ExcelUtil.writeWithTemplate(filePath,data);多个Sheet导出
1、定义好模型对象
packagecom.springboot.utils.excel.test;importcom.alibaba.excel.annotation.ExcelProperty;importcom.alibaba.excel.metadata.BaseRowModel;importlombok.Data;importlombok.EqualsAndHashCode;/***@description:*@author:chenmingjian*@date:19-4-314:44*/@EqualsAndHashCode(callSuper=true)@DatapublicclassTableHeaderExcelPropertyextendsBaseRowModel{/***value:表头名称*index:列的号,0表示第一列*/@ExcelProperty(value=”姓名”,index=0)privateStringname;@ExcelProperty(value=”年龄”,index=1)privateintage;@ExcelProperty(value=”学校”,index=2)privateStringschool;}
2、调用方法
ArrayList<ExcelUtil.MultipleSheelPropety>list1=newArrayList<>();for(intj=1;j<4;j ){ArrayList<TableHeaderExcelProperty>list=newArrayList<>();for(inti=0;i<4;i ){TableHeaderExcelPropertytableHeaderExcelProperty=newTableHeaderExcelProperty();tableHeaderExcelProperty.setName(“cmj” i);tableHeaderExcelProperty.setAge(22 i);tableHeaderExcelProperty.setSchool(“清华大学” i);list.add(tableHeaderExcelProperty);}Sheetsheet=newSheet(j,0);sheet.setSheetName(“sheet” j);ExcelUtil.MultipleSheelPropetymultipleSheelPropety=newExcelUtil.MultipleSheelPropety();multipleSheelPropety.setData(list);multipleSheelPropety.setSheet(sheet);list1.add(multipleSheelPropety);}ExcelUtil.writeWithMultipleSheel(“/home/chenmingjian/Downloads/aaa.xlsx”,list1);
测试类
Spring Boot 基础就不介绍了,推荐下这个实战教程:
https://github.com/javastacks/spring-boot-best-practice
packagecom.springboot.utils.excel;importcom.alibaba.excel.annotation.ExcelProperty;importcom.alibaba.excel.metadata.BaseRowModel;importcom.alibaba.excel.metadata.Sheet;importlombok.Data;importlombok.EqualsAndHashCode;importorg.junit.runner.RunWith;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.List;/***@description:测试类*@author:chenmingjian*@date:19-4-415:24*/@SpringBootTest@RunWith(SpringRunner.class)publicclassTest{/***读取少于1000行的excle*/@org.junit.TestpublicvoidreadLessThan1000Row(){StringfilePath=”/home/chenmingjian/Downloads/测试.xlsx”;List<Object>objects=ExcelUtil.readLessThan1000Row(filePath);objects.forEach(System.out::println);}/***读取少于1000行的excle,可以指定sheet和从几行读起*/@org.junit.TestpublicvoidreadLessThan1000RowBySheet(){StringfilePath=”/home/chenmingjian/Downloads/测试.xlsx”;Sheetsheet=newSheet(1,1);List<Object>objects=ExcelUtil.readLessThan1000RowBySheet(filePath,sheet);objects.forEach(System.out::println);}/***读取大于1000行的excle*带sheet参数的方法可参照测试方法readLessThan1000RowBySheet()*/@org.junit.TestpublicvoidreadMoreThan1000Row(){StringfilePath=”/home/chenmingjian/Downloads/测试.xlsx”;List<Object>objects=ExcelUtil.readMoreThan1000Row(filePath);objects.forEach(System.out::println);}/***生成excle*带sheet参数的方法可参照测试方法readLessThan1000RowBySheet()*/@org.junit.TestpublicvoidwriteBySimple(){StringfilePath=”/home/chenmingjian/Downloads/测试.xlsx”;List<List<Object>>data=newArrayList<>();data.add(Arrays.asList(“111″,”222″,”333”));data.add(Arrays.asList(“111″,”222″,”333”));data.add(Arrays.asList(“111″,”222″,”333”));List<String>head=Arrays.asList(“表头1″,”表头2″,”表头3″);ExcelUtil.writeBySimple(filePath,data,head);}/***生成excle,带用模型*带sheet参数的方法可参照测试方法readLessThan1000RowBySheet()*/@org.junit.TestpublicvoidwriteWithTemplate(){StringfilePath=”/home/chenmingjian/Downloads/测试.xlsx”;ArrayList<TableHeaderExcelProperty>data=newArrayList<>();for(inti=0;i<4;i ){TableHeaderExcelPropertytableHeaderExcelProperty=newTableHeaderExcelProperty();tableHeaderExcelProperty.setName(“cmj” i);tableHeaderExcelProperty.setAge(22 i);tableHeaderExcelProperty.setSchool(“清华大学” i);data.add(tableHeaderExcelProperty);}ExcelUtil.writeWithTemplate(filePath,data);}/***生成excle,带用模型,带多个sheet*/@org.junit.TestpublicvoidwriteWithMultipleSheel(){ArrayList<ExcelUtil.MultipleSheelPropety>list1=newArrayList<>();for(intj=1;j<4;j ){ArrayList<TableHeaderExcelProperty>list=newArrayList<>();for(inti=0;i<4;i ){TableHeaderExcelPropertytableHeaderExcelProperty=newTableHeaderExcelProperty();tableHeaderExcelProperty.setName(“cmj” i);tableHeaderExcelProperty.setAge(22 i);tableHeaderExcelProperty.setSchool(“清华大学” i);list.add(tableHeaderExcelProperty);}Sheetsheet=newSheet(j,0);sheet.setSheetName(“sheet” j);ExcelUtil.MultipleSheelPropetymultipleSheelPropety=newExcelUtil.MultipleSheelPropety();multipleSheelPropety.setData(list);multipleSheelPropety.setSheet(sheet);list1.add(multipleSheelPropety);}ExcelUtil.writeWithMultipleSheel(“/home/chenmingjian/Downloads/aaa.xlsx”,list1);}/*******************匿名内部类,实际开发中该对象要提取出去**********************//***@description:*@author:chenmingjian*@date:19-4-314:44*/@EqualsAndHashCode(callSuper=true)@DatapublicstaticclassTableHeaderExcelPropertyextendsBaseRowModel{/***value:表头名称*index:列的号,0表示第一列*/@ExcelProperty(value=”姓名”,index=0)privateStringname;@ExcelProperty(value=”年龄”,index=1)privateintage;@ExcelProperty(value=”学校”,index=2)privateStringschool;}/*******************匿名内部类,实际开发中该对象要提取出去**********************/}
— End —
最近热文:1.Spring Boot 学习笔记,这个太全了!2.图文并茂,Spring Boot Starter 万字详解!3.Spring Boot Redis 抗住 10w 人秒杀抢单!4.12 个开源的后台管理系统,接私活必备!5.面试官:GET 请求能上传图片吗?
获取一份 Spring Boot 资料!