java一个简单的EasyExcel导出excel模板api
2021/11/23 17:09:47
本文主要是介绍java一个简单的EasyExcel导出excel模板api,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
导出样式
一、设置头部
@HeadRowHeight(35)//表头行高 @ContentRowHeight(25)//内容行高 @ColumnWidth(20)//列宽 @Data @TableName("student") public class ExcelInfo { @ExcelIgnore private Integer id; @ExcelProperty(value = {"学生姓名"}, index = 0) private String name; @ExcelProperty(value = {"学生信息","学生年龄"}, index = 1) private Integer age; @ExcelProperty(value = {"学生信息","学生性别"}, index = 2) private Integer sex; @ExcelProperty(value = {"学生信息","学生住址"}, index = 3) private String address; @ExcelProperty(value = {"学生电话"}, index = 4) private String phone; @ExcelIgnore @ExcelProperty(value = {"学生信息","学生状态"}, index = 5) private Integer status; }
二、为了方便逻辑是直接写在controller层
@RestController @RequestMapping("/excel") @Slf4j public class ExcelController { @Resource private ExcelService excelService; @GetMapping("/export") public void export(HttpServletResponse response) { String filename = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ".xlsx"; ExcelWriter excelWriter = null; try { // 头的策略 WriteCellStyle headWriteCellStyle = new WriteCellStyle(); // 背景色 headWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex()); WriteFont headWriteFont = new WriteFont(); headWriteFont.setFontHeightInPoints((short) 12); headWriteCellStyle.setWriteFont(headWriteFont); WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); // 字体策略 WriteFont contentWriteFont = new WriteFont(); // 字体大小 contentWriteFont.setFontHeightInPoints((short) 12); contentWriteCellStyle.setWriteFont(contentWriteFont); //设置 自动换行 contentWriteCellStyle.setWrapped(true); //设置 垂直居中 contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //水平居中 contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); //设置边框样式 contentWriteCellStyle.setBorderLeft(BorderStyle.THIN); contentWriteCellStyle.setBorderTop(BorderStyle.THIN); contentWriteCellStyle.setBorderRight(BorderStyle.THIN); contentWriteCellStyle.setBorderBottom(BorderStyle.THIN); //传入样式 HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); List<ExcelInfo> list = excelService.list(); response.setHeader("Content-Disposition", "attachment;filename=" + filename); //导入一个sheet // EasyExcel.write(response.getOutputStream(), ExcelInfo.class).sheet(1,"模板1").registerWriteHandler(horizontalCellStyleStrategy).doWrite(list); //导入多个sheet excelWriter = EasyExcel.write(response.getOutputStream(), ExcelInfo.class).build(); //创建一个sheet WriteSheet writeSheet = EasyExcel.writerSheet(0, "模板1").registerWriteHandler(horizontalCellStyleStrategy).build(); excelWriter.write(list, writeSheet); //创建一个新的sheet writeSheet = EasyExcel.writerSheet(1, "模板2").registerWriteHandler(horizontalCellStyleStrategy).build(); excelWriter.write(list, writeSheet); } catch (IOException e) { e.printStackTrace(); log.error("导出{}文件失败!", filename); }finally { //关闭流 if(excelWriter != null){ excelWriter.finish(); } } } }
三、数据库(ORM框架用的mybatis-plus)
create database db_excel; use db_excel; create table if not exists student( id int primary key auto_increment, name varchar(20), age int, sex int(2) , address varchar(30), phone varchar(11), status int(2) );
这篇关于java一个简单的EasyExcel导出excel模板api的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-11cursor试用出现:Too many free trial accounts used on this machine 的解决方法
- 2025-01-11百万架构师第十四课:源码分析:Spring 源码分析:深入分析IOC那些鲜为人知的细节|JavaGuide
- 2025-01-11不得不了解的高效AI办公工具API
- 2025-01-102025 蛇年,J 人直播带货内容审核团队必备的办公软件有哪 6 款?
- 2025-01-10高效运营背后的支柱:文档管理优化指南
- 2025-01-10年末压力山大?试试优化你的文档管理
- 2025-01-10跨部门协作中的进度追踪重要性解析
- 2025-01-10总结 JavaScript 中的变体函数调用方式
- 2025-01-10HR团队如何通过数据驱动提升管理效率?6个策略
- 2025-01-10WBS实战指南:如何一步步构建高效项目管理框架?