使用hutool工具类进行导出
引入依赖为:
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.0.6</version> </dependency>
代码为:
@Test public void merTest() { //测试使用的数据 TestBean bean1 = new TestBean(); bean1.setName("张三"); bean1.setAge(22); bean1.setPass(true); bean1.setScore(66.30); bean1.setExamDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"))); TestBean bean2 = new TestBean(); bean2.setName("李四"); bean2.setAge(28); bean2.setPass(false); bean2.setScore(38.50); bean2.setExamDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"))); //传入list集合数组 List<TestBean> rows = CollUtil.newArrayList(bean1, bean2); // 通过工具类创建writer ExcelWriter writer = ExcelUtil.getBigWriter("E:/data/excel/4.xlsx"); String[] names = {"name", "age", "score", "isPass", "examDate"}; String[] names2 = {"姓名", "年龄", "分数", "是否通过", "考试时间"}; for (int i = 0; i < names.length; i++) { writer.addHeaderAlias(names[i], names2[i]); } //todo 设置自动换行(时间) writer.setColumnWidth(4, 25); // 一次性写出内容,使用默认样式,强制输出标题 writer.write(rows, true); // 关闭writer,释放内存 writer.close(); }
将会在指定的文件中生成对应的excel文件
本文来自博客园,作者:King-DA,转载请注明原文链接:https://www.cnblogs.com/qingmuchuanqi48/p/11968442.html