easyexcel生成excel文件
Posted kiko2014551511
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了easyexcel生成excel文件相关的知识,希望对你有一定的参考价值。
目录
1、将List<List<String>>中的内容生成到excel表格中,无表头
2、将List<List<String>>中的内容生成到excel表格中,有表头
3、将List<Object>的内容生成到表格中
引入Maven依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>1.1.2-beta5</version> </dependency>
1、将List<List<String>>中的内容生成到excel表格中,无表头
package com.harara.easyexcel.write; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.metadata.Sheet;import com.alibaba.excel.support.ExcelTypeEnum;import java.io.FileOutputStream;import java.io.OutputStream; import java.util.ArrayList;import java.util.List; /** * * 通过List、Object生成excel * @author : harara * @version : 2.0 * @date : 2020/6/9 13:49 */ public class ExcelWriteTest { /** * 每行数据是List<String> * 无表头 */ public void writeWithoutHead(){ List<List<String>> data = getExcelData(); OutputStream out; ExcelWriter excelWriter; try { out = new FileOutputStream("withoutHead.xlsx"); excelWriter = new ExcelWriter(out, ExcelTypeEnum.XLSX,false); Sheet sheet1 = new Sheet(1,0); sheet1.setSheetName("sheet1"); excelWriter.write0(data,sheet1); excelWriter.finish(); out.flush(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { ExcelWriteTest excelWriteTest = new ExcelWriteTest(); excelWriteTest.writeWithoutHead(); System.out.println("finish"); } private List<List<String>> getExcelData(){ List<List<String>> data = new ArrayList<List<String>>(); for(int i=0;i<=10;i++){ List<String> line = new ArrayList<String>(); line.add("第"+i+"行,第1列内容"); line.add("第"+i+"行,第2列内容"); line.add("第"+i+"行,第3列内容"); data.add(line); } return data; } }
生成效果
2、将List<List<String>>的内容生成到excel表格中,有表头
package com.harara.easyexcel.write; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.metadata.Table; import com.alibaba.excel.support.ExcelTypeEnum;import java.io.FileOutputStream;import java.io.OutputStream; import java.util.ArrayList;import java.util.List; /** * * 通过List、Object生成excel * @author : harara * @version : 2.0 * @date : 2020/6/9 13:49 */ public class ExcelWriteTest { /** * 每行数据是List<String> * 有表头 */ public void writeWithHead(){ List<List<String>> data = getExcelData(); List<List<String>> head = getExcelHead(); OutputStream out; ExcelWriter excelWriter; try{ out = new FileOutputStream("withHead.xlsx"); excelWriter = new ExcelWriter(out, ExcelTypeEnum.XLSX,true); Table table = new Table(0); table.setHead(head); excelWriter.write0(data,new Sheet(0),table); excelWriter.finish(); out.flush(); }catch (Exception e){ e.printStackTrace(); } } public static void main(String[] args) { ExcelWriteTest excelWriteTest = new ExcelWriteTest(); excelWriteTest.writeWithHead(); System.out.println("finish"); } private List<List<String>> getExcelHead(){ List<List<String>> head = new ArrayList<List<String>>(); List<String> column1 = new ArrayList<String>(); column1.add("第一列"); List<String> column2 = new ArrayList<String>(); column2.add("第二列"); List<String> column3 = new ArrayList<String>(); column3.add("第三列"); head.add(column1); head.add(column2); head.add(column3); return head; } private List<List<String>> getExcelData(){ List<List<String>> data = new ArrayList<List<String>>(); for(int i=0;i<=10;i++){ List<String> line = new ArrayList<String>(); line.add("第"+i+"行,第1列内容"); line.add("第"+i+"行,第2列内容"); line.add("第"+i+"行,第3列内容"); data.add(line); } return data; } }
生成效果
3、将List<Object>的内容生成到表格中
package com.harara.easyexcel.write; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.metadata.Sheet;import com.alibaba.excel.support.ExcelTypeEnum; import com.harara.model.User; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * * 通过List、Object生成excel * @author : harara * @version : 2.0 * @date : 2020/6/9 13:49 */ public class ExcelWriteTest { /** * 每行数据是Object */ public void writeExcelByModel(){ List<User> users = getExcelModelData(); OutputStream outputStream = null; ExcelWriter excelWriter; try{ outputStream = new FileOutputStream("excelByModel.xlsx"); excelWriter = new ExcelWriter(outputStream,ExcelTypeEnum.XLSX,true); //sheet构造函数中指定实体对象为User.class Sheet sheet = new Sheet(1,0, users.get(0).getClass()); excelWriter.write(users,sheet); excelWriter.finish(); outputStream.flush(); }catch (Exception e){ e.printStackTrace(); }finally { if(outputStream != null){ try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) { ExcelWriteTest excelWriteTest = new ExcelWriteTest(); excelWriteTest.writeExcelByModel(); System.out.println("finish"); } /** * 获取实体对象数据 * @return */ private List<User> getExcelModelData(){ List<User> users = new ArrayList<User>(); for(int i=0; i <= 10;i++){ User user = new User(); user.setName("name" + i); user.setAge("age" + i); user.setEmail("email" + i); user.setAddress("address" + i); user.setSax("sax" + i); user.setHeigh("heigh" + i); user.setLast("last" + i); user.setBitthday(new Date()); users.add(user); } return users; } }
实体对象
继承BaseRowModel,字段上添加@ExcelProperty注解表示该字段要写入到excel中,属性value表示表头名,index表示列的顺序排序
package com.harara.model; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.metadata.BaseRowModel; import lombok.Data; import java.util.Date; /** * 要继承BaseRowModel * @author : harara * @version : 2.0 * @date : 2020/6/9 15:32 * */ @Data public class User extends BaseRowModel{ @ExcelProperty(value = "姓名", index = 0) private String name; @ExcelProperty(value = "年龄", index = 1) private String age; @ExcelProperty(value = "邮箱", index = 2) private String email; @ExcelProperty(value = "地址", index = 3) private String address; @ExcelProperty(value = "性别", index = 4) private String sax; @ExcelProperty(value = "高度", index = 5) private String heigh; @ExcelProperty(value = "备注", index = 6) private String last; @ExcelProperty(value = "生日",index = 7) private Date bitthday; }
生成效果
以上是关于easyexcel生成excel文件的主要内容,如果未能解决你的问题,请参考以下文章
[Java]_[初级]_[使用easyexcel生成或下载Excel文件]