本文使用的emoji: 💧 💦 ☔
最近遇到一个需求,需要对页面的列表数据做导出操作,考虑了很多实现方案之后,最终选择了hutool的ExcelWriter + Spring自带的Reflet包来实现这个功能,采用这种方式的主要原因是对现有代码改动较少,并且可以无缝切入系统,实现各个模块的导出操作,设计思路如下:

如上图所示,首先我们定义一个接口,此接口完成Excel导出功能,在ExcelExportService 中实现功能。
话不多说,上代码:

在导出Excel之前,我们需要先定义ExcelExportEnum,此枚举存储了我们后端需要导出的模块和模板名称
-
- @Getter
- @RequiredArgsConstructor
- public enum ExcelExportEnum {
-
- /**
- * 模块
- */
- MODULE_A("moduleAService", "模块A");
-
- private final String key;
- private final String name;
-
- }
- 复制代码
首先,我们需要对入参进行校验,查看传入的 MODULE_NAME 是否和后端Enum的一致,并且我们需要对参数中的自定义方法名进行提取,我们通过ExcelExportEnum来获取导出的module的Service名称
- if (!map.containsKey(MODULE_NAME)) {
- throw new BaseException("缺少参数:moduleName");
- }
- String moduleName = map.get(MODULE_NAME).toString();
- if (!EnumUtil.contains(ExcelExportEnum.class, moduleName)) {
- throw new BaseException("模块查找失败");
- }
- String functionName = DEFAULT_FUNCTION_NAME;
- //如果传了自定义方法名
- if (map.containsKey(CUSTOM_FUNCTION_NAME)) {
- functionName = map.get(CUSTOM_FUNCTION_NAME).toString();
- }
- String serviceName = ExcelExportEnum.valueOf(moduleName).getKey();
- String tableName = ExcelExportEnum.valueOf(moduleName).getName();
- 复制代码
- Map<String, Method> methodMap = Arrays.stream(SpringContextHolder.getBean(serviceName).getClass().getMethods()).collect(Collectors.toMap(Method::getName, Function.identity(), (key1, key2) -> key2));
- 复制代码
在上述代码中SpringContextHolder.getBean(serviceName).getClass().getMethods()方法,首先通过getBean来获取 第一步中得到的ServiceName的Java bean。然后通过Class.getMethods()方法获取此service中的所有方法
最后,使用Arrays.stream方法,将module对应的Service中的所有method获取到,并放入methodMap中,供后续代码使用
- Object o;
- try {
- o = methodMap.get(functionName).getParameterTypes()[0].getDeclaredConstructor().newInstance();
- } catch (Exception e) {
- throw new BaseException(e);
- }
- 复制代码
通过Method.getParameterTypes()方法,将第二步中获取的method的入参的Class取到,并赋值给对象Object o,供后续调用分页列表接口传递入参匹配Class使用
- Map<String, Object> param = (Map<String, Object>) map.computeIfAbsent(PARAM, k -> new HashMap<String, Object>(4));
- param.put("limit", -1);
- CopyOptions copyOptions = new CopyOptions();
- copyOptions.setIgnoreError(true);
- PageUtils page = (PageUtils) methodMap.get(functionName).invoke(SpringContextHolder.getBean(serviceName), BeanUtil.mapToBean(param, o.getClass(), true, copyOptions));
- if (CollUtil.isEmpty(page.getList())) {
- throw new BaseException("数据获取失败");
- }
- Object resultObject = page.getList().get(0);
- ExcelUtils utils = new ExcelUtils(resultObject.getClass());
- utils.exportExcel(page.getList(), response, tableName, moduleName);
- 复制代码
可以看到,在上述导出方法中,我们使用了Method.invoke方法,并且设置转义忽略
使用hutool工具包输出到流
- // 通过工具类创建writer,默认创建xls格式
- ExcelWriter writer = ExcelUtil.getWriter();
- //创建xlsx格式的
- //ExcelWriter writer = ExcelUtil.getWriter(true);
- // 一次性写出内容,使用默认样式,强制输出标题
- writer.write(rows, true);
- //out为OutputStream,需要写出到的目标流
- writer.flush(out);
- // 关闭writer,释放内存
- writer.close();
- 复制代码
注意
ExcelUtil.getWriter()默认创建xls格式的Excel,因此写出到客户端也需要自定义文件名为XXX.xls,否则会出现文件损坏的提示。 若想生成xlsx格式,请使用ExcelUtil.getWriter(true)创建。