This commit is contained in:
clay
2024-03-06 17:44:09 +08:00
commit adaec0eadd
1493 changed files with 219939 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
<?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>
<groupId>cn.fateverse</groupId>
<artifactId>common</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>common-excel</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>cn.fateverse</groupId>
<artifactId>admin-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>${easyexcel.version}</version>
</dependency>
<dependency>
<groupId>cn.fateverse</groupId>
<artifactId>common-core</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,23 @@
package cn.fateverse.common.excel;
import cn.fateverse.common.excel.service.ExcelService;
import org.springframework.context.annotation.Bean;
/**
* @author Clay
* @date 2023-11-10 20:18
*/
public class ExcelAutoConfiguration {
/**
* Excel导出时字典对接服务等
*
* @return excelService
*/
@Bean
public ExcelService excelService() {
return new ExcelService();
}
}

View File

@@ -0,0 +1,28 @@
package cn.fateverse.common.excel.service;
import cn.fateverse.admin.dubbo.DubboDictDataService;
import cn.fateverse.admin.vo.DictDataVo;
import org.apache.dubbo.config.annotation.DubboReference;
import java.util.List;
import java.util.Map;
/**
* @author Clay
* @date 2023-02-20
*/
public class ExcelService {
@DubboReference
private DubboDictDataService dictDataService;
public DubboDictDataService getDictDataService() {
return dictDataService;
}
public Map<String, Map<String, DictDataVo>> searchDictDataCacheKeys(List<String> cacheKeys){
return dictDataService.searchDictDataCacheKeys(cacheKeys);
}
}

View File

@@ -0,0 +1,47 @@
package cn.fateverse.common.excel.utils;
import cn.fateverse.common.core.annotaion.Excel;
import cn.fateverse.common.core.annotaion.Excels;
import lombok.Data;
import java.lang.reflect.Field;
/**
* @author Clay
* @date 2022/12/19
*/
@Data
public class ExcelAssist {
private Field field;
private Excel excel;
private Excels excels;
private int order;
private String objectFieldName;
public ExcelAssist(Field field, Excels excels) {
this.field = field;
this.objectFieldName = field.getName();
this.excels = excels;
order = excels.order();
}
public ExcelAssist(Field field, Excel excel, ExcelAssist assist) {
this.field = field;
this.excel = excel;
if (null != assist) {
this.objectFieldName = assist.objectFieldName;
if (excel.order() != Integer.MAX_VALUE && assist.getExcels().order() != Integer.MAX_VALUE) {
order = assist.getExcels().order() * 10 + excel.order();
} else {
order = excel.order();
}
} else {
order = excel.order();
}
}
}

View File

