关于EasyExcel实现Excel导出功能
Posted zjoe80
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于EasyExcel实现Excel导出功能相关的知识,希望对你有一定的参考价值。
直接上代码:
例子:
第一步:
<!--Excel包--> <!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel --> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>1.1.2-beta5</version> </dependency>
第二步:
@Test public void ExportTest() throws IOException { //指定文件输出位置 OutputStream outputStream =new FileOutputStream("H:/excel/file/myexcel3.xlsx"); ExcelWriter excelWriter =EasyExcelFactory.getWriter(outputStream); //将要输出的内容填充到Sheet里 Sheet sheet =new Sheet(1,0,ExcelModel.class ); //设置sheet表名 sheet.setSheetName("my_three_excel"); /** * 写数据到Write上下文中 * 第一个参数:要写入的内容 * 第二个参数:要写入的sheet目标 */ excelWriter.write(createModelList2(),sheet); excelWriter.finish(); outputStream.close(); }
第三步:
@Data public class ExcelMode extends BaseRowModel { @ExcelProperty(value = "姓名" ,index = 0) private String userName; @ExcelProperty(value = "年龄" ,index = 1) private String age; @ExcelProperty(value = "住址" ,index = 2) private String address; } private List<ExcelMode> createModelList (){ List<ExcelMode> list = new ArrayList<>(); for(int i=0; i<20;i++){ ExcelMode excelMode = new ExcelMode(); excelMode.setUserName("哒哒"+i); excelMode.setAge("22"); excelMode.setAddress("广西"); list.add(excelMode); } return list; }
第四步:
@Data public class ExcelModel2 extends BaseRowModel { @ExcelProperty(value = {"name","name"},index = 0) private String name; @ExcelProperty(value ={"age","age"},index = 1) private String age; @ExcelProperty(value={"cash_value","高"},index = 2) private String cashvalue_high ; @ExcelProperty(value={"cash_value","中"},index = 3) private String cashvalue_during ; @ExcelProperty(value={"cash_value","低"},index = 4) private String cashvalue_low ; }
以上是关于关于EasyExcel实现Excel导出功能的主要内容,如果未能解决你的问题,请参考以下文章
ATP应用测试平台——使用EasyExcel实现excel导入导出多sheet填充模板下载等功能案例实战