2.9 KiB
2.9 KiB
rdms-spring-boot-starter-excel
模块定位
这是一个面向业务的 Excel 能力模块,核心目标是:
- 降低导入导出开发成本(统一工具类)
- 让业务字段和 Excel 展示语义解耦(注解 + Converter)
- 把字典能力接入 Excel 场景(值与标签互转、校验、下拉选项)
设计思路
- 统一入口:通过
ExcelUtils封装读写,Controller 只关心 VO 和数据。 - 注解驱动:通过
@DictFormat、@ExcelColumnSelect把“字段语义”放在 VO 上,而不是散落在业务代码里。 - 转换器分层:
DictConvert、AreaConvert、JsonConvert、MoneyConvert分别处理不同类型转换。 - 字典缓存:
DictFrameworkUtils基于缓存读取字典,减少重复远程调用。 - 导出体验:自动列宽、下拉选项、Long 防精度丢失等细节统一在模块内处理。
核心类
com.njcn.rdms.framework.excel.core.util.ExcelUtilscom.njcn.rdms.framework.excel.core.convert.DictConvertcom.njcn.rdms.framework.excel.core.handler.SelectSheetWriteHandlercom.njcn.rdms.framework.dict.core.DictFrameworkUtilscom.njcn.rdms.framework.dict.validation.InDict
示例:用户导入导出(含字典转换)
1. 定义 Excel VO
import cn.idev.excel.annotation.ExcelProperty;
import com.njcn.rdms.framework.excel.core.annotations.DictFormat;
import com.njcn.rdms.framework.excel.core.convert.DictConvert;
import lombok.Data;
@Data
public class UserImportExcelVO {
@ExcelProperty("登录名称")
private String username;
@ExcelProperty(value = "用户性别", converter = DictConvert.class)
@DictFormat("USER_SEX")
private Integer sex;
@ExcelProperty(value = "账号状态", converter = DictConvert.class)
@DictFormat("COMMON_STATUS")
private Integer status;
}
2. 导出模板
import com.njcn.rdms.framework.excel.core.util.ExcelUtils;
import jakarta.servlet.http.HttpServletResponse;
public void exportTemplate(HttpServletResponse response) throws Exception {
ExcelUtils.write(response, "用户导入模板.xls", "用户列表", UserImportExcelVO.class, List.of());
}
3. 导入解析
import com.njcn.rdms.framework.excel.core.util.ExcelUtils;
import org.springframework.web.multipart.MultipartFile;
public List<UserImportExcelVO> importExcel(MultipartFile file) throws Exception {
return ExcelUtils.read(file, UserImportExcelVO.class);
}
说明:
- 导入时
DictConvert会把 Excel 标签值转回字典 value(如“男”->1)。 - 导出时
DictConvert会把字典 value 转成可读标签(如1->“男”)。
适用场景
- 后台管理常规 Excel 导入导出
- 字典字段较多、希望自动做值/标签转换
- 需要生成带下拉选项的导入模板
不适用场景
- 超大规模离线数据处理(建议走专用批处理链路)
- 复杂流式处理或多工作簿复杂编排(建议单独实现)