This commit is contained in:
kk
2026-07-07 18:01:21 +08:00
commit b259f94d4b
1088 changed files with 121778 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>BladeX-Tool</artifactId>
<groupId>org.springblade</groupId>
<version>${revision}</version>
</parent>
<artifactId>blade-starter-excel</artifactId>
<name>${project.artifactId}</name>
<version>${project.parent.version}</version>
<packaging>jar</packaging>
<properties>
<module.name>org.springblade.blade.starter.excel</module.name>
</properties>
<dependencies>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-launch</artifactId>
</dependency>
<dependency>
<groupId>cn.idev.excel</groupId>
<artifactId>fastexcel</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-full</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,60 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.core.excel.listener;
import cn.idev.excel.context.AnalysisContext;
import cn.idev.excel.event.AnalysisEventListener;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import java.util.ArrayList;
import java.util.List;
/**
* Excel监听器
*
* @author Chill
*/
@Getter
@EqualsAndHashCode(callSuper = true)
public class DataListener<T> extends AnalysisEventListener<T> {
/**
* 缓存的数据列表
*/
private final List<T> dataList = new ArrayList<>();
@Override
public void invoke(T data, AnalysisContext analysisContext) {
dataList.add(data);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
@@ -0,0 +1,173 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.core.excel.listener;
import cn.idev.excel.annotation.ExcelProperty;
import cn.idev.excel.annotation.ExcelIgnore;
import cn.idev.excel.context.AnalysisContext;
import cn.idev.excel.event.AnalysisEventListener;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import org.springblade.core.excel.support.ExcelException;
import org.springblade.core.excel.support.ExcelImporter;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
* Excel监听器
*
* @author Chill
*/
@Data
@RequiredArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class ImportListener<T> extends AnalysisEventListener<T> {
/**
* 默认每隔3000条存储数据库
*/
private int batchCount = 3000;
/**
* 缓存的数据列表
*/
private List<T> list = new ArrayList<>();
/**
* 数据导入类
*/
private final ExcelImporter<T> importer;
/**
* 数据映射类
*/
private final Class<T> clazz;
/**
* 是否校验
*/
private final Boolean check;
/**
* 表头缓存映射 - 静态缓存避免重复反射,提供懒加载机制
*/
private static final Map<Class<?>, List<String>> HEADER_CACHE = new ConcurrentHashMap<>();
@Override
public void invoke(T data, AnalysisContext analysisContext) {
list.add(data);
// 达到BATCH_COUNT,则调用importer方法入库,防止数据几万条数据在内存,容易OOM
if (list.size() >= batchCount) {
// 调用importer方法
importer.save(list);
// 存储完成清理list
list.clear();
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
// 调用importer方法
importer.save(list);
// 存储完成清理list
list.clear();
}
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
if (!Boolean.TRUE.equals(check)) {
super.invokeHeadMap(headMap, context);
return;
}
List<String> expectedHeaders = extractExpectedHeaders();
validateHeaderConsistency(headMap, expectedHeaders);
super.invokeHeadMap(headMap, context);
}
/**
* 提取期望的表头列表
* 使用静态缓存实现懒加载,避免重复反射操作
*/
private List<String> extractExpectedHeaders() {
return HEADER_CACHE.computeIfAbsent(clazz, this::parseHeadersFromClass);
}
/**
* 从类中解析表头信息
* 排除带有@ExcelIgnore注解的字段
*/
private List<String> parseHeadersFromClass(Class<?> targetClass) {
return Arrays.stream(targetClass.getDeclaredFields())
.filter(field -> field.isAnnotationPresent(ExcelProperty.class))
.filter(field -> !field.isAnnotationPresent(ExcelIgnore.class))
.map(this::extractHeaderFromField)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
}
/**
* 从字段中提取表头名称
*/
private Optional<String> extractHeaderFromField(Field field) {
ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
String[] headerValues = annotation.value();
return headerValues.length > 0 ? Optional.of(headerValues[0]) : Optional.empty();
}
/**
* 校验表头一致性
* 检查实际表头是否都存在于期望表头集合中
*/
private void validateHeaderConsistency(Map<Integer, String> actualHeaders, List<String> expectedHeaders) {
// 获取期望表头的集合
Set<String> expectedHeaderSet = new HashSet<>(expectedHeaders);
// 检查实际表头是否有不在预期表头列表中
actualHeaders.values().stream()
.filter(actualHeader -> !expectedHeaderSet.contains(actualHeader))
.findFirst()
.ifPresent(invalidHeader -> {
throw new ExcelException(
String.format("表头校验失败:[%s] 不在预期的表头列表中,预期表头:%s", invalidHeader, expectedHeaders)
);
});
// 检查是否有缺失的必要表头
if (actualHeaders.size() != expectedHeaders.size()) {
Set<String> actualHeaderSet = new HashSet<>(actualHeaders.values());
List<String> missingHeaders = expectedHeaders.stream()
.filter(expected -> !actualHeaderSet.contains(expected))
.toList();
if (!missingHeaders.isEmpty()) {
throw new ExcelException(
String.format("表头校验失败:缺少必要的表头:%s", missingHeaders)
);
}
}
}
}
@@ -0,0 +1,42 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.core.excel.support;
import java.io.Serial;
/**
* Excel异常处理类
*
* @author Chill
*/
public class ExcelException extends RuntimeException {
@Serial
private static final long serialVersionUID = 1L;
public ExcelException(String message) {
super(message);
}
}
@@ -0,0 +1,44 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.core.excel.support;
import java.util.List;
/**
* Excel统一导入接口
*
* @author Chill
*/
public interface ExcelImporter<T> {
/**
* 导入数据逻辑
*
* @param data 数据集合
*/
void save(List<T> data);
}
@@ -0,0 +1,233 @@
/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package org.springblade.core.excel.util;
import cn.idev.excel.FastExcel;
import cn.idev.excel.read.builder.ExcelReaderBuilder;
import cn.idev.excel.read.listener.ReadListener;
import cn.idev.excel.util.DateUtils;
import cn.idev.excel.write.builder.ExcelWriterBuilder;
import cn.idev.excel.write.handler.WriteHandler;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springblade.core.excel.listener.DataListener;
import org.springblade.core.excel.listener.ImportListener;
import org.springblade.core.excel.support.ExcelException;
import org.springblade.core.excel.support.ExcelImporter;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
/**
* Excel工具类
*
* @author Chill
* @apiNote https://readmex.com/fast-excel/fastexcel
*/
public class ExcelUtil {
/**
* 读取excel的所有sheet数据
*
* @param excel excel文件
* @return List<Object>
*/
public static <T> List<T> read(MultipartFile excel, Class<T> clazz) {
DataListener<T> dataListener = new DataListener<>();
ExcelReaderBuilder builder = getReaderBuilder(excel, dataListener, clazz);
if (builder == null) {
return null;
}
builder.doReadAll();
return dataListener.getDataList();
}
/**
* 读取excel的指定sheet数据
*
* @param excel excel文件
* @param sheetNo sheet序号(从0开始)
* @return List<Object>
*/
public static <T> List<T> read(MultipartFile excel, int sheetNo, Class<T> clazz) {
return read(excel, sheetNo, 1, clazz);
}
/**
* 读取excel的指定sheet数据
*
* @param excel excel文件
* @param sheetNo sheet序号(从0开始)
* @param headRowNumber 表头行数
* @return List<Object>
*/
public static <T> List<T> read(MultipartFile excel, int sheetNo, int headRowNumber, Class<T> clazz) {
DataListener<T> dataListener = new DataListener<>();
ExcelReaderBuilder builder = getReaderBuilder(excel, dataListener, clazz);
if (builder == null) {
return null;
}
builder.sheet(sheetNo).headRowNumber(headRowNumber).doRead();
return dataListener.getDataList();
}
/**
* 读取不校验直接导入数据
*
* @param excel excel文件
* @param importer 导入逻辑类
* @param <T> 泛型
*/
public static <T> void save(MultipartFile excel, ExcelImporter<T> importer, Class<T> clazz) {
save(excel, importer, clazz, false);
}
/**
* 读取并校验后导入数据
*
* @param excel excel文件
* @param importer 导入逻辑类
* @param clazz class类
* @param <T> 泛型
*/
public static <T> void saveWithCheck(MultipartFile excel, ExcelImporter<T> importer, Class<T> clazz) {
save(excel, importer, clazz, true);
}
/**
* 读取不校验直接导入数据
*
* @param excel excel文件
* @param importer 导入逻辑类
* @param clazz class类
* @param <T> 泛型
*/
public static <T> void saveWithoutCheck(MultipartFile excel, ExcelImporter<T> importer, Class<T> clazz) {
save(excel, importer, clazz, false);
}
/**
* 读取并导入数据
*
* @param excel excel文件
* @param importer 导入逻辑类
* @param <T> 泛型
* @param check 是否校验
*/
public static <T> void save(MultipartFile excel, ExcelImporter<T> importer, Class<T> clazz, Boolean check) {
ImportListener<T> importListener = new ImportListener<>(importer, clazz, check);
ExcelReaderBuilder builder = getReaderBuilder(excel, importListener, clazz);
if (builder != null) {
builder.doReadAll();
}
}
/**
* 导出excel
*
* @param response 响应类
* @param dataList 数据列表
* @param clazz class类
* @param <T> 泛型
*/
@SneakyThrows
public static <T> void export(HttpServletResponse response, List<T> dataList, Class<T> clazz) {
export(response, DateUtils.format(new Date(), DateUtils.DATE_FORMAT_14), "导出数据", dataList, clazz);
}
/**
* 导出excel
*
* @param response 响应类
* @param fileName 文件名
* @param sheetName sheet名
* @param dataList 数据列表
* @param clazz class类
* @param <T> 泛型
*/
@SneakyThrows
public static <T> void export(HttpServletResponse response, String fileName, String sheetName, List<T> dataList, Class<T> clazz) {
export(response, fileName, sheetName, dataList, null, clazz);
}
/**
* 导出excel
*
* @param response 响应类
* @param fileName 文件名
* @param sheetName sheet名
* @param dataList 数据列表
* @param clazz class类
* @param writeHandler 自定义处理器
* @param <T> 泛型
*/
@SneakyThrows
public static <T> void export(HttpServletResponse response, String fileName, String sheetName, List<T> dataList, WriteHandler writeHandler, Class<T> clazz) {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
ExcelWriterBuilder write = FastExcel.write(response.getOutputStream(), clazz);
if (writeHandler != null) {
write.registerWriteHandler(writeHandler);
}
write.sheet(sheetName).doWrite(dataList);
}
/**
* 获取构建类
*
* @param excel excel文件
* @param readListener excel监听类
* @return ExcelReaderBuilder
*/
public static <T> ExcelReaderBuilder getReaderBuilder(MultipartFile excel, ReadListener<T> readListener, Class<T> clazz) {
String filename = excel.getOriginalFilename();
if (!StringUtils.hasText(filename)) {
throw new ExcelException("请上传文件!");
}
if ((!StringUtils.endsWithIgnoreCase(filename, ".xls") && !StringUtils.endsWithIgnoreCase(filename, ".xlsx"))) {
throw new ExcelException("请上传正确的excel文件!");
}
InputStream inputStream;
try {
inputStream = new BufferedInputStream(excel.getInputStream());
return FastExcel.read(inputStream, clazz, readListener);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}