Commit 2e390d9d by zhangxingmin

修复

parent 01a5c508
...@@ -14,6 +14,8 @@ import org.apache.commons.lang3.StringUtils; ...@@ -14,6 +14,8 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
...@@ -41,6 +43,14 @@ public class ExcelExportServiceImpl implements ExcelExportService { ...@@ -41,6 +43,14 @@ public class ExcelExportServiceImpl implements ExcelExportService {
*/ */
@Override @Override
public ExportResult exportAndUploadToOss(List<?> dataList, ExportParam exportParam, Class<?> entityClass) { public ExportResult exportAndUploadToOss(List<?> dataList, ExportParam exportParam, Class<?> entityClass) {
// 添加参数校验
if (exportParam == null) {
return ExportResult.error("导出参数不能为空");
}
if (CollectionUtils.isEmpty(exportParam.getFieldNames())) {
return ExportResult.error("导出字段列表不能为空");
}
// 声明输出流和工作簿变量,用于后续资源关闭 // 声明输出流和工作簿变量,用于后续资源关闭
ByteArrayOutputStream outputStream = null; ByteArrayOutputStream outputStream = null;
Workbook workbook = null; Workbook workbook = null;
...@@ -52,13 +62,38 @@ public class ExcelExportServiceImpl implements ExcelExportService { ...@@ -52,13 +62,38 @@ public class ExcelExportServiceImpl implements ExcelExportService {
// 将数据列表转换为Map结构,便于EasyPOI处理 // 将数据列表转换为Map结构,便于EasyPOI处理
List<Map<String, Object>> dataMapList = buildDataMapList(dataList, exportParam.getFieldNames(), entityClass); List<Map<String, Object>> dataMapList = buildDataMapList(dataList, exportParam.getFieldNames(), entityClass);
// 添加空数据检查
if (CollectionUtils.isEmpty(dataMapList)) {
// 创建一个空数据的Map,确保至少有表头
dataMapList = new ArrayList<>();
Map<String, Object> emptyMap = new HashMap<>();
for (String fieldName : exportParam.getFieldNames()) {
emptyMap.put(fieldName, "");
}
dataMapList.add(emptyMap);
}
// 检查entityList是否为空
if (CollectionUtils.isEmpty(entityList)) {
return ExportResult.error("导出列配置不能为空");
}
// 创建字节数组输出流,用于将Excel数据写入内存 // 创建字节数组输出流,用于将Excel数据写入内存
outputStream = new ByteArrayOutputStream(); outputStream = new ByteArrayOutputStream();
// 设置导出参数:无标题、工作表名称为sheet1、使用XSSF格式(支持.xlsx) // 设置导出参数:无标题、工作表名称为sheet1、使用XSSF格式(支持.xlsx)
ExportParams params = new ExportParams(null, "sheet1", ExcelType.XSSF); ExportParams params = new ExportParams(null, "sheet1", ExcelType.XSSF);
// 确保params不为null
if (params == null) {
params = new ExportParams();
}
// 使用EasyPOI导出Excel到工作簿 // 使用EasyPOI导出Excel到工作簿
workbook = ExcelExportUtil.exportExcel(params, entityList, dataMapList); workbook = ExcelExportUtil.exportExcel(params, entityList, dataMapList);
// 检查workbook是否创建成功
if (workbook == null) {
return ExportResult.error("Excel工作簿创建失败");
}
// 将工作簿内容写入输出流 // 将工作簿内容写入输出流
workbook.write(outputStream); workbook.write(outputStream);
...@@ -145,16 +180,26 @@ public class ExcelExportServiceImpl implements ExcelExportService { ...@@ -145,16 +180,26 @@ public class ExcelExportServiceImpl implements ExcelExportService {
// 创建导出列配置列表 // 创建导出列配置列表
List<ExcelExportEntity> entityList = new ArrayList<>(); List<ExcelExportEntity> entityList = new ArrayList<>();
// 获取实体类字段的注解映射(字段名->Excel列名) // 获取实体类字段的注解映射(字段名->Excel列名)
Map<String, String> fieldAnnotationMap = getFieldAnnotations(entityClass); Map<String, String> fieldAnnotationMap = null;
if (!Objects.isNull(entityClass)) {
fieldAnnotationMap = getFieldAnnotations(entityClass);
}
// 遍历所有需要导出的字段 // 遍历所有需要导出的字段
for (String fieldName : fieldNames) { for (String fieldName : fieldNames) {
// 检查字段是否存在于注解映射中 // 检查字段是否存在于注解映射中
if (fieldAnnotationMap.containsKey(fieldName)) { if (!Objects.isNull(fieldAnnotationMap) && fieldAnnotationMap.containsKey(fieldName)) {
//传入了entityClass实体类型
// 创建Excel导出列实体:参数1为Excel列标题,参数2为实体字段名 // 创建Excel导出列实体:参数1为Excel列标题,参数2为实体字段名
ExcelExportEntity entity = new ExcelExportEntity(fieldAnnotationMap.get(fieldName), fieldName); ExcelExportEntity entity = new ExcelExportEntity(fieldAnnotationMap.get(fieldName), fieldName);
// 将列配置添加到列表 // 将列配置添加到列表
entityList.add(entity); entityList.add(entity);
}else {
//没有传入entityClass实体类型,那就取fieldNames做为ExcelExportEntity参数1和参数2值
// 创建Excel导出列实体:参数1为Excel列标题,参数2为实体字段名
ExcelExportEntity entity = new ExcelExportEntity(fieldName, fieldName);
// 将列配置添加到列表
entityList.add(entity);
} }
} }
return entityList; return entityList;
...@@ -195,7 +240,9 @@ public class ExcelExportServiceImpl implements ExcelExportService { ...@@ -195,7 +240,9 @@ public class ExcelExportServiceImpl implements ExcelExportService {
private List<Map<String, Object>> buildDataMapList(List<?> dataList, List<String> fieldNames, Class<?> entityClass) { private List<Map<String, Object>> buildDataMapList(List<?> dataList, List<String> fieldNames, Class<?> entityClass) {
// 创建数据Map列表 // 创建数据Map列表
List<Map<String, Object>> dataMapList = new ArrayList<>(); List<Map<String, Object>> dataMapList = new ArrayList<>();
if (CollectionUtils.isEmpty(dataList)) {
return dataMapList;
}
// 遍历数据列表中的每个对象 // 遍历数据列表中的每个对象
for (Object data : dataList) { for (Object data : dataList) {
// 为每个对象创建字段-值的映射 // 为每个对象创建字段-值的映射
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment