java使用easyexcel操作excel
作者:互联网
maven依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>1.0.1</version> </dependency>
写入excel
excel实体类
package com.avivacofco.epidemic.vo; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.metadata.BaseRowModel; /** * @author :jerry_wei * @date :Created in 2020/2/18 23:15 * @description: * @modified By: * @version: */ public class HrUserVo extends BaseRowModel { @ExcelProperty(value = "英文名", index = 6) private String e_name; @ExcelProperty(value = "中文名", index = 5) private String c_name; @ExcelProperty(value = "邮箱", index = 7) private String email; @ExcelProperty(value = "二级机构", index = 1) private String orgCode2; @ExcelProperty(value = "三级机构", index = 2) private String orgCode3; @ExcelProperty(value = "职能单元", index = 3) private String orgCode5; @ExcelProperty(value = "部门", index = 4) private String orgCode6; public String getE_name() { return e_name; } public void setE_name(String e_name) { this.e_name = e_name; }
@Override public String toString() { return "HrUserVo{" + "e_name='" + e_name + '\'' + ", c_name='" + c_name + '\'' + ", email='" + email + '\'' + ", orgCode2='" + orgCode2 + '\'' + ", orgCode3='" + orgCode3 + '\'' + ", orgCode5='" + orgCode5 + '\'' + ", orgCode6='" + orgCode6 + '\'' + '}'; } }
具体实现
try (OutputStream out = new FileOutputStream(file)) { ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX); Sheet sheet1 = new Sheet(1, 0, HrUserVo.class); sheet1.setSheetName("防控统计"); List<HrUserVo> data = new ArrayList<>(); for (int i = 0; i < addUser.size(); i++) { HrUserVo item = new HrUserVo(); HrUser hrUser = addUser.get(i); item.setE_name(hrUser.getE_name()); item.setC_name(hrUser.getC_Name()); item.setEmail(hrUser.getEmail()); item.setOrgCode2(org2.getC_name()); item.setOrgCode3(org3.getC_name()); item.setOrgCode5(org5.getC_name()); item.setOrgCode6(org6.getC_name()); data.add(item); } writer.write(data, sheet1); writer.finish(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
标签:index,java,name,easyexcel,ExcelProperty,excel,private,item,String 来源: https://www.cnblogs.com/jerry-wei/p/12835920.html