初始化 V1
This commit is contained in:
80
yudao-framework/yudao-spring-boot-starter-excel/pom.xml
Normal file
80
yudao-framework/yudao-spring-boot-starter-excel/pom.xml
Normal file
@@ -0,0 +1,80 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>cn.iocoder.cloud</groupId>
|
||||
<artifactId>yudao-framework</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>yudao-spring-boot-starter-excel</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
<description>Excel 拓展</description>
|
||||
<url>https://github.com/YunaiV/ruoyi-vue-pro</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.cloud</groupId>
|
||||
<artifactId>yudao-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring 核心 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- RPC 远程调用相关 -->
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.cloud</groupId>
|
||||
<artifactId>yudao-spring-boot-starter-rpc</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- Web 相关 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<scope>provided</scope> <!-- 设置为 provided,只有 ExcelUtils 使用 -->
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>jakarta.servlet</groupId>
|
||||
<artifactId>jakarta.servlet-api</artifactId>
|
||||
<scope>provided</scope> <!-- 设置为 provided,只有 ExcelUtils 使用 -->
|
||||
</dependency>
|
||||
|
||||
<!-- 工具类相关 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>easyexcel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-compress</artifactId> <!-- 解决 https://github.com/alibaba/easyexcel/issues/3954 问题 -->
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.cloud</groupId>
|
||||
<artifactId>yudao-spring-boot-starter-biz-ip</artifactId>
|
||||
<optional>true</optional> <!-- 设置为 optional,只有在 AreaConvert 的时候使用 -->
|
||||
</dependency>
|
||||
|
||||
<!-- Test 测试相关 -->
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.cloud</groupId>
|
||||
<artifactId>yudao-spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,18 @@
|
||||
package cn.iocoder.yudao.framework.dict.config;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.biz.system.dict.DictDataCommonApi;
|
||||
import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@AutoConfiguration
|
||||
public class YudaoDictAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@SuppressWarnings("InstantiationOfUtilityClass")
|
||||
public DictFrameworkUtils dictUtils(DictDataCommonApi dictDataApi) {
|
||||
DictFrameworkUtils.init(dictDataApi);
|
||||
return new DictFrameworkUtils();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.iocoder.yudao.framework.dict.config;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.biz.system.dict.DictDataCommonApi;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* 字典用到 Feign 的配置项
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@EnableFeignClients(clients = DictDataCommonApi.class) // 主要是引入相关的 API 服务
|
||||
public class YudaoDictRpcAutoConfiguration {
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package cn.iocoder.yudao.framework.dict.core;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.common.biz.system.dict.DictDataCommonApi;
|
||||
import cn.iocoder.yudao.framework.common.util.cache.CacheUtils;
|
||||
import cn.iocoder.yudao.framework.common.biz.system.dict.dto.DictDataRespDTO;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
|
||||
|
||||
/**
|
||||
* 字典工具类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Slf4j
|
||||
public class DictFrameworkUtils {
|
||||
|
||||
private static DictDataCommonApi dictDataApi;
|
||||
|
||||
/**
|
||||
* 针对 dictType 的字段数据缓存
|
||||
*/
|
||||
private static final LoadingCache<String, List<DictDataRespDTO>> GET_DICT_DATA_CACHE = CacheUtils.buildAsyncReloadingCache(
|
||||
Duration.ofMinutes(1L), // 过期时间 1 分钟
|
||||
new CacheLoader<String, List<DictDataRespDTO>>() {
|
||||
|
||||
@Override
|
||||
public List<DictDataRespDTO> load(String dictType) {
|
||||
return dictDataApi.getDictDataList(dictType).getCheckedData();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
public static void init(DictDataCommonApi dictDataApi) {
|
||||
DictFrameworkUtils.dictDataApi = dictDataApi;
|
||||
log.info("[init][初始化 DictFrameworkUtils 成功]");
|
||||
}
|
||||
|
||||
public static void clearCache() {
|
||||
GET_DICT_DATA_CACHE.invalidateAll();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static String parseDictDataLabel(String dictType, Integer value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
return parseDictDataLabel(dictType, String.valueOf(value));
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static String parseDictDataLabel(String dictType, String value) {
|
||||
List<DictDataRespDTO> dictDatas = GET_DICT_DATA_CACHE.get(dictType);
|
||||
DictDataRespDTO dictData = CollUtil.findOne(dictDatas, data -> Objects.equals(data.getValue(), value));
|
||||
return dictData != null ? dictData.getLabel(): null;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static List<String> getDictDataLabelList(String dictType) {
|
||||
List<DictDataRespDTO> dictDatas = GET_DICT_DATA_CACHE.get(dictType);
|
||||
return convertList(dictDatas, DictDataRespDTO::getLabel);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static String parseDictDataValue(String dictType, String label) {
|
||||
List<DictDataRespDTO> dictDatas = GET_DICT_DATA_CACHE.get(dictType);
|
||||
DictDataRespDTO dictData = CollUtil.findOne(dictDatas, data -> Objects.equals(data.getLabel(), label));
|
||||
return dictData!= null ? dictData.getValue(): null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* 字典数据模块,提供 {@link cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils} 工具类
|
||||
*
|
||||
* 通过将字典缓存在内存中,保证性能
|
||||
*/
|
||||
package cn.iocoder.yudao.framework.dict;
|
||||
@@ -0,0 +1,22 @@
|
||||
package cn.iocoder.yudao.framework.excel.core.annotations;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 字典格式化
|
||||
*
|
||||
* 实现将字典数据的值,格式化成字典数据的标签
|
||||
*/
|
||||
@Target({ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Inherited
|
||||
public @interface DictFormat {
|
||||
|
||||
/**
|
||||
* 例如说,SysDictTypeConstants、InfDictTypeConstants
|
||||
*
|
||||
* @return 字典类型
|
||||
*/
|
||||
String value();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package cn.iocoder.yudao.framework.excel.core.annotations;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 给 Excel 列添加下拉选择数据
|
||||
*
|
||||
* 其中 {@link #dictType()} 和 {@link #functionName()} 二选一
|
||||
*
|
||||
* @author HUIHUI
|
||||
*/
|
||||
@Target({ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Inherited
|
||||
public @interface ExcelColumnSelect {
|
||||
|
||||
/**
|
||||
* @return 字典类型
|
||||
*/
|
||||
String dictType() default "";
|
||||
|
||||
/**
|
||||
* @return 获取下拉数据源的方法名称
|
||||
*/
|
||||
String functionName() default "";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package cn.iocoder.yudao.framework.excel.core.convert;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.iocoder.yudao.framework.ip.core.Area;
|
||||
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
|
||||
import com.alibaba.excel.converters.Converter;
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum;
|
||||
import com.alibaba.excel.metadata.GlobalConfiguration;
|
||||
import com.alibaba.excel.metadata.data.ReadCellData;
|
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Excel 数据地区转换器
|
||||
*
|
||||
* @author HUIHUI
|
||||
*/
|
||||
@Slf4j
|
||||
public class AreaConvert implements Converter<Object> {
|
||||
|
||||
@Override
|
||||
public Class<?> supportJavaTypeKey() {
|
||||
throw new UnsupportedOperationException("暂不支持,也不需要");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellDataTypeEnum supportExcelTypeKey() {
|
||||
throw new UnsupportedOperationException("暂不支持,也不需要");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convertToJavaData(ReadCellData readCellData, ExcelContentProperty contentProperty,
|
||||
GlobalConfiguration globalConfiguration) {
|
||||
// 解析地区编号
|
||||
String label = readCellData.getStringValue();
|
||||
Area area = AreaUtils.parseArea(label);
|
||||
if (area == null) {
|
||||
log.error("[convertToJavaData][label({}) 解析不掉]", label);
|
||||
return null;
|
||||
}
|
||||
// 将 value 转换成对应的属性
|
||||
Class<?> fieldClazz = contentProperty.getField().getType();
|
||||
return Convert.convert(fieldClazz, area.getId());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package cn.iocoder.yudao.framework.excel.core.convert;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
|
||||
import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
|
||||
import com.alibaba.excel.converters.Converter;
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum;
|
||||
import com.alibaba.excel.metadata.GlobalConfiguration;
|
||||
import com.alibaba.excel.metadata.data.ReadCellData;
|
||||
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Excel 数据字典转换器
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Slf4j
|
||||
public class DictConvert implements Converter<Object> {
|
||||
|
||||
@Override
|
||||
public Class<?> supportJavaTypeKey() {
|
||||
throw new UnsupportedOperationException("暂不支持,也不需要");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellDataTypeEnum supportExcelTypeKey() {
|
||||
throw new UnsupportedOperationException("暂不支持,也不需要");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convertToJavaData(ReadCellData readCellData, ExcelContentProperty contentProperty,
|
||||
GlobalConfiguration globalConfiguration) {
|
||||
// 使用字典解析
|
||||
String type = getType(contentProperty);
|
||||
String label = readCellData.getStringValue();
|
||||
String value = DictFrameworkUtils.parseDictDataValue(type, label);
|
||||
if (value == null) {
|
||||
log.error("[convertToJavaData][type({}) 解析不掉 label({})]", type, label);
|
||||
return null;
|
||||
}
|
||||
// 将 String 的 value 转换成对应的属性
|
||||
Class<?> fieldClazz = contentProperty.getField().getType();
|
||||
return Convert.convert(fieldClazz, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WriteCellData<String> convertToExcelData(Object object, ExcelContentProperty contentProperty,
|
||||
GlobalConfiguration globalConfiguration) {
|
||||
// 空时,返回空
|
||||
if (object == null) {
|
||||
return new WriteCellData<>("");
|
||||
}
|
||||
|
||||
// 使用字典格式化
|
||||
String type = getType(contentProperty);
|
||||
String value = String.valueOf(object);
|
||||
String label = DictFrameworkUtils.parseDictDataLabel(type, value);
|
||||
if (label == null) {
|
||||
log.error("[convertToExcelData][type({}) 转换不了 label({})]", type, value);
|
||||
return new WriteCellData<>("");
|
||||
}
|
||||
// 生成 Excel 小表格
|
||||
return new WriteCellData<>(label);
|
||||
}
|
||||
|
||||
private static String getType(ExcelContentProperty contentProperty) {
|
||||
return contentProperty.getField().getAnnotation(DictFormat.class).value();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package cn.iocoder.yudao.framework.excel.core.convert;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
import com.alibaba.excel.converters.Converter;
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum;
|
||||
import com.alibaba.excel.metadata.GlobalConfiguration;
|
||||
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
||||
|
||||
/**
|
||||
* Excel Json 转换器
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class JsonConvert implements Converter<Object> {
|
||||
|
||||
@Override
|
||||
public Class<?> supportJavaTypeKey() {
|
||||
throw new UnsupportedOperationException("暂不支持,也不需要");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellDataTypeEnum supportExcelTypeKey() {
|
||||
throw new UnsupportedOperationException("暂不支持,也不需要");
|
||||
}
|
||||
|
||||
@Override
|
||||
public WriteCellData<String> convertToExcelData(Object value, ExcelContentProperty contentProperty,
|
||||
GlobalConfiguration globalConfiguration) {
|
||||
// 生成 Excel 小表格
|
||||
return new WriteCellData<>(JsonUtils.toJsonString(value));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package cn.iocoder.yudao.framework.excel.core.convert;
|
||||
|
||||
import com.alibaba.excel.converters.Converter;
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum;
|
||||
import com.alibaba.excel.metadata.GlobalConfiguration;
|
||||
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
/**
|
||||
* 金额转换器
|
||||
*
|
||||
* 金额单位:分
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class MoneyConvert implements Converter<Integer> {
|
||||
|
||||
@Override
|
||||
public Class<?> supportJavaTypeKey() {
|
||||
throw new UnsupportedOperationException("暂不支持,也不需要");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellDataTypeEnum supportExcelTypeKey() {
|
||||
throw new UnsupportedOperationException("暂不支持,也不需要");
|
||||
}
|
||||
|
||||
@Override
|
||||
public WriteCellData<String> convertToExcelData(Integer value, ExcelContentProperty contentProperty,
|
||||
GlobalConfiguration globalConfiguration) {
|
||||
BigDecimal result = BigDecimal.valueOf(value)
|
||||
.divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
|
||||
return new WriteCellData<>(result.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package cn.iocoder.yudao.framework.excel.core.function;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Excel 列下拉数据源获取接口
|
||||
*
|
||||
* 为什么不直接解析字典还搞个接口?考虑到有的下拉数据不是从字典中获取的所有需要做一个兼容
|
||||
|
||||
* @author HUIHUI
|
||||
*/
|
||||
public interface ExcelColumnSelectFunction {
|
||||
|
||||
/**
|
||||
* 获得方法名称
|
||||
*
|
||||
* @return 方法名称
|
||||
*/
|
||||
String getName();
|
||||
|
||||
/**
|
||||
* 获得列下拉数据源
|
||||
*
|
||||
* @return 下拉数据源
|
||||
*/
|
||||
List<String> getOptions();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package cn.iocoder.yudao.framework.excel.core.handler;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.hutool.poi.excel.ExcelUtil;
|
||||
import cn.iocoder.yudao.framework.common.core.KeyValue;
|
||||
import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
|
||||
import cn.iocoder.yudao.framework.excel.core.annotations.ExcelColumnSelect;
|
||||
import cn.iocoder.yudao.framework.excel.core.function.ExcelColumnSelectFunction;
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import com.alibaba.excel.write.handler.SheetWriteHandler;
|
||||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
|
||||
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.ss.util.CellRangeAddressList;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
|
||||
|
||||
/**
|
||||
* 基于固定 sheet 实现下拉框
|
||||
*
|
||||
* @author HUIHUI
|
||||
*/
|
||||
@Slf4j
|
||||
public class SelectSheetWriteHandler implements SheetWriteHandler {
|
||||
|
||||
/**
|
||||
* 数据起始行从 0 开始
|
||||
*
|
||||
* 约定:本项目第一行有标题所以从 1 开始如果您的 Excel 有多行标题请自行更改
|
||||
*/
|
||||
public static final int FIRST_ROW = 1;
|
||||
/**
|
||||
* 下拉列需要创建下拉框的行数,默认两千行如需更多请自行调整
|
||||
*/
|
||||
public static final int LAST_ROW = 2000;
|
||||
|
||||
private static final String DICT_SHEET_NAME = "字典sheet";
|
||||
|
||||
/**
|
||||
* key: 列 value: 下拉数据源
|
||||
*/
|
||||
private final Map<Integer, List<String>> selectMap = new HashMap<>();
|
||||
|
||||
public SelectSheetWriteHandler(Class<?> head) {
|
||||
// 解析下拉数据
|
||||
int colIndex = 0;
|
||||
for (Field field : head.getDeclaredFields()) {
|
||||
if (field.isAnnotationPresent(ExcelColumnSelect.class)) {
|
||||
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
|
||||
if (excelProperty != null && excelProperty.index() != -1) {
|
||||
colIndex = excelProperty.index();
|
||||
}
|
||||
getSelectDataList(colIndex, field);
|
||||
}
|
||||
colIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得下拉数据,并添加到 {@link #selectMap} 中
|
||||
*
|
||||
* @param colIndex 列索引
|
||||
* @param field 字段
|
||||
*/
|
||||
private void getSelectDataList(int colIndex, Field field) {
|
||||
ExcelColumnSelect columnSelect = field.getAnnotation(ExcelColumnSelect.class);
|
||||
String dictType = columnSelect.dictType();
|
||||
String functionName = columnSelect.functionName();
|
||||
Assert.isTrue(ObjectUtil.isNotEmpty(dictType) || ObjectUtil.isNotEmpty(functionName),
|
||||
"Field({}) 的 @ExcelColumnSelect 注解,dictType 和 functionName 不能同时为空", field.getName());
|
||||
|
||||
// 情况一:使用 dictType 获得下拉数据
|
||||
if (StrUtil.isNotEmpty(dictType)) { // 情况一: 字典数据 (默认)
|
||||
selectMap.put(colIndex, DictFrameworkUtils.getDictDataLabelList(dictType));
|
||||
return;
|
||||
}
|
||||
|
||||
// 情况二:使用 functionName 获得下拉数据
|
||||
Map<String, ExcelColumnSelectFunction> functionMap = SpringUtil.getApplicationContext().getBeansOfType(ExcelColumnSelectFunction.class);
|
||||
ExcelColumnSelectFunction function = CollUtil.findOne(functionMap.values(), item -> item.getName().equals(functionName));
|
||||
Assert.notNull(function, "未找到对应的 function({})", functionName);
|
||||
selectMap.put(colIndex, function.getOptions());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
|
||||
if (CollUtil.isEmpty(selectMap)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. 获取相应操作对象
|
||||
DataValidationHelper helper = writeSheetHolder.getSheet().getDataValidationHelper(); // 需要设置下拉框的 sheet 页的数据验证助手
|
||||
Workbook workbook = writeWorkbookHolder.getWorkbook(); // 获得工作簿
|
||||
List<KeyValue<Integer, List<String>>> keyValues = convertList(selectMap.entrySet(), entry -> new KeyValue<>(entry.getKey(), entry.getValue()));
|
||||
keyValues.sort(Comparator.comparing(item -> item.getValue().size())); // 升序不然创建下拉会报错
|
||||
|
||||
// 2. 创建数据字典的 sheet 页
|
||||
Sheet dictSheet = workbook.createSheet(DICT_SHEET_NAME);
|
||||
for (KeyValue<Integer, List<String>> keyValue : keyValues) {
|
||||
int rowLength = keyValue.getValue().size();
|
||||
// 2.1 设置字典 sheet 页的值,每一列一个字典项
|
||||
for (int i = 0; i < rowLength; i++) {
|
||||
Row row = dictSheet.getRow(i);
|
||||
if (row == null) {
|
||||
row = dictSheet.createRow(i);
|
||||
}
|
||||
row.createCell(keyValue.getKey()).setCellValue(keyValue.getValue().get(i));
|
||||
}
|
||||
// 2.2 设置单元格下拉选择
|
||||
setColumnSelect(writeSheetHolder, workbook, helper, keyValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置单元格下拉选择
|
||||
*/
|
||||
private static void setColumnSelect(WriteSheetHolder writeSheetHolder, Workbook workbook, DataValidationHelper helper,
|
||||
KeyValue<Integer, List<String>> keyValue) {
|
||||
// 1.1 创建可被其他单元格引用的名称
|
||||
Name name = workbook.createName();
|
||||
String excelColumn = ExcelUtil.indexToColName(keyValue.getKey());
|
||||
// 1.2 下拉框数据来源 eg:字典sheet!$B1:$B2
|
||||
String refers = DICT_SHEET_NAME + "!$" + excelColumn + "$1:$" + excelColumn + "$" + keyValue.getValue().size();
|
||||
name.setNameName("dict" + keyValue.getKey()); // 设置名称的名字
|
||||
name.setRefersToFormula(refers); // 设置公式
|
||||
|
||||
// 2.1 设置约束
|
||||
DataValidationConstraint constraint = helper.createFormulaListConstraint("dict" + keyValue.getKey()); // 设置引用约束
|
||||
// 设置下拉单元格的首行、末行、首列、末列
|
||||
CellRangeAddressList rangeAddressList = new CellRangeAddressList(FIRST_ROW, LAST_ROW,
|
||||
keyValue.getKey(), keyValue.getKey());
|
||||
DataValidation validation = helper.createValidation(constraint, rangeAddressList);
|
||||
if (validation instanceof HSSFDataValidation) {
|
||||
validation.setSuppressDropDownArrow(false);
|
||||
} else {
|
||||
validation.setSuppressDropDownArrow(true);
|
||||
validation.setShowErrorBox(true);
|
||||
}
|
||||
// 2.2 阻止输入非下拉框的值
|
||||
validation.setErrorStyle(DataValidation.ErrorStyle.STOP);
|
||||
validation.createErrorBox("提示", "此值不存在于下拉选择中!");
|
||||
// 2.3 添加下拉框约束
|
||||
writeSheetHolder.getSheet().addValidationData(validation);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package cn.iocoder.yudao.framework.excel.core.util;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
|
||||
import cn.iocoder.yudao.framework.excel.core.handler.SelectSheetWriteHandler;
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.converters.longconverter.LongStringConverter;
|
||||
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Excel 工具类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class ExcelUtils {
|
||||
|
||||
/**
|
||||
* 将列表以 Excel 响应给前端
|
||||
*
|
||||
* @param response 响应
|
||||
* @param filename 文件名
|
||||
* @param sheetName Excel sheet 名
|
||||
* @param head Excel head 头
|
||||
* @param data 数据列表哦
|
||||
* @param <T> 泛型,保证 head 和 data 类型的一致性
|
||||
* @throws IOException 写入失败的情况
|
||||
*/
|
||||
public static <T> void write(HttpServletResponse response, String filename, String sheetName,
|
||||
Class<T> head, List<T> data) throws IOException {
|
||||
// 输出 Excel
|
||||
EasyExcel.write(response.getOutputStream(), head)
|
||||
.autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理
|
||||
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 基于 column 长度,自动适配。最大 255 宽度
|
||||
.registerWriteHandler(new SelectSheetWriteHandler(head)) // 基于固定 sheet 实现下拉框
|
||||
.registerConverter(new LongStringConverter()) // 避免 Long 类型丢失精度
|
||||
.sheet(sheetName).doWrite(data);
|
||||
// 设置 header 和 contentType。写在最后的原因是,避免报错时,响应 contentType 已经被修改了
|
||||
response.addHeader("Content-Disposition", "attachment;filename=" + HttpUtils.encodeUtf8(filename));
|
||||
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
|
||||
}
|
||||
|
||||
public static <T> List<T> read(MultipartFile file, Class<T> head) throws IOException {
|
||||
return EasyExcel.read(file.getInputStream(), head, null)
|
||||
.autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理
|
||||
.doReadAllSync();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* 基于 EasyExcel 实现 Excel 相关的操作
|
||||
*/
|
||||
package cn.iocoder.yudao.framework.excel;
|
||||
@@ -0,0 +1,2 @@
|
||||
cn.iocoder.yudao.framework.dict.config.YudaoDictRpcAutoConfiguration
|
||||
cn.iocoder.yudao.framework.dict.config.YudaoDictAutoConfiguration
|
||||
@@ -0,0 +1,61 @@
|
||||
package cn.iocoder.yudao.framework.dict.core.util;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.biz.system.dict.DictDataCommonApi;
|
||||
import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
||||
import cn.iocoder.yudao.framework.common.biz.system.dict.dto.DictDataRespDTO;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* {@link DictFrameworkUtils} 的单元测试
|
||||
*/
|
||||
public class DictFrameworkUtilsTest extends BaseMockitoUnitTest {
|
||||
|
||||
@Mock
|
||||
private DictDataCommonApi dictDataApi;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
DictFrameworkUtils.init(dictDataApi);
|
||||
DictFrameworkUtils.clearCache();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseDictDataLabel() {
|
||||
// mock 数据
|
||||
List<DictDataRespDTO> dictDatas = List.of(
|
||||
randomPojo(DictDataRespDTO.class, o -> o.setDictType("animal").setValue("cat").setLabel("猫")),
|
||||
randomPojo(DictDataRespDTO.class, o -> o.setDictType("animal").setValue("dog").setLabel("狗"))
|
||||
);
|
||||
// mock 方法
|
||||
when(dictDataApi.getDictDataList(eq("animal"))).thenReturn(success(dictDatas));
|
||||
|
||||
// 断言返回值
|
||||
assertEquals("狗", DictFrameworkUtils.parseDictDataLabel("animal", "dog"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseDictDataValue() {
|
||||
// mock 数据
|
||||
List<DictDataRespDTO> dictDatas = List.of(
|
||||
randomPojo(DictDataRespDTO.class, o -> o.setDictType("animal").setValue("cat").setLabel("猫")),
|
||||
randomPojo(DictDataRespDTO.class, o -> o.setDictType("animal").setValue("dog").setLabel("狗"))
|
||||
);
|
||||
// mock 方法
|
||||
when(dictDataApi.getDictDataList(eq("animal"))).thenReturn(success(dictDatas));
|
||||
|
||||
// 断言返回值
|
||||
assertEquals("dog", DictFrameworkUtils.parseDictDataValue("animal", "狗"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user