喝水也不忘挖井。感谢阿里巴巴项目团队提供easyexcel工具类、github地址。

文章列表

环境构建Sheet文件读取基本读取指定读取基本读取指定读取小于1000行的数据1000行以上导出excle无模型映射导出模型映射导出单页导出多页导出工具类测试类环境构建

Easyexcel依赖于springboot(非必需)lombok(非必需)dependency

GroupIdcom.alibaba/groupId

ArtifactIdeasyexcel/artifactId

版本1.1.2-beat 1/版本

/dependency

Dependency

groupidorg . project lombok/groupid

artifactidlombok/artifact id

版本1.18.2/版本

/dependency

读取Excel文件

少于1000行的数据

基本阅读

读取图纸1中的所有数据

string file path='/home/chenmingjian/downloads/学生表。“xlsx”;

Listobjectobjects=excelu(文件路径);

指定读取

如果您正在学习Spring Boot,建议您使用连载多年不断更新的免费教程。

下面是学生票。xlsx中工作表1、工作表2中的数据

获取图纸1标题下的信息

string file path='/home/chenmingjian/downloads/学生表。“xlsx”;

//第一个1表示图纸1,第二个1表示从前几行开始读取数据,行号最小值为0

图纸图纸=新图纸(1,1);

Listobjectobjects=excelu(文件路径、图纸);

获取有关图纸2的所有信息

string file path='/home/chenmingjian/downloads/学生表。“xlsx”;

图纸图纸=新图纸(2,0);

Listobjectobjects=excelu(文件路径、图纸);

超过1000行的数据

基本阅读

string file path='/home/chenmingjian/downloads/学生表。“xlsx”;

Listobjectobjects=excelu(文件路径);

指定读取

string file path='/home/chenmingjian/downloads/学生表。“xlsx”;

图纸图纸=新图纸(1、2);

Listobjectobjects=excelu(文件路径、图纸);

Excle导出

汇出单一图纸

无模型映射导出

string file path='/home/chenmingjian/downloads/测试。“xlsx”;

列表lt

;List<Object>> data = new ArrayList<>(); da("111","222","333")); da("111","222","333")); da("111","222","333")); List<String> head = Arrays.asList("表头1", "表头2", "表头3"); ExcelU(filePath,data,head);

结果

模型映射导出

1、定义好模型对象

package com.; import com.alibaba.excel.anno; import com.alibaba.excel.me; import lombok.Data; import lombok.EqualsAndHashCode; /** * @description: * @author: chenmingjian * @date: 19-4-3 14:44 */ @EqualsAndHashCode(callSuper = true) @Data public class TableHeaderExcelProperty extends BaseRowModel { /** * value: 表头名称 * index: 列的号, 0表示第一列 */ @ExcelProperty(value = "姓名", index = 0) private String name; @ExcelProperty(value = "年龄",index = 1) private int age; @ExcelProperty(value = "学校",index = 2) private String school; }

2、调用方法

String filePath = "/home/chenmingjian/Downloads/测试.xlsx"; ArrayList<TableHeaderExcelProperty> data = new ArrayList<>(); for(int i = 0; i < 4; i++){ TableHeaderExcelProperty tableHeaderExcelProperty = new TableHeaderExcelProperty(); ("cmj" + i); (22 + i); ("清华大学" + i); da(tableHeaderExcelProperty); } ExcelU(filePath,data);

多个Sheet导出

1、定义好模型对象

package com.; import com.alibaba.excel.anno; import com.alibaba.excel.me; import lombok.Data; import lombok.EqualsAndHashCode; /** * @description: * @author: chenmingjian * @date: 19-4-3 14:44 */ @EqualsAndHashCode(callSuper = true) @Data public class TableHeaderExcelProperty extends BaseRowModel { /** * value: 表头名称 * index: 列的号, 0表示第一列 */ @ExcelProperty(value = "姓名", index = 0) private String name; @ExcelProperty(value = "年龄",index = 1) private int age; @ExcelProperty(value = "学校",index = 2) private String school; }