@@ -0,0 +1,206 @@
package cn.fateverse.common.excel.utils;
import cn.fateverse.common.excel.service.ExcelService;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.fateverse.admin.vo.DictDataVo;
import cn.fateverse.common.core.exception.CustomException;
import cn.fateverse.common.core.utils.HttpServletUtils;
import cn.fateverse.common.core.utils.SpringContextHolder;
import cn.fateverse.common.core.annotaion.Excel;
import cn.fateverse.common.core.annotaion.Excels;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.ehcache.impl.internal.concurrent.ConcurrentHashMap;
import org.springframework.util.ReflectionUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author Clay
* @date 2022/12/19
*/
public class ExcelUtil {
private static final Map<Class<?>, ExcelAssistWrapper> wrapperMap = new ConcurrentHashMap<>(8);
private static Map<String, Map<String, DictDataVo>> dictDataMap;
private static ExcelService getBean() {
return SpringContextHolder.getBean(ExcelService.class);
}
/**
* 获取到数据列表
*
* @param list
* @return
*/
private static List<List<Object>> getDataList(List<?> list, List<ExcelAssist> assistList) {
return list.stream().map(object ->
assistList.stream().map(assist -> {
Field field = assist.getField();
field.setAccessible(true);
Excel excel = assist.getExcel();
Object value = null;
if (StrUtil.isEmpty(assist.getObjectFieldName())) {
value = getData(object, excel, field);
} else {
Object sunValue = null;
try {
sunValue = ReflectionUtils.getField(field, object);
} catch (Exception e) {
return null;
}
if (null != sunValue) {
value = getData(object, excel, field);
}
}
return value;
}).collect(Collectors.toList())
).collect(Collectors.toList());
}
/**
* 获取到对象中的数据
*
* @param object
* @param excel
* @param field
* @return
*/
private static Object getData(Object object, Excel excel, Field field) {
try {
Object objectValue = ReflectionUtils.getField(field, object);
if (objectValue instanceof Date) {
objectValue = DateUtil.format((Date) objectValue, excel.dateFormat());
}
String dictType = excel.dictType();
if (!StrUtil.isEmpty(dictType)) {
Map<String, DictDataVo> dictMap = dictDataMap.get(dictType);
if (null != dictMap) {
DictDataVo dictData = dictMap.get(objectValue.toString());
if (null != dictData) {
objectValue = dictData.getDictLabel();
}
}
}
return objectValue;
} catch (Exception e) {
return null;
}
}
/**
* 初始化header数据
*
* @param clazz
* @param assist
*/
private static void initHeader(Class<?> clazz, ExcelAssist assist, List<ExcelAssist> assistList) {
for (Field field : clazz.getDeclaredFields()) {
Excel excel = field.getAnnotation(Excel.class);
if (null != excel) {
assistList.add(new ExcelAssist(field, excel, assist));
} else {
Excels excels = field.getAnnotation(Excels.class);
if (excels == null) {
continue;
}
Class<?> objectValue = field.getType();
if (clazz == objectValue) {
throw new CustomException("不允许嵌套对象导出Excel");
}
ExcelAssist excelAssist = new ExcelAssist(field, excels);
initHeader(objectValue, excelAssist, assistList);
}
}
}
public static void exportExcel(List<?> list, Class<?> clazz) {
exportExcel(list, clazz, null);
}
/**
* 导出数据Excel
*
* @param list
* @param clazz
* @param sheetName
*/
public static void exportExcel(List<?> list, Class<?> clazz, String sheetName) {
Set<String> dictList = new HashSet<>();
ExcelAssistWrapper wrapper = wrapperMap.get(clazz);
if (wrapper == null) {
synchronized (wrapperMap) {
wrapper = wrapperMap.get(clazz);
if (wrapper == null) {
wrapper = new ExcelAssistWrapper();
List<ExcelAssist> assistList = new ArrayList<>();
initHeader(clazz, null, assistList);
List<List<String>> headerList = new ArrayList<>(assistList.size());
assistList = assistList.stream().sorted(Comparator.comparing(ExcelAssist::getOrder))
.peek(assist -> headerList.add(Collections.singletonList(assist.getExcel().value())))
.peek(assist -> {
String dictType = assist.getExcel().dictType();
if (!StrUtil.isEmpty(dictType)) {
dictList.add(dictType);
}
})
.collect(Collectors.toList());
wrapper.assistList = assistList;
wrapper.headerList = headerList;
wrapperMap.put(clazz, wrapper);
}
}
}
if (dictList.size() > 0) {
getDictData(new ArrayList<>(dictList));
}
List<List<Object>> dataList = getDataList(list, wrapper.assistList);
try {
sheetName = StrUtil.isEmpty(sheetName) ? "sheet" : sheetName;
HttpServletResponse response = getResponse(sheetName);
EasyExcel.write(response.getOutputStream())
.head(wrapper.headerList)
.excelType(ExcelTypeEnum.XLSX)
.sheet(StrUtil.isEmpty(sheetName) ? "sheet" : sheetName)
.doWrite(dataList);
} catch (IOException e) {
throw new CustomException("Excel导出失败!");
}
}
private static HttpServletResponse getResponse(String sheetName) {
HttpServletResponse response = HttpServletUtils.getResponse();
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition",
"attachment;fileName=" + sheetName + UUID.randomUUID() + ".xlsx");
return response;
}
private static void getDictData(List<String> dictList) {
ExcelService dictDataService = getBean();
Map<String, Map<String, DictDataVo>> result = dictDataService.searchDictDataCacheKeys(dictList);
if (null != result) {
System.out.println(result.toString());
dictDataMap = result;
}
}
private static class ExcelAssistWrapper {
private List<ExcelAssist> assistList;
List<List<String>> headerList;
}
}

View File

@@ -0,0 +1 @@
cn.fateverse.common.excel.ExcelAutoConfiguration