引言
文件的导入与导出功能扮演着至关重要的角色,特别是在处理大量数据和复杂的表格时。通过整合Spring Boot、Vue、Mybatis-Plus和Easyexcel等先进技术,我们可以构建一个高效、灵活的文件处理系统。其中,Excel作为广泛使用的电子表格软件,其单元格下拉列表功能对于数据录入和校验尤为有用。本文将探讨如何利用这些技术实现文件导入导出,并重点关注Excel单元格下拉列表的模拟与实现,从而提升数据处理的便捷性和准确性。
Excel导出
EasyExcel官方文档
官方文档本身写的非常详细,有需要可以进行查阅。
后端导出
先进行导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.3.3</version>
</dependency>
对象
需要写一个实体类
其中涉及到一些用到的EasyExcel的注解
- @ColumnWidth(30) 列宽设为30,自定义的,放在实体类上面是整个实体类的每个字段的列宽都为30,放在单个字段上则是改字段为30
- @ContentRowHeight(10) 设置数据行的行高为10个单位
- @HeadRowHeight(20) 设置Excel表格的表头行高度为20
- @ExcelIgnore 在导出文件的时候忽略该字段列
- @ExcelProperty("xxx") 导出文件原本标题行字段是数据库内写的字段,加上这个注解就会变为括号内写入的文字
- @DateTimeFormat("yyyy-MM-dd HH:mm:ss") 设置日期格式,自定义
@Data
@TableName("cp_leave_info")
public class CpLeaveEntity{
// 请假的id
// @TableField(value = "id")
@ExcelIgnore
private Integer id;
// 类型
@ExcelProperty(value = "类型", converter = TypeLeaveConverter.class)
private Integer type;
// 开始时间
@ExcelProperty(value = "开始时间")
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date beginTime;
// 结束时间
@ExcelProperty(value = "结束时间")
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date endTime;
// 时长
@ExcelProperty(value = "时长")
private Double duration;
// 请假理由
@ExcelProperty(value = "请假理由")
private String reason;
// 图片
@ExcelProperty(value = "图片")
private String pictures;
// 单位
@ExcelProperty(value = "单位")
private String unit;
/*
审批结果(通过 :1 ; 未通过: 0 :没有:2)
* */
@ExcelProperty(value = "审批结果", converter = ResultLeaveConverter.class)
private Integer examineResult;
// 审批状态
@ExcelProperty(value = "审批状态", converter = StatusLeaveConverter.class)
private Integer examineStatus;
// 创建时间
@ExcelProperty(value = "创建时间")
private Date createTime;
// 更新时间
@ExcelProperty(value = "更新时间")
private Date updateTime;
// 拒绝理由
@ExcelProperty(value = "拒绝理由")
private String reasonsForRefusal;
/**
* 获取登录信息的用户名
*/
@ExcelProperty(value = "用户名")
private String username;
//逻辑删除(删除: -1, 未删除: 0)
@TableLogic
@ExcelIgnore
private Integer isDelete;
}
controller层
文件下载并且失败的时候返回json(默认失败了会返回一个有部分数据的Excel)
这种方法是将Excel文件的生成过程放在后端进行。前端发起一个请求到后端,后端处理数据并生成Excel文件,然后将文件返回给前端