| | |
| | | import cn.hutool.core.util.EnumUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.alibaba.excel.annotation.ExcelProperty; |
| | | import com.alibaba.excel.metadata.FieldCache; |
| | | import com.alibaba.excel.metadata.FieldWrapper; |
| | | import com.alibaba.excel.util.ClassUtils; |
| | | import com.alibaba.excel.write.handler.SheetWriteHandler; |
| | | import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
| | | import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; |
| | |
| | | import org.dromara.common.core.service.DictService; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.core.utils.StreamUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import org.dromara.common.excel.annotation.ExcelDictFormat; |
| | | import org.dromara.common.excel.annotation.ExcelEnumFormat; |
| | | |
| | |
| | | Sheet sheet = writeSheetHolder.getSheet(); |
| | | // 开始设置下拉框 HSSFWorkbook |
| | | DataValidationHelper helper = sheet.getDataValidationHelper(); |
| | | Field[] fields = writeWorkbookHolder.getClazz().getDeclaredFields(); |
| | | Workbook workbook = writeWorkbookHolder.getWorkbook(); |
| | | int length = fields.length; |
| | | for (int i = 0; i < length; i++) { |
| | | FieldCache fieldCache = ClassUtils.declaredFields(writeWorkbookHolder.getClazz(), writeWorkbookHolder); |
| | | for (Map.Entry<Integer, FieldWrapper> entry : fieldCache.getSortedFieldMap().entrySet()) { |
| | | Integer index = entry.getKey(); |
| | | FieldWrapper wrapper = entry.getValue(); |
| | | Field field = wrapper.getField(); |
| | | // 循环实体中的每个属性 |
| | | // 可选的下拉值 |
| | | List<String> options = new ArrayList<>(); |
| | | if (fields[i].isAnnotationPresent(ExcelDictFormat.class)) { |
| | | if (field.isAnnotationPresent(ExcelDictFormat.class)) { |
| | | // 如果指定了@ExcelDictFormat,则使用字典的逻辑 |
| | | ExcelDictFormat format = fields[i].getDeclaredAnnotation(ExcelDictFormat.class); |
| | | ExcelDictFormat format = field.getDeclaredAnnotation(ExcelDictFormat.class); |
| | | String dictType = format.dictType(); |
| | | String converterExp = format.readConverterExp(); |
| | | if (StrUtil.isNotBlank(dictType)) { |
| | | if (StringUtils.isNotBlank(dictType)) { |
| | | // 如果传递了字典名,则依据字典建立下拉 |
| | | Collection<String> values = Optional.ofNullable(dictService.getAllDictByDictType(dictType)) |
| | | .orElseThrow(() -> new ServiceException(String.format("字典 %s 不存在", dictType))) |
| | | .values(); |
| | | options = new ArrayList<>(values); |
| | | } else if (StrUtil.isNotBlank(converterExp)) { |
| | | } else if (StringUtils.isNotBlank(converterExp)) { |
| | | // 如果指定了确切的值,则直接解析确切的值 |
| | | options = StrUtil.split(converterExp, format.separator(), true, true); |
| | | List<String> strList = StringUtils.splitList(converterExp, format.separator()); |
| | | options = StreamUtils.toList(strList, s -> StringUtils.split(s, "=")[1]); |
| | | } |
| | | } else if (fields[i].isAnnotationPresent(ExcelEnumFormat.class)) { |
| | | } else if (field.isAnnotationPresent(ExcelEnumFormat.class)) { |
| | | // 否则如果指定了@ExcelEnumFormat,则使用枚举的逻辑 |
| | | ExcelEnumFormat format = fields[i].getDeclaredAnnotation(ExcelEnumFormat.class); |
| | | ExcelEnumFormat format = field.getDeclaredAnnotation(ExcelEnumFormat.class); |
| | | List<Object> values = EnumUtil.getFieldValues(format.enumClass(), format.textField()); |
| | | options = StreamUtils.toList(values, String::valueOf); |
| | | } |
| | | if (ObjectUtil.isNotEmpty(options)) { |
| | | // 仅当下拉可选项不为空时执行 |
| | | // 获取列下标,默认为当前循环次数 |
| | | int index = i; |
| | | if (fields[i].isAnnotationPresent(ExcelProperty.class)) { |
| | | // 如果指定了列下标,以指定的为主 |
| | | index = fields[i].getDeclaredAnnotation(ExcelProperty.class).index(); |
| | | } |
| | | if (options.size() > 20) { |
| | | // 这里限制如果可选项大于20,则使用额外表形式 |
| | | dropDownWithSheet(helper, workbook, sheet, index, options); |
| | |
| | | } |
| | | } |
| | | } |
| | | if (CollUtil.isEmpty(dropDownOptions)) { |
| | | return; |
| | | } |
| | | dropDownOptions.forEach(everyOptions -> { |
| | | // 如果传递了下拉框选择器参数 |
| | | if (!everyOptions.getNextOptions().isEmpty()) { |