赞
踩
本篇讲述EasyPOI的最基本使用,即导出和导入Excel文档。
<properties> <easypoi.version>4.1.2</easypoi.version> </properties> <dependencies> <!-- easy-poi --> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>${easypoi.version}</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>${easypoi.version}</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>${easypoi.version}</version> </dependency> </dependencies>
@Test public void directExportExcelByObject() throws IOException { List<StudentVO> list = new ArrayList<>(16); StudentVO student; Random random = new Random(); for (int i = 0; i < 10; i++) { student = new StudentVO(i + "", "name" + i, random.nextInt(2), random.nextInt(100), new Date(), "className" + i); student.setSchoolName("学校名称" + i); student.setSchoolAddress("学校地址" + i); list.add(student); } ExportParams exportParams = new ExportParams(); // 设置sheet得名称 exportParams.setSheetName("我是sheet名字"); List<Map<String, Object>> sheetsList = new ArrayList<>(); // 创建sheet1使用得map Map<String, Object> deptExportMap = new HashMap<>(); // title的参数为ExportParams类型,目前仅仅在ExportParams中设置了sheetName deptExportMap.put("title", exportParams); // 模版导出对应得实体类型 deptExportMap.put("entity", StudentVO.class); // sheet中要填充得数据 deptExportMap.put("data", list); sheetsList.add(deptExportMap); // 创建参数对象(用来设定excel得sheet得内容等信息) ExportParams empExportParams = new ExportParams(); empExportParams.setSheetName("我是sheet名字2"); // 创建sheet2使用得map Map<String, Object> empExportMap = new HashMap<>(); empExportMap.put("title", empExportParams); empExportMap.put("entity", StudentVO.class); empExportMap.put("data", list); sheetsList.add(empExportMap); // 生成workbook 并导出 Workbook workbook = ExcelExportUtil.exportExcel(sheetsList,ExcelType.XSSF); //Workbook workbook1 = ExcelExportUtil.exportExcel(exportParams, StudentVO.class, list); /* File savefile = new File("C:/Users/JustryDeng/Desktop/"); if (!savefile.exists()) { boolean result = savefile.mkdirs(); System.out.println("目录不存在,创建" + result); }*/ FileOutputStream fos = new FileOutputStream("D:\\test\\student11.xlsx"); workbook.write(fos); fos.close(); }
/** * 学校模型 */ @Data class SchoolVO { /** * 学校名称 */ @Excel(name = "学校名称", orderNum = "6", width = 20) private String schoolName; /** * 学校地址 */ @Excel(name = "学校地址", orderNum = "8", width = 20) private String schoolAddress; } @Data class StudentVO extends SchoolVO { /** * name指定导出excel时生成的列名 * orderNum可指定导出的该属性对应的所在列的位置 * width设置单元格宽度 * type设置导出类型 1是文本, 2是图片, 3是函数,10 数字 默认是文本 */ @Excel(name = "学号", orderNum = "1", type = 10, width = 15) private String studentID; @Excel(name = "姓名", orderNum = "2", width = 15) private String name; /** * 当gender为1时,导出的结果为 男, 当gender为0时,导出的结果为 女 * mergeVertical设置是否纵向合并列 */ @Excel(name = "性别", mergeVertical = true, replace = {"男_1", "女_0"}, orderNum = "3", width = 5) private Integer gender; /** * type设置导出类型 1是文本, 2是图片, 3是函数,10 数字 默认是文本 */ @Excel(name = "年龄", orderNum = "4", type = 10, width = 5) private int age; /** * 将Data日期导出为yyyy-MM-dd格式 * mergeVertical设置是否纵向合并列 * mergeRely设置合并列的前提条件,即:只有当索引为2的列(即:"性别"列)已经 * 合并了时,那么此时这一列的纵向相同时,才能纵向合并;如果引为2的 * 列(即:"性别"列)纵向数据不同,那么就算此列的纵向数据相同,那么 * 也不会合并 */ @Excel(name = "入校时间", mergeVertical = true, mergeRely = {2}, format = "yyyy-MM-dd", orderNum = "5", width = 20) private Date entranceTime; @Excel(name = "班级", orderNum = "7", width = 15) private String className; /** * 无参构造 */ public StudentVO() { } /** * 全参构造 */ public StudentVO(String studentID, String name, Integer gender, int age, Date entranceTime, String className) { this.studentID = studentID; this.name = name; this.gender = gender; this.age = age; this.entranceTime = entranceTime; this.className = className; } }
如果此篇文章有帮助到您, 希望打大佬们能
关注
、点赞
、收藏
、评论
支持一波,非常感谢大家!
如果有不对的地方请指正!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。