2、调用方法

ArrayList<ExcelU; list1 = new ArrayList<>(); for(int j = 1; j < 4; j++){ ArrayList<TableHeaderExcelProperty> list = new ArrayList<>(); for(int i = 0; i < 4; i++){ TableHeaderExcelProperty tableHeaderExcelProperty = new TableHeaderExcelProperty(); ("cmj" + i); (22 + i); ("清华大学" + i); li(tableHeaderExcelProperty); } Sheet sheet = new Sheet(j, 0); ("sheet" + j); ExcelU multipleSheelPropety = new ExcelU(); mul(list); mul(sheet); li(multipleSheelPropety); } ExcelU("/home/chenmingjian/Download;,list1);

工具类

另外,如果您正在学习Spring Cloud,推荐一个连载多年还在继续更新的免费教程:

package com.; import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.con; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.me; import com.alibaba.excel.me; import lombok.Data; import lombok.Getter; import lombok.Setter; import lombok.ex; import org.; import org.; import java.io.*; import java.u; import java.u; import java.u; /** * @description: * @author: chenmingjian * @date: 19-3-18 16:16 */ @Slf4j public class ExcelUtil { private static Sheet initSheet; static { initSheet = new Sheet(1, 0); ini("sheet"); //设置自适应宽度 ini); } /** * 读取少于1000行数据 * @param filePath 文件绝对路径 * @return */ public static List<Object> readLessThan1000Row(String filePath){ return readLessThan1000RowBySheet(filePath,null); } /** * 读小于1000行数据, 带样式 * filePath 文件绝对路径 * initSheet : * sheetNo: sheet页码,默认为1 * headLineMun: 从第几行开始读取数据,默认为0, 表示从第一行开始读取 * clazz: 返回数据List<Object> 中Object的类名 */ public static List<Object> readLessThan1000RowBySheet(String filePath, Sheet sheet){ if(!S(filePath)){ return null; } sheet = sheet != null ? sheet : initSheet; InputStream fileStream = null; try { fileStream = new FileInputStream(filePath); return Ea(fileStream, sheet); } catch (FileNotFoundException e) { log.info("找不到文件或文件路径错误, 文件:{}", filePath); }finally { try { if(fileStream != null){ (); } } catch (IOException e) { log.info("excel文件读取失败, 失败原因:{}", e); } } return null; } /** * 读大于1000行数据 * @param filePath 文件觉得路径 * @return */ public static List<Object> readMoreThan1000Row(String filePath){ return readMoreThan1000RowBySheet(filePath,null); } /** * 读大于1000行数据, 带样式 * @param filePath 文件觉得路径 * @return */ public static List<Object> readMoreThan1000RowBySheet(String filePath, Sheet sheet){ if(!S(filePath)){ return null; } sheet = sheet != null ? sheet : initSheet; InputStream fileStream = null; try { fileStream = new FileInputStream(filePath); ExcelListener excelListener = new ExcelListener(); EaBySax(fileStream, sheet, excelListener); return excelLi(); } catch (FileNotFoundException e) { log.error("找不到文件或文件路径错误, 文件:{}", filePath); }finally { try { if(fileStream != null){ (); } } catch (IOException e) { log.error("excel文件读取失败, 失败原因:{}", e); } } return null; } /** * 生成excle * @param filePath 绝对路径, 如:/home/chenmingjian/Download * @param data 数据源 * @param head 表头 */ public static void writeBySimple(String filePath, List<List<Object>> data, List<String> head){ writeSimpleBySheet(filePath,data,head,null); } /** * 生成excle * @param filePath 绝对路径, 如:/home/chenmingjian/Download * @param data 数据源 * @param sheet excle页面样式 * @param head 表头 */ public static void writeSimpleBySheet(String filePath, List<List<Object>> data, List<String> head, Sheet sheet){ sheet = (sheet != null) ? sheet : initSheet; if(head != null){ List<List<String>> list = new ArrayList<>(); (h -> li(h))); (list); } OutputStream outputStream = null; ExcelWriter writer = null; try { outputStream = new FileOutputStream(filePath); writer = Ea(outputStream); wri(data,sheet); } catch (FileNotFoundException e) { log.error("找不到文件或文件路径错误, 文件:{}", filePath); }finally { try { if(writer != null){ wri(); } if(outputStream != null){ ou(); } } catch (IOException e) { log.error("excel文件导出失败, 失败原因:{}", e); } } } /** * 生成excle * @param filePath 绝对路径, 如:/home/chenmingjian/Download * @param data 数据源 */ public static void writeWithTemplate(String filePath, List<? extends BaseRowModel> data){ writeWithTemplateAndSheet(filePath,data,null); } /** * 生成excle * @param filePath 绝对路径, 如:/home/chenmingjian/Download * @param data 数据源 * @param sheet excle页面样式 */ public static void writeWithTemplateAndSheet(String filePath, List<? extends BaseRowModel> data, Sheet sheet){ i(data)){ return; } sheet = (sheet != null) ? sheet : initSheet; (0).getClass()); OutputStream outputStream = null; ExcelWriter writer = null; try { outputStream = new FileOutputStream(filePath); writer = Ea(outputStream); wri(data,sheet); } catch (FileNotFoundException e) { log.error("找不到文件或文件路径错误, 文件:{}", filePath); }finally { try { if(writer != null){ wri(); } if(outputStream != null){ ou(); } } catch (IOException e) { log.error("excel文件导出失败, 失败原因:{}", e); } } } /** * 生成多Sheet的excle * @param filePath 绝对路径, 如:/home/chenmingjian/Download * @param multipleSheelPropetys */ public static void writeWithMultipleSheel(String filePath,List<MultipleSheelPropety> multipleSheelPropetys){ i(multipleSheelPropetys)){ return; } OutputStream outputStream = null; ExcelWriter writer = null; try { outputStream = new FileOutputStream(filePath); writer = Ea(outputStream); for (MultipleSheelPropety multipleSheelPropety : multipleSheelPropetys) { Sheet sheet = mul() != null ? mul() : initSheet; if(!Collec())){ ().get(0).getClass()); } wri(), sheet); } } catch (FileNotFoundException e) { log.error("找不到文件或文件路径错误, 文件:{}", filePath); }finally { try { if(writer != null){ wri(); } if(outputStream != null){ ou(); } } catch (IOException e) { log.error("excel文件导出失败, 失败原因:{}", e); } } } /*********************匿名内部类开始,可以提取出去******************************/ @Data public static class MultipleSheelPropety{ private List<? extends BaseRowModel> data; private Sheet sheet; } /** * 解析监听器, * 每解析一行会回调invoke()方法。 * 整个excel解析结束会执行doAfterAllAnalysed()方法 * * @author: chenmingjian * @date: 19-4-3 14:11 */ @Getter @Setter public static class ExcelListener extends AnalysisEventListener { private List<Object> datas = new ArrayList<>(); /** * 逐行解析 * object : 当前行的数据 */ @Override public void invoke(Object object, AnalysisContext context) { //当前行 // con() if (object != null) { da(object); } } /** * 解析完所有数据后会调用该方法 */ @Override public void doAfterAllAnalysed(AnalysisContext context) { //解析结束销毁不用的资源 } } /************************匿名内部类结束,可以提取出去***************************/ }

测试类

package com.; import com.alibaba.excel.anno; import com.alibaba.excel.me; import com.alibaba.excel.me; import lombok.Data; import lombok.EqualsAndHashCode; import org.junit.runner.RunWith; import org.; import org.; import java.u; import java.u; import java.u; /** * @description: 测试类 * @author: chenmingjian * @date: 19-4-4 15:24 */ @SpringBootTest @RunWi) public class Test { /** * 读取少于1000行的excle */ @org.junit.Test public void readLessThan1000Row(){ String filePath = "/home/chenmingjian/Downloads/测试.xlsx"; List<Object> objects = ExcelU(filePath); objec(System.out::println); } /** * 读取少于1000行的excle,可以指定sheet和从几行读起 */ @org.junit.Test public void readLessThan1000RowBySheet(){ String filePath = "/home/chenmingjian/Downloads/测试.xlsx"; Sheet sheet = new Sheet(1, 1); List<Object> objects = ExcelUBySheet(filePath,sheet); objec(System.out::println); } /** * 读取大于1000行的excle * 带sheet参数的方法可参照测试方法readLessThan1000RowBySheet() */ @org.junit.Test public void readMoreThan1000Row(){ String filePath = "/home/chenmingjian/Downloads/测试.xlsx"; List<Object> objects = ExcelU(filePath); objec(System.out::println); } /** * 生成excle * 带sheet参数的方法可参照测试方法readLessThan1000RowBySheet() */ @org.junit.Test public void writeBySimple(){ String filePath = "/home/chenmingjian/Downloads/测试.xlsx"; List<List<Object>> data = new ArrayList<>(); da("111","222","333")); da("111","222","333")); da("111","222","333")); List<String> head = Arrays.asList("表头1", "表头2", "表头3"); ExcelU(filePath,data,head); } /** * 生成excle, 带用模型 * 带sheet参数的方法可参照测试方法readLessThan1000RowBySheet() */ @org.junit.Test public void writeWithTemplate(){ String filePath = "/home/chenmingjian/Downloads/测试.xlsx"; ArrayList<TableHeaderExcelProperty> data = new ArrayList<>(); for(int i = 0; i < 4; i++){ TableHeaderExcelProperty tableHeaderExcelProperty = new TableHeaderExcelProperty(); ("cmj" + i); (22 + i); ("清华大学" + i); da(tableHeaderExcelProperty); } ExcelU(filePath,data); } /** * 生成excle, 带用模型,带多个sheet */ @org.junit.Test public void writeWithMultipleSheel(){ ArrayList<ExcelU; list1 = new ArrayList<>(); for(int j = 1; j < 4; j++){ ArrayList<TableHeaderExcelProperty> list = new ArrayList<>(); for(int i = 0; i < 4; i++){ TableHeaderExcelProperty tableHeaderExcelProperty = new TableHeaderExcelProperty(); ("cmj" + i); (22 + i); ("清华大学" + i); li(tableHeaderExcelProperty); } Sheet sheet = new Sheet(j, 0); ("sheet" + j); ExcelU multipleSheelPropety = new ExcelU(); mul(list); mul(sheet); li(multipleSheelPropety); } ExcelU("/home/chenmingjian/Download;,list1); } /*******************匿名内部类,实际开发中该对象要提取出去**********************/ /** * @description: * @author: chenmingjian * @date: 19-4-3 14:44 */ @EqualsAndHashCode(callSuper = true) @Data public static class TableHeaderExcelProperty extends BaseRowModel { /** * value: 表头名称 * index: 列的号, 0表示第一列 */ @ExcelProperty(value = "姓名", index = 0) private String name; @ExcelProperty(value = "年龄",index = 1) private int age; @ExcelProperty(value = "学校",index = 2) private String school; } /*******************匿名内部类,实际开发中该对象要提取出去**********************/ }

作者 | 冲奶粉的奶爸

来源 |

1.《【excel导入怎么导】历史上最完整的Excel导入和导出(easyexcel版)》援引自互联网,旨在传递更多网络信息知识,仅代表作者本人观点,与本网站无关,侵删请联系页脚下方联系方式。

2.《【excel导入怎么导】历史上最完整的Excel导入和导出(easyexcel版)》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。

3.文章转载时请保留本站内容来源地址,https://www.lu-xu.com/keji/2492374.html