| | |
| | | import com.ycl.common.group.Add; |
| | | import com.ycl.common.group.Update; |
| | | import com.ycl.common.utils.ProjectCodeGenerator; |
| | | import com.ycl.common.utils.excel.OutputExcelUtils; |
| | | import com.ycl.domain.excel.ProjectExcelTemplate; |
| | | import com.ycl.domain.form.DocumentInfoForm; |
| | | import com.ycl.domain.form.ProjectInfoForm; |
| | | import com.ycl.domain.query.ProjectInfoQuery; |
| | |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.validation.constraints.NotEmpty; |
| | | import java.io.IOException; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | public Result getManagerFlag(@PathVariable("recordId") Integer recordId) { |
| | | return projectInfoService.getManagerFlag(recordId); |
| | | } |
| | | |
| | | /** |
| | | * 导出模板 |
| | | * @param response |
| | | * @return |
| | | */ |
| | | @PostMapping("/export/template") |
| | | public void exportTemplate(HttpServletResponse response, |
| | | @RequestBody List<String> fieldList |
| | | ) throws IOException { |
| | | OutputExcelUtils.export(response, "导入模板", "项目信息", null, ProjectExcelTemplate.class ,fieldList); |
| | | } |
| | | } |
| | |
| | | <groupId>javax.servlet</groupId> |
| | | <artifactId>javax.servlet-api</artifactId> |
| | | </dependency> |
| | | <!-- easy excel --> |
| | | <dependency> |
| | | <groupId>com.alibaba</groupId> |
| | | <artifactId>easyexcel</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>cn.hutool</groupId> |
| | | <artifactId>hutool-crypto</artifactId> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package com.ycl.common.utils.excel; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.IdUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.ZipUtil; |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; |
| | | import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; |
| | | import com.ycl.common.utils.excel.convert.ExcelBigNumberConvert; |
| | | import com.ycl.common.utils.excel.core.CellMergeStrategy; |
| | | import com.ycl.common.utils.excel.core.DropDownOptions; |
| | | import com.ycl.common.utils.excel.core.ExcelDownHandler; |
| | | import com.ycl.common.utils.file.FileUtils; |
| | | import org.apache.commons.codec.Charsets; |
| | | |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.validation.constraints.NotNull; |
| | | import java.io.*; |
| | | import java.lang.reflect.Field; |
| | | import java.net.URLEncoder; |
| | | import java.nio.file.Files; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * @Author: ljx |
| | | * @CreateTime: 2024-10-18 10:13 |
| | | */ |
| | | |
| | | public class OutputExcelUtils { |
| | | |
| | | /** |
| | | * 忽略部分导出字段方法 |
| | | * @param response |
| | | * @param fileName 文件名称 |
| | | * @param sheetName sheet名称 |
| | | * @param dataList 需要导出的数据 |
| | | * @param clazz 类 |
| | | * @param |
| | | * @param <T> |
| | | * @throws IOException |
| | | */ |
| | | public static <T> void export(HttpServletResponse response, String fileName, String sheetName, List<T> dataList, Class<T> clazz, List<String> fieldNames) throws IOException { |
| | | response.setContentType("application/zip"); |
| | | response.setCharacterEncoding(Charsets.UTF_8.name()); |
| | | fileName = URLEncoder.encode(fileName, Charsets.UTF_8); |
| | | response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".zip"); |
| | | Set<Integer> selectedIndexes = getSelectFields(fieldNames, clazz); |
| | | |
| | | //下载模板 |
| | | downloadTemplate(response, sheetName, dataList, clazz, selectedIndexes); |
| | | |
| | | |
| | | // 临时向资源文件夹写 名为template的文件夹 |
| | | // ClassPathResource classPathResource = new ClassPathResource("/template/test.xls"); |
| | | // File file = classPathResource.getFile(); |
| | | // try(FileOutputStream fileOutputStream = new FileOutputStream(file,false)) { |
| | | // EasyExcel.write(fileOutputStream, clazz).sheet(sheetName).doWrite(dataList); |
| | | // } |
| | | // |
| | | // ClassPathResource classPathResource1 = new ClassPathResource("/template"); |
| | | // File zip = ZipUtil.zip(classPathResource1.getFile()); |
| | | // byte[] bytes; |
| | | // try (FileInputStream fileInputStream = new FileInputStream(zip)) { |
| | | // bytes = fileInputStream.readAllBytes(); |
| | | // } |
| | | // response.getOutputStream().write(bytes); |
| | | |
| | | // if (selectedIndexes.size() > 0) { |
| | | // EasyExcel.write(response.getOutputStream(), clazz).excludeColumnIndexes(selectedIndexes).sheet(sheetName).doWrite(dataList); |
| | | // } else { |
| | | // EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(dataList); |
| | | // } |
| | | } |
| | | |
| | | private synchronized static <T> void downloadTemplate(HttpServletResponse response, String sheetName, List<T> dataList, Class<T> clazz, Set<Integer> columnIndex) throws IOException { |
| | | |
| | | File tempDir = null; |
| | | try { |
| | | // 创建临时目录 |
| | | tempDir = Files.createTempDirectory("temp").toFile(); |
| | | |
| | | File templateDir = new File(tempDir, "template"); |
| | | if (!templateDir.exists()) { |
| | | templateDir.mkdirs(); |
| | | } |
| | | |
| | | // 创建 Excel 文件 |
| | | File excelFile = new File(templateDir, "excel.xlsx"); |
| | | if (!excelFile.exists()) { |
| | | excelFile.createNewFile(); |
| | | } |
| | | |
| | | // 写入 Excel 模板数据 |
| | | try (FileOutputStream fileOutputStream = new FileOutputStream(excelFile, false)) { |
| | | EasyExcel.write(fileOutputStream, clazz).includeColumnIndexes(columnIndex).sheet(sheetName).doWrite(dataList); |
| | | } |
| | | |
| | | // 创建附件目录 |
| | | File attachmentDir = new File(templateDir, "attachment"); |
| | | if (!attachmentDir.exists()) { |
| | | attachmentDir.mkdirs(); |
| | | } |
| | | |
| | | // 打包 ZIP 文件 |
| | | File zipFile = ZipUtil.zip(templateDir); |
| | | byte[] zipBytes = Files.readAllBytes(zipFile.toPath()); |
| | | |
| | | // 将 ZIP 文件写入响应 |
| | | try(ServletOutputStream outputStream = response.getOutputStream()) { |
| | | outputStream.write(zipBytes); |
| | | } |
| | | } finally { |
| | | deleteDirectoryOrFile(tempDir); |
| | | } |
| | | } |
| | | |
| | | private static void deleteDirectoryOrFile(File file) { |
| | | if (ObjectUtil.isNull(file)) { |
| | | return; |
| | | } |
| | | |
| | | if (file.isDirectory()) { |
| | | File[] files = file.listFiles(); |
| | | if (files != null) { |
| | | for (File f : files) { |
| | | deleteDirectoryOrFile(f); |
| | | } |
| | | } |
| | | } |
| | | file.delete(); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 导出模板 |
| | | * @param response |
| | | * @param fileName |
| | | * @param sheetName |
| | | * @param dataList |
| | | * @param clazz |
| | | * @param fieldNames |
| | | */ |
| | | public static <T> void exportTemplate(HttpServletResponse response,String fileName, String sheetName, List<T> dataList, Class<T> clazz, List<String> fieldNames) throws IOException { |
| | | Set<Integer> selectedIndexes = getSelectFields(fieldNames, clazz); |
| | | |
| | | resetResponse(fileName, response); |
| | | |
| | | exportExcel(dataList, sheetName, clazz, false, response.getOutputStream(), null, selectedIndexes); |
| | | } |
| | | |
| | | public static <T> @NotNull Set<Integer> getSelectFields(List<String> fieldNames, Class<T> clazz) { |
| | | Set<Integer> selectedIndexes = new HashSet<>(); |
| | | if (CollUtil.isNotEmpty(fieldNames)) { |
| | | // 反射获取字段属性 |
| | | Field[] declaredFields = clazz.getDeclaredFields(); |
| | | // // 匹配需要导入的字段 |
| | | for (int i = 0; i < declaredFields.length; i++) { |
| | | if (fieldNames.contains(declaredFields[i].getName())) { |
| | | // 获取需要导入的字段下标 |
| | | selectedIndexes.add(i); |
| | | } |
| | | } |
| | | } |
| | | return selectedIndexes; |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 导出excel |
| | | * |
| | | * @param list 导出数据集合 |
| | | * @param sheetName 工作表的名称 |
| | | * @param clazz 实体类 |
| | | * @param merge 是否合并单元格 |
| | | * @param os 输出流 |
| | | */ |
| | | public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, |
| | | OutputStream os, List<DropDownOptions> options, Set<Integer> selectedIndexes) { |
| | | ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz) |
| | | .autoCloseStream(false) |
| | | .includeColumnIndexes(selectedIndexes) |
| | | // 自动适配 |
| | | .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) |
| | | // 大数值自动转换 防止失真 |
| | | .registerConverter(new ExcelBigNumberConvert()) |
| | | .sheet(sheetName); |
| | | if (merge) { |
| | | // 合并处理器 |
| | | builder.registerWriteHandler(new CellMergeStrategy(list, true)); |
| | | } |
| | | // 添加下拉框操作 |
| | | builder.registerWriteHandler(new ExcelDownHandler(options)); |
| | | builder.doWrite(list); |
| | | } |
| | | |
| | | /** |
| | | * 重置响应体 |
| | | */ |
| | | private static void resetResponse(String sheetName, HttpServletResponse response) throws UnsupportedEncodingException { |
| | | String filename = encodingFilename(sheetName); |
| | | FileUtils.setAttachmentResponseHeader(response, filename); |
| | | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8"); |
| | | } |
| | | |
| | | /** |
| | | * 编码文件名 |
| | | */ |
| | | public static String encodingFilename(String filename) { |
| | | return IdUtil.fastSimpleUUID() + "_" + filename + ".xlsx"; |
| | | } |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.annotation; |
| | | |
| | | import com.ycl.common.utils.excel.core.CellMergeStrategy; |
| | | |
| | | import java.lang.annotation.*; |
| | | |
| | | /** |
| | | * excel 列单元格合并(合并列相同项) |
| | | * <p> |
| | | * 需搭配 {@link CellMergeStrategy} 策略使用 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Target(ElementType.FIELD) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Inherited |
| | | public @interface CellMerge { |
| | | |
| | | /** |
| | | * col index |
| | | */ |
| | | int index() default -1; |
| | | |
| | | /** |
| | | * 合并需要依赖的其他字段名称 |
| | | */ |
| | | String[] mergeBy() default {}; |
| | | |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.annotation; |
| | | |
| | | |
| | | import com.ycl.common.utils.excel.utils.StringUtils; |
| | | |
| | | import java.lang.annotation.*; |
| | | |
| | | /** |
| | | * 字典格式化 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Target({ElementType.FIELD}) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Inherited |
| | | public @interface ExcelDictFormat { |
| | | /** |
| | | * 如果是字典类型,请设置字典的type值 (如: sys_user_sex) |
| | | */ |
| | | String dictType() default ""; |
| | | |
| | | /** |
| | | * 读取内容转表达式 (如: 0=男,1=女,2=未知) |
| | | */ |
| | | String readConverterExp() default ""; |
| | | |
| | | /** |
| | | * 分隔符,读取字符串组内容 |
| | | */ |
| | | String separator() default StringUtils.SEPARATOR; |
| | | |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.annotation; |
| | | |
| | | import java.lang.annotation.*; |
| | | |
| | | /** |
| | | * 枚举格式化 |
| | | * |
| | | * @author Liang |
| | | */ |
| | | @Target({ElementType.FIELD}) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Inherited |
| | | public @interface ExcelEnumFormat { |
| | | |
| | | /** |
| | | * 字典枚举类型 |
| | | */ |
| | | Class<? extends Enum<?>> enumClass(); |
| | | |
| | | /** |
| | | * 字典枚举类中对应的code属性名称,默认为code |
| | | */ |
| | | String codeField() default "code"; |
| | | |
| | | /** |
| | | * 字典枚举类中对应的text属性名称,默认为text |
| | | */ |
| | | String textField() default "text"; |
| | | |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.convert; |
| | | |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | 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; |
| | | |
| | | import java.math.BigDecimal; |
| | | |
| | | /** |
| | | * 大数值转换 |
| | | * Excel 数值长度位15位 大于15位的数值转换位字符串 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | public class ExcelBigNumberConvert implements Converter<Long> { |
| | | |
| | | @Override |
| | | public Class<Long> supportJavaTypeKey() { |
| | | return Long.class; |
| | | } |
| | | |
| | | @Override |
| | | public CellDataTypeEnum supportExcelTypeKey() { |
| | | return CellDataTypeEnum.STRING; |
| | | } |
| | | |
| | | @Override |
| | | public Long convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { |
| | | return Convert.toLong(cellData.getData()); |
| | | } |
| | | |
| | | @Override |
| | | public WriteCellData<Object> convertToExcelData(Long object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { |
| | | if (ObjectUtil.isNotNull(object)) { |
| | | String str = Convert.toStr(object); |
| | | if (str.length() > 15) { |
| | | return new WriteCellData<>(str); |
| | | } |
| | | } |
| | | WriteCellData<Object> cellData = new WriteCellData<>(new BigDecimal(object)); |
| | | cellData.setType(CellDataTypeEnum.NUMBER); |
| | | return cellData; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.convert; |
| | | |
| | | import cn.hutool.core.annotation.AnnotationUtil; |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | 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 com.ycl.common.utils.excel.annotation.ExcelEnumFormat; |
| | | import com.ycl.common.utils.reflect.ReflectUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 枚举格式化转换处理 |
| | | * |
| | | * @author Liang |
| | | */ |
| | | @Slf4j |
| | | public class ExcelEnumConvert implements Converter<Object> { |
| | | |
| | | @Override |
| | | public Class<Object> supportJavaTypeKey() { |
| | | return Object.class; |
| | | } |
| | | |
| | | @Override |
| | | public CellDataTypeEnum supportExcelTypeKey() { |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { |
| | | cellData.checkEmpty(); |
| | | // Excel中填入的是枚举中指定的描述 |
| | | Object textValue = switch (cellData.getType()) { |
| | | case STRING, DIRECT_STRING, RICH_TEXT_STRING -> cellData.getStringValue(); |
| | | case NUMBER -> cellData.getNumberValue(); |
| | | case BOOLEAN -> cellData.getBooleanValue(); |
| | | default -> throw new IllegalArgumentException("单元格类型异常!"); |
| | | }; |
| | | // 如果是空值 |
| | | if (ObjectUtil.isNull(textValue)) { |
| | | return null; |
| | | } |
| | | Map<Object, String> enumCodeToTextMap = beforeConvert(contentProperty); |
| | | // 从Java输出至Excel是code转text |
| | | // 因此从Excel转Java应该将text与code对调 |
| | | Map<Object, Object> enumTextToCodeMap = new HashMap<>(); |
| | | enumCodeToTextMap.forEach((key, value) -> enumTextToCodeMap.put(value, key)); |
| | | // 应该从text -> code中查找 |
| | | Object codeValue = enumTextToCodeMap.get(textValue); |
| | | return Convert.convert(contentProperty.getField().getType(), codeValue); |
| | | } |
| | | |
| | | @Override |
| | | public WriteCellData<String> convertToExcelData(Object object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { |
| | | if (ObjectUtil.isNull(object)) { |
| | | return new WriteCellData<>(""); |
| | | } |
| | | Map<Object, String> enumValueMap = beforeConvert(contentProperty); |
| | | String value = Convert.toStr(enumValueMap.get(object), ""); |
| | | return new WriteCellData<>(value); |
| | | } |
| | | |
| | | private Map<Object, String> beforeConvert(ExcelContentProperty contentProperty) { |
| | | ExcelEnumFormat anno = getAnnotation(contentProperty.getField()); |
| | | Map<Object, String> enumValueMap = new HashMap<>(); |
| | | Enum<?>[] enumConstants = anno.enumClass().getEnumConstants(); |
| | | for (Enum<?> enumConstant : enumConstants) { |
| | | Object codeValue = ReflectUtils.invokeGetter(enumConstant, anno.codeField()); |
| | | String textValue = ReflectUtils.invokeGetter(enumConstant, anno.textField()); |
| | | enumValueMap.put(codeValue, textValue); |
| | | } |
| | | return enumValueMap; |
| | | } |
| | | |
| | | private ExcelEnumFormat getAnnotation(Field field) { |
| | | return AnnotationUtil.getAnnotation(field, ExcelEnumFormat.class); |
| | | } |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.core; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ReflectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.alibaba.excel.annotation.ExcelProperty; |
| | | import com.alibaba.excel.metadata.Head; |
| | | import com.alibaba.excel.write.handler.WorkbookWriteHandler; |
| | | import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext; |
| | | import com.alibaba.excel.write.merge.AbstractMergeStrategy; |
| | | import com.ycl.common.utils.excel.annotation.CellMerge; |
| | | import com.ycl.common.utils.excel.utils.ReflectUtils; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Data; |
| | | import lombok.SneakyThrows; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.ss.usermodel.Cell; |
| | | import org.apache.poi.ss.usermodel.Sheet; |
| | | import org.apache.poi.ss.util.CellRangeAddress; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * 列值重复合并策略 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | public class CellMergeStrategy extends AbstractMergeStrategy implements WorkbookWriteHandler { |
| | | |
| | | private final List<CellRangeAddress> cellList; |
| | | private final boolean hasTitle; |
| | | private int rowIndex; |
| | | |
| | | public CellMergeStrategy(List<?> list, boolean hasTitle) { |
| | | this.hasTitle = hasTitle; |
| | | // 行合并开始下标 |
| | | this.rowIndex = hasTitle ? 1 : 0; |
| | | this.cellList = handle(list, hasTitle); |
| | | } |
| | | |
| | | @Override |
| | | protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) { |
| | | //单元格写入了,遍历合并区域,如果该Cell在区域内,但非首行,则清空 |
| | | final int rowIndex = cell.getRowIndex(); |
| | | if (CollUtil.isNotEmpty(cellList)){ |
| | | for (CellRangeAddress cellAddresses : cellList) { |
| | | final int firstRow = cellAddresses.getFirstRow(); |
| | | if (cellAddresses.isInRange(cell) && rowIndex != firstRow){ |
| | | cell.setBlank(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void afterWorkbookDispose(final WorkbookWriteHandlerContext context) { |
| | | //当前表格写完后,统一写入 |
| | | if (CollUtil.isNotEmpty(cellList)){ |
| | | for (CellRangeAddress item : cellList) { |
| | | context.getWriteContext().writeSheetHolder().getSheet().addMergedRegion(item); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @SneakyThrows |
| | | private List<CellRangeAddress> handle(List<?> list, boolean hasTitle) { |
| | | List<CellRangeAddress> cellList = new ArrayList<>(); |
| | | if (CollUtil.isEmpty(list)) { |
| | | return cellList; |
| | | } |
| | | Field[] fields = ReflectUtils.getFields(list.get(0).getClass(), field -> !"serialVersionUID".equals(field.getName())); |
| | | |
| | | // 有注解的字段 |
| | | List<Field> mergeFields = new ArrayList<>(); |
| | | List<Integer> mergeFieldsIndex = new ArrayList<>(); |
| | | for (int i = 0; i < fields.length; i++) { |
| | | Field field = fields[i]; |
| | | if (field.isAnnotationPresent(CellMerge.class)) { |
| | | CellMerge cm = field.getAnnotation(CellMerge.class); |
| | | mergeFields.add(field); |
| | | mergeFieldsIndex.add(cm.index() == -1 ? i : cm.index()); |
| | | if (hasTitle) { |
| | | ExcelProperty property = field.getAnnotation(ExcelProperty.class); |
| | | rowIndex = Math.max(rowIndex, property.value().length); |
| | | } |
| | | } |
| | | } |
| | | |
| | | Map<Field, RepeatCell> map = new HashMap<>(); |
| | | // 生成两两合并单元格 |
| | | for (int i = 0; i < list.size(); i++) { |
| | | for (int j = 0; j < mergeFields.size(); j++) { |
| | | Field field = mergeFields.get(j); |
| | | Object val = ReflectUtils.invokeGetter(list.get(i), field.getName()); |
| | | |
| | | int colNum = mergeFieldsIndex.get(j); |
| | | if (!map.containsKey(field)) { |
| | | map.put(field, new RepeatCell(val, i)); |
| | | } else { |
| | | RepeatCell repeatCell = map.get(field); |
| | | Object cellValue = repeatCell.getValue(); |
| | | if (cellValue == null || "".equals(cellValue)) { |
| | | // 空值跳过不合并 |
| | | continue; |
| | | } |
| | | |
| | | if (!cellValue.equals(val)) { |
| | | if ((i - repeatCell.getCurrent() > 1)) { |
| | | cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex - 1, colNum, colNum)); |
| | | } |
| | | map.put(field, new RepeatCell(val, i)); |
| | | } else if (i == list.size() - 1) { |
| | | if (i > repeatCell.getCurrent() && isMerge(list, i, field)) { |
| | | cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum)); |
| | | } |
| | | } else if (!isMerge(list, i, field)) { |
| | | if ((i - repeatCell.getCurrent() > 1)) { |
| | | cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex - 1, colNum, colNum)); |
| | | } |
| | | map.put(field, new RepeatCell(val, i)); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return cellList; |
| | | } |
| | | |
| | | private boolean isMerge(List<?> list, int i, Field field) { |
| | | boolean isMerge = true; |
| | | CellMerge cm = field.getAnnotation(CellMerge.class); |
| | | final String[] mergeBy = cm.mergeBy(); |
| | | if (StrUtil.isAllNotBlank(mergeBy)) { |
| | | //比对当前list(i)和list(i - 1)的各个属性值一一比对 如果全为真 则为真 |
| | | for (String fieldName : mergeBy) { |
| | | final Object valCurrent = ReflectUtil.getFieldValue(list.get(i), fieldName); |
| | | final Object valPre = ReflectUtil.getFieldValue(list.get(i - 1), fieldName); |
| | | if (!Objects.equals(valPre, valCurrent)) { |
| | | //依赖字段如有任一不等值,则标记为不可合并 |
| | | isMerge = false; |
| | | } |
| | | } |
| | | } |
| | | return isMerge; |
| | | } |
| | | |
| | | @Data |
| | | @AllArgsConstructor |
| | | static class RepeatCell { |
| | | |
| | | private Object value; |
| | | |
| | | private int current; |
| | | |
| | | } |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.core; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.alibaba.excel.context.AnalysisContext; |
| | | import com.alibaba.excel.event.AnalysisEventListener; |
| | | import com.alibaba.excel.exception.ExcelAnalysisException; |
| | | import com.alibaba.excel.exception.ExcelDataConvertException; |
| | | import com.ycl.common.utils.excel.utils.JsonUtils; |
| | | import com.ycl.common.utils.excel.utils.StreamUtils; |
| | | import com.ycl.common.utils.excel.utils.ValidatorUtils; |
| | | import lombok.NoArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import javax.validation.ConstraintViolation; |
| | | import javax.validation.ConstraintViolationException; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * Excel 导入监听 |
| | | * |
| | | * @author Yjoioooo |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @NoArgsConstructor |
| | | public class DefaultExcelListener<T> extends AnalysisEventListener<T> implements ExcelListener<T> { |
| | | |
| | | /** |
| | | * 是否Validator检验,默认为是 |
| | | */ |
| | | private Boolean isValidate = Boolean.TRUE; |
| | | |
| | | /** |
| | | * excel 表头数据 |
| | | */ |
| | | private Map<Integer, String> headMap; |
| | | |
| | | /** |
| | | * 导入回执 |
| | | */ |
| | | private ExcelResult<T> excelResult; |
| | | |
| | | public DefaultExcelListener(boolean isValidate) { |
| | | this.excelResult = new DefaultExcelResult<>(); |
| | | this.isValidate = isValidate; |
| | | } |
| | | |
| | | /** |
| | | * 处理异常 |
| | | * |
| | | * @param exception ExcelDataConvertException |
| | | * @param context Excel 上下文 |
| | | */ |
| | | @Override |
| | | public void onException(Exception exception, AnalysisContext context) throws Exception { |
| | | String errMsg = null; |
| | | if (exception instanceof ExcelDataConvertException excelDataConvertException) { |
| | | // 如果是某一个单元格的转换异常 能获取到具体行号 |
| | | Integer rowIndex = excelDataConvertException.getRowIndex(); |
| | | Integer columnIndex = excelDataConvertException.getColumnIndex(); |
| | | errMsg = StrUtil.format("第{}行-第{}列-表头{}: 解析异常<br/>", |
| | | rowIndex + 1, columnIndex + 1, headMap.get(columnIndex)); |
| | | if (log.isDebugEnabled()) { |
| | | log.error(errMsg); |
| | | } |
| | | } |
| | | if (exception instanceof ConstraintViolationException constraintViolationException) { |
| | | Set<ConstraintViolation<?>> constraintViolations = constraintViolationException.getConstraintViolations(); |
| | | String constraintViolationsMsg = StreamUtils.join(constraintViolations, ConstraintViolation::getMessage, ", "); |
| | | errMsg = StrUtil.format("第{}行数据校验异常: {}", context.readRowHolder().getRowIndex() + 1, constraintViolationsMsg); |
| | | if (log.isDebugEnabled()) { |
| | | log.error(errMsg); |
| | | } |
| | | } |
| | | excelResult.getErrorList().add(errMsg); |
| | | throw new ExcelAnalysisException(errMsg); |
| | | } |
| | | |
| | | @Override |
| | | public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { |
| | | this.headMap = headMap; |
| | | log.debug("解析到一条表头数据: {}", JsonUtils.toJsonString(headMap)); |
| | | } |
| | | |
| | | @Override |
| | | public void invoke(T data, AnalysisContext context) { |
| | | if (isValidate) { |
| | | ValidatorUtils.validate(data); |
| | | } |
| | | excelResult.getList().add(data); |
| | | } |
| | | |
| | | @Override |
| | | public void doAfterAllAnalysed(AnalysisContext context) { |
| | | log.debug("所有数据解析完成!"); |
| | | } |
| | | |
| | | @Override |
| | | public ExcelResult<T> getExcelResult() { |
| | | return excelResult; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.core; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import lombok.Setter; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 默认excel返回对象 |
| | | * |
| | | * @author Yjoioooo |
| | | * @author Lion Li |
| | | */ |
| | | public class DefaultExcelResult<T> implements ExcelResult<T> { |
| | | |
| | | /** |
| | | * 数据对象list |
| | | */ |
| | | @Setter |
| | | private List<T> list; |
| | | |
| | | /** |
| | | * 错误信息列表 |
| | | */ |
| | | @Setter |
| | | private List<String> errorList; |
| | | |
| | | public DefaultExcelResult() { |
| | | this.list = new ArrayList<>(); |
| | | this.errorList = new ArrayList<>(); |
| | | } |
| | | |
| | | public DefaultExcelResult(List<T> list, List<String> errorList) { |
| | | this.list = list; |
| | | this.errorList = errorList; |
| | | } |
| | | |
| | | public DefaultExcelResult(ExcelResult<T> excelResult) { |
| | | this.list = excelResult.getList(); |
| | | this.errorList = excelResult.getErrorList(); |
| | | } |
| | | |
| | | @Override |
| | | public List<T> getList() { |
| | | return list; |
| | | } |
| | | |
| | | @Override |
| | | public List<String> getErrorList() { |
| | | return errorList; |
| | | } |
| | | |
| | | /** |
| | | * 获取导入回执 |
| | | * |
| | | * @return 导入回执 |
| | | */ |
| | | @Override |
| | | public String getAnalysis() { |
| | | int successCount = list.size(); |
| | | int errorCount = errorList.size(); |
| | | if (successCount == 0) { |
| | | return "读取失败,未解析到数据"; |
| | | } else { |
| | | if (errorCount == 0) { |
| | | return StrUtil.format("恭喜您,全部读取成功!共{}条", successCount); |
| | | } else { |
| | | return ""; |
| | | } |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.core; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.ycl.common.exception.ServiceException; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Data; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <h1>Excel下拉可选项</h1> |
| | | * 注意:为确保下拉框解析正确,传值务必使用createOptionValue()做为值的拼接 |
| | | * |
| | | * @author Emil.Zhang |
| | | */ |
| | | @Data |
| | | @AllArgsConstructor |
| | | @NoArgsConstructor |
| | | @SuppressWarnings("unused") |
| | | public class DropDownOptions { |
| | | /** |
| | | * 一级下拉所在列index,从0开始算 |
| | | */ |
| | | private int index = 0; |
| | | /** |
| | | * 二级下拉所在的index,从0开始算,不能与一级相同 |
| | | */ |
| | | private int nextIndex = 0; |
| | | /** |
| | | * 一级下拉所包含的数据 |
| | | */ |
| | | private List<String> options = new ArrayList<>(); |
| | | /** |
| | | * 二级下拉所包含的数据Map |
| | | * <p>以每一个一级选项值为Key,每个一级选项对应的二级数据为Value</p> |
| | | */ |
| | | private Map<String, List<String>> nextOptions = new HashMap<>(); |
| | | /** |
| | | * 分隔符 |
| | | */ |
| | | private static final String DELIMITER = "_"; |
| | | |
| | | /** |
| | | * 创建只有一级的下拉选 |
| | | */ |
| | | public DropDownOptions(int index, List<String> options) { |
| | | this.index = index; |
| | | this.options = options; |
| | | } |
| | | |
| | | /** |
| | | * <h2>创建每个选项可选值</h2> |
| | | * <p>注意:不能以数字,特殊符号开头,选项中不可以包含任何运算符号</p> |
| | | * |
| | | * @param vars 可选值内包含的参数 |
| | | * @return 合规的可选值 |
| | | */ |
| | | public static String createOptionValue(Object... vars) { |
| | | StringBuilder stringBuffer = new StringBuilder(); |
| | | String regex = "^[\\S\\d\\u4e00-\\u9fa5]+$"; |
| | | for (int i = 0; i < vars.length; i++) { |
| | | String var = StrUtil.trimToEmpty(String.valueOf(vars[i])); |
| | | if (!var.matches(regex)) { |
| | | throw new ServiceException("选项数据不符合规则,仅允许使用中英文字符以及数字"); |
| | | } |
| | | stringBuffer.append(var); |
| | | if (i < vars.length - 1) { |
| | | // 直至最后一个前,都以_作为切割线 |
| | | stringBuffer.append(DELIMITER); |
| | | } |
| | | } |
| | | if (stringBuffer.toString().matches("^\\d_*$")) { |
| | | throw new ServiceException("禁止以数字开头"); |
| | | } |
| | | return stringBuffer.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 将处理后合理的可选值解析为原始的参数 |
| | | * |
| | | * @param option 经过处理后的合理的可选项 |
| | | * @return 原始的参数 |
| | | */ |
| | | public static List<String> analyzeOptionValue(String option) { |
| | | return StrUtil.split(option, DELIMITER, true, true); |
| | | } |
| | | |
| | | /** |
| | | * 创建级联下拉选项 |
| | | * |
| | | * @param parentList 父实体可选项原始数据 |
| | | * @param parentIndex 父下拉选位置 |
| | | * @param sonList 子实体可选项原始数据 |
| | | * @param sonIndex 子下拉选位置 |
| | | * @param parentHowToGetIdFunction 父类如何获取唯一标识 |
| | | * @param sonHowToGetParentIdFunction 子类如何获取父类的唯一标识 |
| | | * @param howToBuildEveryOption 如何生成下拉选内容 |
| | | * @return 级联下拉选项 |
| | | */ |
| | | public static <T> DropDownOptions buildLinkedOptions(List<T> parentList, |
| | | int parentIndex, |
| | | List<T> sonList, |
| | | int sonIndex, |
| | | Function<T, Number> parentHowToGetIdFunction, |
| | | Function<T, Number> sonHowToGetParentIdFunction, |
| | | Function<T, String> howToBuildEveryOption) { |
| | | DropDownOptions parentLinkSonOptions = new DropDownOptions(); |
| | | // 先创建父类的下拉 |
| | | parentLinkSonOptions.setIndex(parentIndex); |
| | | parentLinkSonOptions.setOptions( |
| | | parentList.stream() |
| | | .map(howToBuildEveryOption) |
| | | .collect(Collectors.toList()) |
| | | ); |
| | | // 提取父-子级联下拉 |
| | | Map<String, List<String>> sonOptions = new HashMap<>(); |
| | | // 父级依据自己的ID分组 |
| | | Map<Number, List<T>> parentGroupByIdMap = |
| | | parentList.stream().collect(Collectors.groupingBy(parentHowToGetIdFunction)); |
| | | // 遍历每个子集,提取到Map中 |
| | | sonList.forEach(everySon -> { |
| | | if (parentGroupByIdMap.containsKey(sonHowToGetParentIdFunction.apply(everySon))) { |
| | | // 找到对应的上级 |
| | | T parentObj = parentGroupByIdMap.get(sonHowToGetParentIdFunction.apply(everySon)).get(0); |
| | | // 提取名称和ID作为Key |
| | | String key = howToBuildEveryOption.apply(parentObj); |
| | | // Key对应的Value |
| | | List<String> thisParentSonOptionList; |
| | | if (sonOptions.containsKey(key)) { |
| | | thisParentSonOptionList = sonOptions.get(key); |
| | | } else { |
| | | thisParentSonOptionList = new ArrayList<>(); |
| | | sonOptions.put(key, thisParentSonOptionList); |
| | | } |
| | | // 往Value中添加当前子集选项 |
| | | thisParentSonOptionList.add(howToBuildEveryOption.apply(everySon)); |
| | | } |
| | | }); |
| | | parentLinkSonOptions.setNextIndex(sonIndex); |
| | | parentLinkSonOptions.setNextOptions(sonOptions); |
| | | return parentLinkSonOptions; |
| | | } |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.core; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.hutool.core.util.EnumUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | 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 com.ycl.common.exception.ServiceException; |
| | | import com.ycl.common.utils.excel.annotation.ExcelDictFormat; |
| | | import com.ycl.common.utils.excel.annotation.ExcelEnumFormat; |
| | | import com.ycl.common.utils.excel.service.DictService; |
| | | import com.ycl.common.utils.excel.utils.StreamUtils; |
| | | import com.ycl.common.utils.excel.utils.StringUtils; |
| | | import com.ycl.common.utils.spring.SpringUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.ss.usermodel.*; |
| | | import org.apache.poi.ss.util.CellRangeAddressList; |
| | | import org.apache.poi.ss.util.WorkbookUtil; |
| | | import org.apache.poi.xssf.usermodel.XSSFDataValidation; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * <h1>Excel表格下拉选操作</h1> |
| | | * 考虑到下拉选过多可能导致Excel打开缓慢的问题,只校验前1000行 |
| | | * <p> |
| | | * 即只有前1000行的数据可以用下拉框,超出的自行通过限制数据量的形式,第二次输出 |
| | | * |
| | | * @author Emil.Zhang |
| | | */ |
| | | @Slf4j |
| | | public class ExcelDownHandler implements SheetWriteHandler { |
| | | |
| | | /** |
| | | * Excel表格中的列名英文 |
| | | * 仅为了解析列英文,禁止修改 |
| | | */ |
| | | private static final String EXCEL_COLUMN_NAME = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| | | /** |
| | | * 单选数据Sheet名 |
| | | */ |
| | | private static final String OPTIONS_SHEET_NAME = "options"; |
| | | /** |
| | | * 联动选择数据Sheet名的头 |
| | | */ |
| | | private static final String LINKED_OPTIONS_SHEET_NAME = "linkedOptions"; |
| | | /** |
| | | * 下拉可选项 |
| | | */ |
| | | private final List<DropDownOptions> dropDownOptions; |
| | | /** |
| | | * 当前单选进度 |
| | | */ |
| | | private int currentOptionsColumnIndex; |
| | | /** |
| | | * 当前联动选择进度 |
| | | */ |
| | | private int currentLinkedOptionsSheetIndex; |
| | | private final DictService dictService; |
| | | |
| | | public ExcelDownHandler(List<DropDownOptions> options) { |
| | | this.dropDownOptions = options; |
| | | this.currentOptionsColumnIndex = 0; |
| | | this.currentLinkedOptionsSheetIndex = 0; |
| | | this.dictService = SpringUtils.getBean(DictService.class); |
| | | } |
| | | |
| | | /** |
| | | * <h2>开始创建下拉数据</h2> |
| | | * 1.通过解析传入的@ExcelProperty同级是否标注有@DropDown选项 |
| | | * 如果有且设置了value值,则将其直接置为下拉可选项 |
| | | * <p> |
| | | * 2.或者在调用ExcelUtil时指定了可选项,将依据传入的可选项做下拉 |
| | | * <p> |
| | | * 3.二者并存,注意调用方式 |
| | | */ |
| | | @Override |
| | | public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { |
| | | Sheet sheet = writeSheetHolder.getSheet(); |
| | | // 开始设置下拉框 HSSFWorkbook |
| | | DataValidationHelper helper = sheet.getDataValidationHelper(); |
| | | Workbook workbook = writeWorkbookHolder.getWorkbook(); |
| | | 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 (field.isAnnotationPresent(ExcelDictFormat.class)) { |
| | | // 如果指定了@ExcelDictFormat,则使用字典的逻辑 |
| | | ExcelDictFormat format = field.getDeclaredAnnotation(ExcelDictFormat.class); |
| | | String dictType = format.dictType(); |
| | | String converterExp = format.readConverterExp(); |
| | | 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 (StringUtils.isNotBlank(converterExp)) { |
| | | // 如果指定了确切的值,则直接解析确切的值 |
| | | List<String> strList = StringUtils.splitList(converterExp, format.separator()); |
| | | options = StreamUtils.toList(strList, s -> StringUtils.split(s, "=")[1]); |
| | | } |
| | | } else if (field.isAnnotationPresent(ExcelEnumFormat.class)) { |
| | | // 否则如果指定了@ExcelEnumFormat,则使用枚举的逻辑 |
| | | 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)) { |
| | | // 仅当下拉可选项不为空时执行 |
| | | if (options.size() > 20) { |
| | | // 这里限制如果可选项大于20,则使用额外表形式 |
| | | dropDownWithSheet(helper, workbook, sheet, index, options); |
| | | } else { |
| | | // 否则使用固定值形式 |
| | | dropDownWithSimple(helper, sheet, index, options); |
| | | } |
| | | } |
| | | } |
| | | if (CollUtil.isEmpty(dropDownOptions)) { |
| | | return; |
| | | } |
| | | dropDownOptions.forEach(everyOptions -> { |
| | | // 如果传递了下拉框选择器参数 |
| | | if (!everyOptions.getNextOptions().isEmpty()) { |
| | | // 当二级选项不为空时,使用额外关联表的形式 |
| | | dropDownLinkedOptions(helper, workbook, sheet, everyOptions); |
| | | } else if (everyOptions.getOptions().size() > 10) { |
| | | // 当一级选项参数个数大于10,使用额外表的形式 |
| | | dropDownWithSheet(helper, workbook, sheet, everyOptions.getIndex(), everyOptions.getOptions()); |
| | | } else if (everyOptions.getOptions().size() != 0) { |
| | | // 当一级选项个数不为空,使用默认形式 |
| | | dropDownWithSimple(helper, sheet, everyOptions.getIndex(), everyOptions.getOptions()); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * <h2>简单下拉框</h2> |
| | | * 直接将可选项拼接为指定列的数据校验值 |
| | | * |
| | | * @param celIndex 列index |
| | | * @param value 下拉选可选值 |
| | | */ |
| | | private void dropDownWithSimple(DataValidationHelper helper, Sheet sheet, Integer celIndex, List<String> value) { |
| | | if (ObjectUtil.isEmpty(value)) { |
| | | return; |
| | | } |
| | | this.markOptionsToSheet(helper, sheet, celIndex, helper.createExplicitListConstraint(ArrayUtil.toArray(value, String.class))); |
| | | } |
| | | |
| | | /** |
| | | * <h2>额外表格形式的级联下拉框</h2> |
| | | * |
| | | * @param options 额外表格形式存储的下拉可选项 |
| | | */ |
| | | private void dropDownLinkedOptions(DataValidationHelper helper, Workbook workbook, Sheet sheet, DropDownOptions options) { |
| | | String linkedOptionsSheetName = String.format("%s_%d", LINKED_OPTIONS_SHEET_NAME, currentLinkedOptionsSheetIndex); |
| | | // 创建联动下拉数据表 |
| | | Sheet linkedOptionsDataSheet = workbook.createSheet(WorkbookUtil.createSafeSheetName(linkedOptionsSheetName)); |
| | | // 将下拉表隐藏 |
| | | workbook.setSheetHidden(workbook.getSheetIndex(linkedOptionsDataSheet), true); |
| | | // 完善横向的一级选项数据表 |
| | | List<String> firstOptions = options.getOptions(); |
| | | Map<String, List<String>> secoundOptionsMap = options.getNextOptions(); |
| | | |
| | | // 创建名称管理器 |
| | | Name name = workbook.createName(); |
| | | // 设置名称管理器的别名 |
| | | name.setNameName(linkedOptionsSheetName); |
| | | // 以横向第一行创建一级下拉拼接引用位置 |
| | | String firstOptionsFunction = String.format("%s!$%s$1:$%s$1", |
| | | linkedOptionsSheetName, |
| | | getExcelColumnName(0), |
| | | getExcelColumnName(firstOptions.size()) |
| | | ); |
| | | // 设置名称管理器的引用位置 |
| | | name.setRefersToFormula(firstOptionsFunction); |
| | | // 设置数据校验为序列模式,引用的是名称管理器中的别名 |
| | | this.markOptionsToSheet(helper, sheet, options.getIndex(), helper.createFormulaListConstraint(linkedOptionsSheetName)); |
| | | |
| | | for (int columIndex = 0; columIndex < firstOptions.size(); columIndex++) { |
| | | // 先提取主表中一级下拉的列名 |
| | | String firstOptionsColumnName = getExcelColumnName(columIndex); |
| | | // 一次循环是每一个一级选项 |
| | | int finalI = columIndex; |
| | | // 本次循环的一级选项值 |
| | | String thisFirstOptionsValue = firstOptions.get(columIndex); |
| | | // 创建第一行的数据 |
| | | Optional.ofNullable(linkedOptionsDataSheet.getRow(0)) |
| | | // 如果不存在则创建第一行 |
| | | .orElseGet(() -> linkedOptionsDataSheet.createRow(finalI)) |
| | | // 第一行当前列 |
| | | .createCell(columIndex) |
| | | // 设置值为当前一级选项值 |
| | | .setCellValue(thisFirstOptionsValue); |
| | | |
| | | // 第二行开始,设置第二级别选项参数 |
| | | List<String> secondOptions = secoundOptionsMap.get(thisFirstOptionsValue); |
| | | if (CollUtil.isEmpty(secondOptions)) { |
| | | // 必须保证至少有一个关联选项,否则将导致Excel解析错误 |
| | | secondOptions = Collections.singletonList("暂无_0"); |
| | | } |
| | | |
| | | // 以该一级选项值创建子名称管理器 |
| | | Name sonName = workbook.createName(); |
| | | // 设置名称管理器的别名 |
| | | sonName.setNameName(thisFirstOptionsValue); |
| | | // 以第二行该列数据拼接引用位置 |
| | | String sonFunction = String.format("%s!$%s$2:$%s$%d", |
| | | linkedOptionsSheetName, |
| | | firstOptionsColumnName, |
| | | firstOptionsColumnName, |
| | | secondOptions.size() + 1 |
| | | ); |
| | | // 设置名称管理器的引用位置 |
| | | sonName.setRefersToFormula(sonFunction); |
| | | // 数据验证为序列模式,引用到每一个主表中的二级选项位置 |
| | | // 创建子项的名称管理器,只是为了使得Excel可以识别到数据 |
| | | String mainSheetFirstOptionsColumnName = getExcelColumnName(options.getIndex()); |
| | | for (int i = 0; i < 100; i++) { |
| | | // 以一级选项对应的主体所在位置创建二级下拉 |
| | | String secondOptionsFunction = String.format("=INDIRECT(%s%d)", mainSheetFirstOptionsColumnName, i + 1); |
| | | // 二级只能主表每一行的每一列添加二级校验 |
| | | markLinkedOptionsToSheet(helper, sheet, i, options.getNextIndex(), helper.createFormulaListConstraint(secondOptionsFunction)); |
| | | } |
| | | |
| | | for (int rowIndex = 0; rowIndex < secondOptions.size(); rowIndex++) { |
| | | // 从第二行开始填充二级选项 |
| | | int finalRowIndex = rowIndex + 1; |
| | | int finalColumIndex = columIndex; |
| | | |
| | | Row row = Optional.ofNullable(linkedOptionsDataSheet.getRow(finalRowIndex)) |
| | | // 没有则创建 |
| | | .orElseGet(() -> linkedOptionsDataSheet.createRow(finalRowIndex)); |
| | | Optional |
| | | // 在本级一级选项所在的列 |
| | | .ofNullable(row.getCell(finalColumIndex)) |
| | | // 不存在则创建 |
| | | .orElseGet(() -> row.createCell(finalColumIndex)) |
| | | // 设置二级选项值 |
| | | .setCellValue(secondOptions.get(rowIndex)); |
| | | } |
| | | } |
| | | |
| | | currentLinkedOptionsSheetIndex++; |
| | | } |
| | | |
| | | /** |
| | | * <h2>额外表格形式的普通下拉框</h2> |
| | | * 由于下拉框可选值数量过多,为提升Excel打开效率,使用额外表格形式做下拉 |
| | | * |
| | | * @param celIndex 下拉选 |
| | | * @param value 下拉选可选值 |
| | | */ |
| | | private void dropDownWithSheet(DataValidationHelper helper, Workbook workbook, Sheet sheet, Integer celIndex, List<String> value) { |
| | | // 创建下拉数据表 |
| | | Sheet simpleDataSheet = Optional.ofNullable(workbook.getSheet(WorkbookUtil.createSafeSheetName(OPTIONS_SHEET_NAME))) |
| | | .orElseGet(() -> workbook.createSheet(WorkbookUtil.createSafeSheetName(OPTIONS_SHEET_NAME))); |
| | | // 将下拉表隐藏 |
| | | workbook.setSheetHidden(workbook.getSheetIndex(simpleDataSheet), true); |
| | | // 完善纵向的一级选项数据表 |
| | | for (int i = 0; i < value.size(); i++) { |
| | | int finalI = i; |
| | | // 获取每一选项行,如果没有则创建 |
| | | Row row = Optional.ofNullable(simpleDataSheet.getRow(i)) |
| | | .orElseGet(() -> simpleDataSheet.createRow(finalI)); |
| | | // 获取本级选项对应的选项列,如果没有则创建 |
| | | Cell cell = Optional.ofNullable(row.getCell(currentOptionsColumnIndex)) |
| | | .orElseGet(() -> row.createCell(currentOptionsColumnIndex)); |
| | | // 设置值 |
| | | cell.setCellValue(value.get(i)); |
| | | } |
| | | |
| | | // 创建名称管理器 |
| | | Name name = workbook.createName(); |
| | | // 设置名称管理器的别名 |
| | | String nameName = String.format("%s_%d", OPTIONS_SHEET_NAME, celIndex); |
| | | name.setNameName(nameName); |
| | | // 以纵向第一列创建一级下拉拼接引用位置 |
| | | String function = String.format("%s!$%s$1:$%s$%d", |
| | | OPTIONS_SHEET_NAME, |
| | | getExcelColumnName(currentOptionsColumnIndex), |
| | | getExcelColumnName(currentOptionsColumnIndex), |
| | | value.size()); |
| | | // 设置名称管理器的引用位置 |
| | | name.setRefersToFormula(function); |
| | | // 设置数据校验为序列模式,引用的是名称管理器中的别名 |
| | | this.markOptionsToSheet(helper, sheet, celIndex, helper.createFormulaListConstraint(nameName)); |
| | | currentOptionsColumnIndex++; |
| | | } |
| | | |
| | | /** |
| | | * 挂载下拉的列,仅限一级选项 |
| | | */ |
| | | private void markOptionsToSheet(DataValidationHelper helper, Sheet sheet, Integer celIndex, |
| | | DataValidationConstraint constraint) { |
| | | // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列 |
| | | CellRangeAddressList addressList = new CellRangeAddressList(1, 1000, celIndex, celIndex); |
| | | markDataValidationToSheet(helper, sheet, constraint, addressList); |
| | | } |
| | | |
| | | /** |
| | | * 挂载下拉的列,仅限二级选项 |
| | | */ |
| | | private void markLinkedOptionsToSheet(DataValidationHelper helper, Sheet sheet, Integer rowIndex, |
| | | Integer celIndex, DataValidationConstraint constraint) { |
| | | // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列 |
| | | CellRangeAddressList addressList = new CellRangeAddressList(rowIndex, rowIndex, celIndex, celIndex); |
| | | markDataValidationToSheet(helper, sheet, constraint, addressList); |
| | | } |
| | | |
| | | /** |
| | | * 应用数据校验 |
| | | */ |
| | | private void markDataValidationToSheet(DataValidationHelper helper, Sheet sheet, |
| | | DataValidationConstraint constraint, CellRangeAddressList addressList) { |
| | | // 数据有效性对象 |
| | | DataValidation dataValidation = helper.createValidation(constraint, addressList); |
| | | // 处理Excel兼容性问题 |
| | | if (dataValidation instanceof XSSFDataValidation) { |
| | | //数据校验 |
| | | dataValidation.setSuppressDropDownArrow(true); |
| | | //错误提示 |
| | | dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP); |
| | | dataValidation.createErrorBox("提示", "此值与单元格定义数据不一致"); |
| | | dataValidation.setShowErrorBox(true); |
| | | //选定提示 |
| | | dataValidation.createPromptBox("填写说明:", "填写内容只能为下拉中数据,其他数据将导致导入失败"); |
| | | dataValidation.setShowPromptBox(true); |
| | | sheet.addValidationData(dataValidation); |
| | | } else { |
| | | dataValidation.setSuppressDropDownArrow(false); |
| | | } |
| | | sheet.addValidationData(dataValidation); |
| | | } |
| | | |
| | | /** |
| | | * <h2>依据列index获取列名英文</h2> |
| | | * 依据列index转换为Excel中的列名英文 |
| | | * <p>例如第1列,index为0,解析出来为A列</p> |
| | | * 第27列,index为26,解析为AA列 |
| | | * <p>第28列,index为27,解析为AB列</p> |
| | | * |
| | | * @param columnIndex 列index |
| | | * @return 列index所在得英文名 |
| | | */ |
| | | private String getExcelColumnName(int columnIndex) { |
| | | // 26一循环的次数 |
| | | int columnCircleCount = columnIndex / 26; |
| | | // 26一循环内的位置 |
| | | int thisCircleColumnIndex = columnIndex % 26; |
| | | // 26一循环的次数大于0,则视为栏名至少两位 |
| | | String columnPrefix = columnCircleCount == 0 |
| | | ? StrUtil.EMPTY |
| | | : StrUtil.subWithLength(EXCEL_COLUMN_NAME, columnCircleCount - 1, 1); |
| | | // 从26一循环内取对应的栏位名 |
| | | String columnNext = StrUtil.subWithLength(EXCEL_COLUMN_NAME, thisCircleColumnIndex, 1); |
| | | // 将二者拼接即为最终的栏位名 |
| | | return columnPrefix + columnNext; |
| | | } |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.core; |
| | | |
| | | import com.alibaba.excel.read.listener.ReadListener; |
| | | |
| | | /** |
| | | * Excel 导入监听 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface ExcelListener<T> extends ReadListener<T> { |
| | | |
| | | ExcelResult<T> getExcelResult(); |
| | | |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.core; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * excel返回对象 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface ExcelResult<T> { |
| | | |
| | | /** |
| | | * 对象列表 |
| | | */ |
| | | List<T> getList(); |
| | | |
| | | /** |
| | | * 错误列表 |
| | | */ |
| | | List<String> getErrorList(); |
| | | |
| | | /** |
| | | * 导入回执 |
| | | */ |
| | | String getAnalysis(); |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.service; |
| | | |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 通用 字典服务 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface DictService { |
| | | |
| | | /** |
| | | * 分隔符 |
| | | */ |
| | | String SEPARATOR = ","; |
| | | |
| | | /** |
| | | * 根据字典类型和字典值获取字典标签 |
| | | * |
| | | * @param dictType 字典类型 |
| | | * @param dictValue 字典值 |
| | | * @return 字典标签 |
| | | */ |
| | | default String getDictLabel(String dictType, String dictValue) { |
| | | return getDictLabel(dictType, dictValue, SEPARATOR); |
| | | } |
| | | |
| | | /** |
| | | * 根据字典类型和字典标签获取字典值 |
| | | * |
| | | * @param dictType 字典类型 |
| | | * @param dictLabel 字典标签 |
| | | * @return 字典值 |
| | | */ |
| | | default String getDictValue(String dictType, String dictLabel) { |
| | | return getDictValue(dictType, dictLabel, SEPARATOR); |
| | | } |
| | | |
| | | /** |
| | | * 根据字典类型和字典值获取字典标签 |
| | | * |
| | | * @param dictType 字典类型 |
| | | * @param dictValue 字典值 |
| | | * @param separator 分隔符 |
| | | * @return 字典标签 |
| | | */ |
| | | String getDictLabel(String dictType, String dictValue, String separator); |
| | | |
| | | /** |
| | | * 根据字典类型和字典标签获取字典值 |
| | | * |
| | | * @param dictType 字典类型 |
| | | * @param dictLabel 字典标签 |
| | | * @param separator 分隔符 |
| | | * @return 字典值 |
| | | */ |
| | | String getDictValue(String dictType, String dictLabel, String separator); |
| | | |
| | | /** |
| | | * 获取字典下所有的字典值与标签 |
| | | * |
| | | * @param dictType 字典类型 |
| | | * @return dictValue为key,dictLabel为值组成的Map |
| | | */ |
| | | Map<String, String> getAllDictByDictType(String dictType); |
| | | |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.utils; |
| | | |
| | | import cn.hutool.core.lang.Dict; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.core.type.TypeReference; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.fasterxml.jackson.databind.exc.MismatchedInputException; |
| | | import com.ycl.common.utils.spring.SpringUtils; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * JSON 工具类 |
| | | * |
| | | * @author 芋道源码 |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class JsonUtils { |
| | | |
| | | private static final ObjectMapper OBJECT_MAPPER = SpringUtils.getBean(ObjectMapper.class); |
| | | |
| | | public static ObjectMapper getObjectMapper() { |
| | | return OBJECT_MAPPER; |
| | | } |
| | | |
| | | /** |
| | | * 将对象转换为JSON格式的字符串 |
| | | * |
| | | * @param object 要转换的对象 |
| | | * @return JSON格式的字符串,如果对象为null,则返回null |
| | | * @throws RuntimeException 如果转换过程中发生JSON处理异常,则抛出运行时异常 |
| | | */ |
| | | public static String toJsonString(Object object) { |
| | | if (ObjectUtil.isNull(object)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.writeValueAsString(object); |
| | | } catch (JsonProcessingException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将JSON格式的字符串转换为指定类型的对象 |
| | | * |
| | | * @param text JSON格式的字符串 |
| | | * @param clazz 要转换的目标对象类型 |
| | | * @param <T> 目标对象的泛型类型 |
| | | * @return 转换后的对象,如果字符串为空则返回null |
| | | * @throws RuntimeException 如果转换过程中发生IO异常,则抛出运行时异常 |
| | | */ |
| | | public static <T> T parseObject(String text, Class<T> clazz) { |
| | | if (StringUtils.isEmpty(text)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(text, clazz); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将字节数组转换为指定类型的对象 |
| | | * |
| | | * @param bytes 字节数组 |
| | | * @param clazz 要转换的目标对象类型 |
| | | * @param <T> 目标对象的泛型类型 |
| | | * @return 转换后的对象,如果字节数组为空则返回null |
| | | * @throws RuntimeException 如果转换过程中发生IO异常,则抛出运行时异常 |
| | | */ |
| | | public static <T> T parseObject(byte[] bytes, Class<T> clazz) { |
| | | if (ArrayUtil.isEmpty(bytes)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(bytes, clazz); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将JSON格式的字符串转换为指定类型的对象,支持复杂类型 |
| | | * |
| | | * @param text JSON格式的字符串 |
| | | * @param typeReference 指定类型的TypeReference对象 |
| | | * @param <T> 目标对象的泛型类型 |
| | | * @return 转换后的对象,如果字符串为空则返回null |
| | | * @throws RuntimeException 如果转换过程中发生IO异常,则抛出运行时异常 |
| | | */ |
| | | public static <T> T parseObject(String text, TypeReference<T> typeReference) { |
| | | if (StringUtils.isBlank(text)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(text, typeReference); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将JSON格式的字符串转换为Dict对象 |
| | | * |
| | | * @param text JSON格式的字符串 |
| | | * @return 转换后的Dict对象,如果字符串为空或者不是JSON格式则返回null |
| | | * @throws RuntimeException 如果转换过程中发生IO异常,则抛出运行时异常 |
| | | */ |
| | | public static Dict parseMap(String text) { |
| | | if (StringUtils.isBlank(text)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructType(Dict.class)); |
| | | } catch (MismatchedInputException e) { |
| | | // 类型不匹配说明不是json |
| | | return null; |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将JSON格式的字符串转换为Dict对象的列表 |
| | | * |
| | | * @param text JSON格式的字符串 |
| | | * @return 转换后的Dict对象的列表,如果字符串为空则返回null |
| | | * @throws RuntimeException 如果转换过程中发生IO异常,则抛出运行时异常 |
| | | */ |
| | | public static List<Dict> parseArrayMap(String text) { |
| | | if (StringUtils.isBlank(text)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, Dict.class)); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将JSON格式的字符串转换为指定类型对象的列表 |
| | | * |
| | | * @param text JSON格式的字符串 |
| | | * @param clazz 要转换的目标对象类型 |
| | | * @param <T> 目标对象的泛型类型 |
| | | * @return 转换后的对象的列表,如果字符串为空则返回空列表 |
| | | * @throws RuntimeException 如果转换过程中发生IO异常,则抛出运行时异常 |
| | | */ |
| | | public static <T> List<T> parseArray(String text, Class<T> clazz) { |
| | | if (StringUtils.isEmpty(text)) { |
| | | return new ArrayList<>(); |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz)); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.utils; |
| | | |
| | | import cn.hutool.core.util.ReflectUtil; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.lang.reflect.Method; |
| | | |
| | | /** |
| | | * 反射工具类. 提供调用getter/setter方法, 访问私有变量, 调用私有方法, 获取泛型类型Class, 被AOP过的真实类等工具函数. |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @SuppressWarnings("rawtypes") |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class ReflectUtils extends ReflectUtil { |
| | | |
| | | private static final String SETTER_PREFIX = "set"; |
| | | |
| | | private static final String GETTER_PREFIX = "get"; |
| | | |
| | | /** |
| | | * 调用Getter方法. |
| | | * 支持多级,如:对象名.对象名.方法 |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <E> E invokeGetter(Object obj, String propertyName) { |
| | | Object object = obj; |
| | | for (String name : StringUtils.split(propertyName, ".")) { |
| | | String getterMethodName = GETTER_PREFIX + StringUtils.capitalize(name); |
| | | object = invoke(object, getterMethodName); |
| | | } |
| | | return (E) object; |
| | | } |
| | | |
| | | /** |
| | | * 调用Setter方法, 仅匹配方法名。 |
| | | * 支持多级,如:对象名.对象名.方法 |
| | | */ |
| | | public static <E> void invokeSetter(Object obj, String propertyName, E value) { |
| | | Object object = obj; |
| | | String[] names = StringUtils.split(propertyName, "."); |
| | | for (int i = 0; i < names.length; i++) { |
| | | if (i < names.length - 1) { |
| | | String getterMethodName = GETTER_PREFIX + StringUtils.capitalize(names[i]); |
| | | object = invoke(object, getterMethodName); |
| | | } else { |
| | | String setterMethodName = SETTER_PREFIX + StringUtils.capitalize(names[i]); |
| | | Method method = getMethodByName(object.getClass(), setterMethodName); |
| | | invoke(object, method, value); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.utils; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.map.MapUtil; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.util.*; |
| | | import java.util.function.BiFunction; |
| | | import java.util.function.Function; |
| | | import java.util.function.Predicate; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * stream 流工具类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class StreamUtils { |
| | | |
| | | /** |
| | | * 将collection过滤 |
| | | * |
| | | * @param collection 需要转化的集合 |
| | | * @param function 过滤方法 |
| | | * @return 过滤后的list |
| | | */ |
| | | public static <E> List<E> filter(Collection<E> collection, Predicate<E> function) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return CollUtil.newArrayList(); |
| | | } |
| | | // 注意此处不要使用 .toList() 新语法 因为返回的是不可变List 会导致序列化问题 |
| | | return collection.stream().filter(function).collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 找到流中满足条件的第一个元素 |
| | | * |
| | | * @param collection 需要查询的集合 |
| | | * @param function 过滤方法 |
| | | * @return 找到符合条件的第一个元素,没有则返回null |
| | | */ |
| | | public static <E> E findFirst(Collection<E> collection, Predicate<E> function) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return null; |
| | | } |
| | | return collection.stream().filter(function).findFirst().orElse(null); |
| | | } |
| | | |
| | | /** |
| | | * 找到流中任意一个满足条件的元素 |
| | | * |
| | | * @param collection 需要查询的集合 |
| | | * @param function 过滤方法 |
| | | * @return 找到符合条件的任意一个元素,没有则返回null |
| | | */ |
| | | public static <E> Optional<E> findAny(Collection<E> collection, Predicate<E> function) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return Optional.empty(); |
| | | } |
| | | return collection.stream().filter(function).findAny(); |
| | | } |
| | | |
| | | /** |
| | | * 将collection拼接 |
| | | * |
| | | * @param collection 需要转化的集合 |
| | | * @param function 拼接方法 |
| | | * @return 拼接后的list |
| | | */ |
| | | public static <E> String join(Collection<E> collection, Function<E, String> function) { |
| | | return join(collection, function, StringUtils.SEPARATOR); |
| | | } |
| | | |
| | | /** |
| | | * 将collection拼接 |
| | | * |
| | | * @param collection 需要转化的集合 |
| | | * @param function 拼接方法 |
| | | * @param delimiter 拼接符 |
| | | * @return 拼接后的list |
| | | */ |
| | | public static <E> String join(Collection<E> collection, Function<E, String> function, CharSequence delimiter) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return StringUtils.EMPTY; |
| | | } |
| | | return collection.stream().map(function).filter(Objects::nonNull).collect(Collectors.joining(delimiter)); |
| | | } |
| | | |
| | | /** |
| | | * 将collection排序 |
| | | * |
| | | * @param collection 需要转化的集合 |
| | | * @param comparing 排序方法 |
| | | * @return 排序后的list |
| | | */ |
| | | public static <E> List<E> sorted(Collection<E> collection, Comparator<E> comparing) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return CollUtil.newArrayList(); |
| | | } |
| | | // 注意此处不要使用 .toList() 新语法 因为返回的是不可变List 会导致序列化问题 |
| | | return collection.stream().filter(Objects::nonNull).sorted(comparing).collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 将collection转化为类型不变的map<br> |
| | | * <B>{@code Collection<V> ----> Map<K,V>}</B> |
| | | * |
| | | * @param collection 需要转化的集合 |
| | | * @param key V类型转化为K类型的lambda方法 |
| | | * @param <V> collection中的泛型 |
| | | * @param <K> map中的key类型 |
| | | * @return 转化后的map |
| | | */ |
| | | public static <V, K> Map<K, V> toIdentityMap(Collection<V> collection, Function<V, K> key) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return MapUtil.newHashMap(); |
| | | } |
| | | return collection.stream().filter(Objects::nonNull).collect(Collectors.toMap(key, Function.identity(), (l, r) -> l)); |
| | | } |
| | | |
| | | /** |
| | | * 将Collection转化为map(value类型与collection的泛型不同)<br> |
| | | * <B>{@code Collection<E> -----> Map<K,V> }</B> |
| | | * |
| | | * @param collection 需要转化的集合 |
| | | * @param key E类型转化为K类型的lambda方法 |
| | | * @param value E类型转化为V类型的lambda方法 |
| | | * @param <E> collection中的泛型 |
| | | * @param <K> map中的key类型 |
| | | * @param <V> map中的value类型 |
| | | * @return 转化后的map |
| | | */ |
| | | public static <E, K, V> Map<K, V> toMap(Collection<E> collection, Function<E, K> key, Function<E, V> value) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return MapUtil.newHashMap(); |
| | | } |
| | | return collection.stream().filter(Objects::nonNull).collect(Collectors.toMap(key, value, (l, r) -> l)); |
| | | } |
| | | |
| | | /** |
| | | * 将collection按照规则(比如有相同的班级id)分类成map<br> |
| | | * <B>{@code Collection<E> -------> Map<K,List<E>> } </B> |
| | | * |
| | | * @param collection 需要分类的集合 |
| | | * @param key 分类的规则 |
| | | * @param <E> collection中的泛型 |
| | | * @param <K> map中的key类型 |
| | | * @return 分类后的map |
| | | */ |
| | | public static <E, K> Map<K, List<E>> groupByKey(Collection<E> collection, Function<E, K> key) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return MapUtil.newHashMap(); |
| | | } |
| | | return collection |
| | | .stream().filter(Objects::nonNull) |
| | | .collect(Collectors.groupingBy(key, LinkedHashMap::new, Collectors.toList())); |
| | | } |
| | | |
| | | /** |
| | | * 将collection按照两个规则(比如有相同的年级id,班级id)分类成双层map<br> |
| | | * <B>{@code Collection<E> ---> Map<T,Map<U,List<E>>> } </B> |
| | | * |
| | | * @param collection 需要分类的集合 |
| | | * @param key1 第一个分类的规则 |
| | | * @param key2 第二个分类的规则 |
| | | * @param <E> 集合元素类型 |
| | | * @param <K> 第一个map中的key类型 |
| | | * @param <U> 第二个map中的key类型 |
| | | * @return 分类后的map |
| | | */ |
| | | public static <E, K, U> Map<K, Map<U, List<E>>> groupBy2Key(Collection<E> collection, Function<E, K> key1, Function<E, U> key2) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return MapUtil.newHashMap(); |
| | | } |
| | | return collection |
| | | .stream().filter(Objects::nonNull) |
| | | .collect(Collectors.groupingBy(key1, LinkedHashMap::new, Collectors.groupingBy(key2, LinkedHashMap::new, Collectors.toList()))); |
| | | } |
| | | |
| | | /** |
| | | * 将collection按照两个规则(比如有相同的年级id,班级id)分类成双层map<br> |
| | | * <B>{@code Collection<E> ---> Map<T,Map<U,E>> } </B> |
| | | * |
| | | * @param collection 需要分类的集合 |
| | | * @param key1 第一个分类的规则 |
| | | * @param key2 第二个分类的规则 |
| | | * @param <T> 第一个map中的key类型 |
| | | * @param <U> 第二个map中的key类型 |
| | | * @param <E> collection中的泛型 |
| | | * @return 分类后的map |
| | | */ |
| | | public static <E, T, U> Map<T, Map<U, E>> group2Map(Collection<E> collection, Function<E, T> key1, Function<E, U> key2) { |
| | | if (CollUtil.isEmpty(collection) || key1 == null || key2 == null) { |
| | | return MapUtil.newHashMap(); |
| | | } |
| | | return collection |
| | | .stream().filter(Objects::nonNull) |
| | | .collect(Collectors.groupingBy(key1, LinkedHashMap::new, Collectors.toMap(key2, Function.identity(), (l, r) -> l))); |
| | | } |
| | | |
| | | /** |
| | | * 将collection转化为List集合,但是两者的泛型不同<br> |
| | | * <B>{@code Collection<E> ------> List<T> } </B> |
| | | * |
| | | * @param collection 需要转化的集合 |
| | | * @param function collection中的泛型转化为list泛型的lambda表达式 |
| | | * @param <E> collection中的泛型 |
| | | * @param <T> List中的泛型 |
| | | * @return 转化后的list |
| | | */ |
| | | public static <E, T> List<T> toList(Collection<E> collection, Function<E, T> function) { |
| | | if (CollUtil.isEmpty(collection)) { |
| | | return CollUtil.newArrayList(); |
| | | } |
| | | return collection |
| | | .stream() |
| | | .map(function) |
| | | .filter(Objects::nonNull) |
| | | // 注意此处不要使用 .toList() 新语法 因为返回的是不可变List 会导致序列化问题 |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 将collection转化为Set集合,但是两者的泛型不同<br> |
| | | * <B>{@code Collection<E> ------> Set<T> } </B> |
| | | * |
| | | * @param collection 需要转化的集合 |
| | | * @param function collection中的泛型转化为set泛型的lambda表达式 |
| | | * @param <E> collection中的泛型 |
| | | * @param <T> Set中的泛型 |
| | | * @return 转化后的Set |
| | | */ |
| | | public static <E, T> Set<T> toSet(Collection<E> collection, Function<E, T> function) { |
| | | if (CollUtil.isEmpty(collection) || function == null) { |
| | | return CollUtil.newHashSet(); |
| | | } |
| | | return collection |
| | | .stream() |
| | | .map(function) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toSet()); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 合并两个相同key类型的map |
| | | * |
| | | * @param map1 第一个需要合并的 map |
| | | * @param map2 第二个需要合并的 map |
| | | * @param merge 合并的lambda,将key value1 value2合并成最终的类型,注意value可能为空的情况 |
| | | * @param <K> map中的key类型 |
| | | * @param <X> 第一个 map的value类型 |
| | | * @param <Y> 第二个 map的value类型 |
| | | * @param <V> 最终map的value类型 |
| | | * @return 合并后的map |
| | | */ |
| | | public static <K, X, Y, V> Map<K, V> merge(Map<K, X> map1, Map<K, Y> map2, BiFunction<X, Y, V> merge) { |
| | | if (MapUtil.isEmpty(map1) && MapUtil.isEmpty(map2)) { |
| | | return MapUtil.newHashMap(); |
| | | } else if (MapUtil.isEmpty(map1)) { |
| | | map1 = MapUtil.newHashMap(); |
| | | } else if (MapUtil.isEmpty(map2)) { |
| | | map2 = MapUtil.newHashMap(); |
| | | } |
| | | Set<K> key = new HashSet<>(); |
| | | key.addAll(map1.keySet()); |
| | | key.addAll(map2.keySet()); |
| | | Map<K, V> map = new HashMap<>(); |
| | | for (K t : key) { |
| | | X x = map1.get(t); |
| | | Y y = map2.get(t); |
| | | V z = merge.apply(x, y); |
| | | if (z != null) { |
| | | map.put(t, z); |
| | | } |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.utils; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.lang.Validator; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.springframework.util.AntPathMatcher; |
| | | |
| | | import java.util.*; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 字符串工具类 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class StringUtils extends org.apache.commons.lang3.StringUtils { |
| | | |
| | | public static final String SEPARATOR = ","; |
| | | |
| | | public static final String SLASH = "/"; |
| | | |
| | | /** |
| | | * 获取参数不为空值 |
| | | * |
| | | * @param str defaultValue 要判断的value |
| | | * @return value 返回值 |
| | | */ |
| | | public static String blankToDefault(String str, String defaultValue) { |
| | | return StrUtil.blankToDefault(str, defaultValue); |
| | | } |
| | | |
| | | /** |
| | | * * 判断一个字符串是否为空串 |
| | | * |
| | | * @param str String |
| | | * @return true:为空 false:非空 |
| | | */ |
| | | public static boolean isEmpty(String str) { |
| | | return StrUtil.isEmpty(str); |
| | | } |
| | | |
| | | /** |
| | | * * 判断一个字符串是否为非空串 |
| | | * |
| | | * @param str String |
| | | * @return true:非空串 false:空串 |
| | | */ |
| | | public static boolean isNotEmpty(String str) { |
| | | return !isEmpty(str); |
| | | } |
| | | |
| | | /** |
| | | * 去空格 |
| | | */ |
| | | public static String trim(String str) { |
| | | return StrUtil.trim(str); |
| | | } |
| | | |
| | | /** |
| | | * 截取字符串 |
| | | * |
| | | * @param str 字符串 |
| | | * @param start 开始 |
| | | * @return 结果 |
| | | */ |
| | | public static String substring(final String str, int start) { |
| | | return substring(str, start, str.length()); |
| | | } |
| | | |
| | | /** |
| | | * 截取字符串 |
| | | * |
| | | * @param str 字符串 |
| | | * @param start 开始 |
| | | * @param end 结束 |
| | | * @return 结果 |
| | | */ |
| | | public static String substring(final String str, int start, int end) { |
| | | return StrUtil.sub(str, start, end); |
| | | } |
| | | |
| | | /** |
| | | * 格式化文本, {} 表示占位符<br> |
| | | * 此方法只是简单将占位符 {} 按照顺序替换为参数<br> |
| | | * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br> |
| | | * 例:<br> |
| | | * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br> |
| | | * 转义{}: format("this is \\{} for {}", "a", "b") -> this is {} for a<br> |
| | | * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br> |
| | | * |
| | | * @param template 文本模板,被替换的部分用 {} 表示 |
| | | * @param params 参数值 |
| | | * @return 格式化后的文本 |
| | | */ |
| | | public static String format(String template, Object... params) { |
| | | return StrUtil.format(template, params); |
| | | } |
| | | |
| | | /** |
| | | * 是否为http(s)://开头 |
| | | * |
| | | * @param link 链接 |
| | | * @return 结果 |
| | | */ |
| | | public static boolean ishttp(String link) { |
| | | return Validator.isUrl(link); |
| | | } |
| | | |
| | | /** |
| | | * 字符串转set |
| | | * |
| | | * @param str 字符串 |
| | | * @param sep 分隔符 |
| | | * @return set集合 |
| | | */ |
| | | public static Set<String> str2Set(String str, String sep) { |
| | | return new HashSet<>(str2List(str, sep, true, false)); |
| | | } |
| | | |
| | | /** |
| | | * 字符串转list |
| | | * |
| | | * @param str 字符串 |
| | | * @param sep 分隔符 |
| | | * @param filterBlank 过滤纯空白 |
| | | * @param trim 去掉首尾空白 |
| | | * @return list集合 |
| | | */ |
| | | public static List<String> str2List(String str, String sep, boolean filterBlank, boolean trim) { |
| | | List<String> list = new ArrayList<>(); |
| | | if (isEmpty(str)) { |
| | | return list; |
| | | } |
| | | |
| | | // 过滤空白字符串 |
| | | if (filterBlank && isBlank(str)) { |
| | | return list; |
| | | } |
| | | String[] split = str.split(sep); |
| | | for (String string : split) { |
| | | if (filterBlank && isBlank(string)) { |
| | | continue; |
| | | } |
| | | if (trim) { |
| | | string = trim(string); |
| | | } |
| | | list.add(string); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 查找指定字符串是否包含指定字符串列表中的任意一个字符串同时串忽略大小写 |
| | | * |
| | | * @param cs 指定字符串 |
| | | * @param searchCharSequences 需要检查的字符串数组 |
| | | * @return 是否包含任意一个字符串 |
| | | */ |
| | | public static boolean containsAnyIgnoreCase(CharSequence cs, CharSequence... searchCharSequences) { |
| | | return StrUtil.containsAnyIgnoreCase(cs, searchCharSequences); |
| | | } |
| | | |
| | | /** |
| | | * 驼峰转下划线命名 |
| | | */ |
| | | public static String toUnderScoreCase(String str) { |
| | | return StrUtil.toUnderlineCase(str); |
| | | } |
| | | |
| | | /** |
| | | * 是否包含字符串 |
| | | * |
| | | * @param str 验证字符串 |
| | | * @param strs 字符串组 |
| | | * @return 包含返回true |
| | | */ |
| | | public static boolean inStringIgnoreCase(String str, String... strs) { |
| | | return StrUtil.equalsAnyIgnoreCase(str, strs); |
| | | } |
| | | |
| | | /** |
| | | * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld |
| | | * |
| | | * @param name 转换前的下划线大写方式命名的字符串 |
| | | * @return 转换后的驼峰式命名的字符串 |
| | | */ |
| | | public static String convertToCamelCase(String name) { |
| | | return StrUtil.upperFirst(StrUtil.toCamelCase(name)); |
| | | } |
| | | |
| | | /** |
| | | * 驼峰式命名法 例如:user_name->userName |
| | | */ |
| | | public static String toCamelCase(String s) { |
| | | return StrUtil.toCamelCase(s); |
| | | } |
| | | |
| | | /** |
| | | * 查找指定字符串是否匹配指定字符串列表中的任意一个字符串 |
| | | * |
| | | * @param str 指定字符串 |
| | | * @param strs 需要检查的字符串数组 |
| | | * @return 是否匹配 |
| | | */ |
| | | public static boolean matches(String str, List<String> strs) { |
| | | if (isEmpty(str) || CollUtil.isEmpty(strs)) { |
| | | return false; |
| | | } |
| | | for (String pattern : strs) { |
| | | if (isMatch(pattern, str)) { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 判断url是否与规则配置: |
| | | * ? 表示单个字符; |
| | | * * 表示一层路径内的任意字符串,不可跨层级; |
| | | * ** 表示任意层路径; |
| | | * |
| | | * @param pattern 匹配规则 |
| | | * @param url 需要匹配的url |
| | | */ |
| | | public static boolean isMatch(String pattern, String url) { |
| | | AntPathMatcher matcher = new AntPathMatcher(); |
| | | return matcher.match(pattern, url); |
| | | } |
| | | |
| | | /** |
| | | * 数字左边补齐0,使之达到指定长度。注意,如果数字转换为字符串后,长度大于size,则只保留 最后size个字符。 |
| | | * |
| | | * @param num 数字对象 |
| | | * @param size 字符串指定长度 |
| | | * @return 返回数字的字符串格式,该字符串为指定长度。 |
| | | */ |
| | | public static String padl(final Number num, final int size) { |
| | | return padl(num.toString(), size, '0'); |
| | | } |
| | | |
| | | /** |
| | | * 字符串左补齐。如果原始字符串s长度大于size,则只保留最后size个字符。 |
| | | * |
| | | * @param s 原始字符串 |
| | | * @param size 字符串指定长度 |
| | | * @param c 用于补齐的字符 |
| | | * @return 返回指定长度的字符串,由原字符串左补齐或截取得到。 |
| | | */ |
| | | public static String padl(final String s, final int size, final char c) { |
| | | final StringBuilder sb = new StringBuilder(size); |
| | | if (s != null) { |
| | | final int len = s.length(); |
| | | if (s.length() <= size) { |
| | | sb.append(String.valueOf(c).repeat(size - len)); |
| | | sb.append(s); |
| | | } else { |
| | | return s.substring(len - size, len); |
| | | } |
| | | } else { |
| | | sb.append(String.valueOf(c).repeat(Math.max(0, size))); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 切分字符串(分隔符默认逗号) |
| | | * |
| | | * @param str 被切分的字符串 |
| | | * @return 分割后的数据列表 |
| | | */ |
| | | public static List<String> splitList(String str) { |
| | | return splitTo(str, Convert::toStr); |
| | | } |
| | | |
| | | /** |
| | | * 切分字符串 |
| | | * |
| | | * @param str 被切分的字符串 |
| | | * @param separator 分隔符 |
| | | * @return 分割后的数据列表 |
| | | */ |
| | | public static List<String> splitList(String str, String separator) { |
| | | return splitTo(str, separator, Convert::toStr); |
| | | } |
| | | |
| | | /** |
| | | * 切分字符串自定义转换(分隔符默认逗号) |
| | | * |
| | | * @param str 被切分的字符串 |
| | | * @param mapper 自定义转换 |
| | | * @return 分割后的数据列表 |
| | | */ |
| | | public static <T> List<T> splitTo(String str, Function<? super Object, T> mapper) { |
| | | return splitTo(str, SEPARATOR, mapper); |
| | | } |
| | | |
| | | /** |
| | | * 切分字符串自定义转换 |
| | | * |
| | | * @param str 被切分的字符串 |
| | | * @param separator 分隔符 |
| | | * @param mapper 自定义转换 |
| | | * @return 分割后的数据列表 |
| | | */ |
| | | public static <T> List<T> splitTo(String str, String separator, Function<? super Object, T> mapper) { |
| | | if (isBlank(str)) { |
| | | return new ArrayList<>(0); |
| | | } |
| | | return StrUtil.split(str, separator) |
| | | .stream() |
| | | .filter(Objects::nonNull) |
| | | .map(mapper) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.ycl.common.utils.excel.utils; |
| | | |
| | | import com.ycl.common.utils.spring.SpringUtils; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import javax.validation.ConstraintViolation; |
| | | import javax.validation.ConstraintViolationException; |
| | | import javax.validation.Validator; |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * Validator 校验框架工具 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class ValidatorUtils { |
| | | |
| | | private static final Validator VALID = SpringUtils.getBean(Validator.class); |
| | | |
| | | /** |
| | | * 对给定对象进行参数校验,并根据指定的校验组进行校验 |
| | | * |
| | | * @param object 要进行校验的对象 |
| | | * @param groups 校验组 |
| | | * @throws ConstraintViolationException 如果校验不通过,则抛出参数校验异常 |
| | | */ |
| | | public static <T> void validate(T object, Class<?>... groups) { |
| | | Set<ConstraintViolation<T>> validate = VALID.validate(object, groups); |
| | | if (!validate.isEmpty()) { |
| | | throw new ConstraintViolationException("参数校验异常", validate); |
| | | } |
| | | } |
| | | |
| | | } |
| | |
| | | <artifactId>knife4j-spring-boot-starter</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- easy excel --> |
| | | <dependency> |
| | | <groupId>com.alibaba</groupId> |
| | | <artifactId>easyexcel</artifactId> |
| | | </dependency> |
| | | |
| | | |
| | | <!-- 验证码 --> |
| | | <dependency> |