基于注解玩转excel导出导入
demo 请 加入 598347590
技术解决群获取

话不多说 开始
| |
| |
| <!-- poi office --> |
| <dependency> |
| <groupId>org.apache.poi</groupId> |
| <artifactId>poi</artifactId> |
| <version>3.13</version> |
| </dependency> |
| <dependency> |
| <groupId>org.apache.poi</groupId> |
| <artifactId>poi-ooxml</artifactId> |
| <version>3.13</version> |
| </dependency> |
| <dependency> |
| <groupId>org.apache.poi</groupId> |
| <artifactId>poi-ooxml-schemas</artifactId> |
| <version>3.13</version> |
| </dependency> |
| <dependency> |
| <groupId>javax.servlet</groupId> |
| <artifactId>javax.servlet-api</artifactId> |
| </dependency> |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-aop</artifactId> |
| </dependency> |
| <dependency> |
| <groupId>org.apache.commons</groupId> |
| <artifactId>commons-lang3</artifactId> |
| <version>3.0</version> |
| </dependency> |
| <dependency> |
| <groupId>javax.validation</groupId> |
| <artifactId>validation-api</artifactId> |
| </dependency> |
请自行创建项目 我就不一一演示了 代码生成器即可


生成三层架构 完毕后
讲解 @ExcelField 注解
| |
| |
| |
| |
| |
| @Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE}) |
| @Retention(RetentionPolicy.RUNTIME) |
| public @interface ExcelField { |
| |
| |
| |
| |
| String value() default ""; |
| |
| |
| |
| |
| String title(); |
| |
| |
| |
| |
| int type() default 0; |
| |
| |
| |
| |
| int align() default 0; |
| |
| |
| |
| |
| int sort() default 0; |
| |
| |
| |
| |
| String dictType() default ""; |
| |
| |
| |
| |
| Class<?> fieldType() default Class.class; |
| |
| |
| |
| |
| int[] groups() default {}; |
| } |
基本用法
| title: 导出的单元格标题 |
| aling: 对齐格式 0:自动;1:靠左;2:居中;3:靠右 |
| sort[重要]: 导出的字段顺序 别重复哦 会覆盖 |
| |
| |
| @ExcelField(title = "管理员", align = 2, sort = 1) |
| private String name; |
这里定义完毕之后 在controller 调用即可
创建controller 访问
| package top.yangbuyi.controller; |
| |
| |
| import lombok.extern.slf4j.Slf4j; |
| import org.springframework.web.bind.annotation.RequestMapping; |
| import org.springframework.web.bind.annotation.RestController; |
| import top.yangbuyi.common.excel.ExportExcel; |
| import top.yangbuyi.domain.User; |
| import top.yangbuyi.service.UserService; |
| |
| import javax.annotation.Resource; |
| import javax.servlet.http.HttpServletRequest; |
| import javax.servlet.http.HttpServletResponse; |
| import java.util.Date; |
| import java.util.List; |
| |
| |
| |
| |
| |
| |
| |
| |
| @RestController |
| public class excelController { |
| |
| |
| @Resource |
| private UserService userService; |
| |
| @Resource |
| HttpServletResponse httpServletResponse; |
| |
| @Resource |
| HttpServletRequest httpServletRequest; |
| |
| |
| |
| |
| @GetMapping |
| public String excel() { |
| try { |
| String fileName = "测试" + new Date() + ".xlsx"; |
| |
| List<User> list = this.userService.list(); |
| |
| new ExportExcel("测试", User.class).setDataList(list).write(httpServletResponse, fileName, httpServletRequest).dispose(); |
| } catch (Exception e) { |
| log.error("测试: {}", e); |
| } |
| return ""; |
| } |
| } |


完结。。。。。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南