init
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package com.newyetai.commonexport.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class CommonExportDynamicDTO implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
private String fileName;
|
||||
|
||||
/**
|
||||
* 表头列表
|
||||
*/
|
||||
private List<String> head;
|
||||
|
||||
/**
|
||||
* List<T> : 数据
|
||||
*/
|
||||
private List<List<Object>> datas;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.newyetai.commonexport.dto;
|
||||
|
||||
import com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy;
|
||||
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 导出多个sheet实体类
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class CommonExportMapDTO<T> implements Serializable {
|
||||
private static final long serialVersionUID = -8482074133630026236L;
|
||||
|
||||
private static final String FILE_SUFFIX = ".xlsx";
|
||||
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
private String fileName;
|
||||
|
||||
/**
|
||||
* 文件后缀
|
||||
*/
|
||||
private String fileSuffix;
|
||||
|
||||
/**
|
||||
* 列宽度策略
|
||||
*/
|
||||
private AbstractHeadColumnWidthStyleStrategy columnWidthStyleStrategy;
|
||||
|
||||
/**
|
||||
* key : sheetName
|
||||
* List<T> : 数据
|
||||
*/
|
||||
private Map<String, List<T>> datas;
|
||||
|
||||
public String getFileSuffix() {
|
||||
if (StringUtils.isEmpty(fileSuffix)) {
|
||||
this.fileSuffix = FILE_SUFFIX;
|
||||
}
|
||||
return this.fileSuffix;
|
||||
}
|
||||
|
||||
public AbstractHeadColumnWidthStyleStrategy getColumnWidthStyleStrategy() {
|
||||
if (this.columnWidthStyleStrategy == null) {
|
||||
this.columnWidthStyleStrategy = new SimpleColumnWidthStyleStrategy(20);
|
||||
}
|
||||
return this.columnWidthStyleStrategy;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.newyetai.commonexport.dto;
|
||||
|
||||
import com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy;
|
||||
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author chenyun
|
||||
* @description 公共导出DTO
|
||||
* @date 2020/10/21 14:03
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class CommonExportPropertiesDTO<T> implements Serializable {
|
||||
private static final long serialVersionUID = -8482074133630026236L;
|
||||
|
||||
private static final String FILE_SUFFIX = ".xlsx";
|
||||
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
private String fileName;
|
||||
|
||||
/**
|
||||
* 文件后缀
|
||||
*/
|
||||
private String fileSuffix;
|
||||
|
||||
/**
|
||||
* 列宽度策略
|
||||
*/
|
||||
private AbstractHeadColumnWidthStyleStrategy columnWidthStyleStrategy;
|
||||
|
||||
/**
|
||||
* sheet 名称
|
||||
*/
|
||||
private String sheetName;
|
||||
|
||||
/**
|
||||
* 导出头
|
||||
*/
|
||||
private List<T> datas;
|
||||
|
||||
public String getFileSuffix() {
|
||||
if (StringUtils.isEmpty(fileSuffix)) {
|
||||
this.fileSuffix = FILE_SUFFIX;
|
||||
}
|
||||
return this.fileSuffix;
|
||||
}
|
||||
|
||||
public AbstractHeadColumnWidthStyleStrategy getColumnWidthStyleStrategy() {
|
||||
if (this.columnWidthStyleStrategy == null) {
|
||||
this.columnWidthStyleStrategy = new SimpleColumnWidthStyleStrategy(20);
|
||||
}
|
||||
return this.columnWidthStyleStrategy;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.newyetai.commonexport.service;
|
||||
|
||||
import com.newyetai.commonexport.dto.CommonExportDynamicDTO;
|
||||
import com.newyetai.commonexport.dto.CommonExportMapDTO;
|
||||
import com.newyetai.commonexport.dto.CommonExportPropertiesDTO;
|
||||
import com.newyetai.component.file.entity.FileRecord;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ExportService {
|
||||
|
||||
/**
|
||||
* 公共导出方法
|
||||
*
|
||||
* @param commonExportPropertiesDTO 导出实体
|
||||
* @return FileRecord
|
||||
*/
|
||||
FileRecord export(CommonExportPropertiesDTO commonExportPropertiesDTO);
|
||||
|
||||
|
||||
FileRecord export(CommonExportMapDTO commonExportMapDTO);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.newyetai.commonexport.service.impl;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.apache.poi.ss.formula.functions.T;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.ExcelWriter;
|
||||
import com.newyetai.common.exception.BusinessException;
|
||||
import com.newyetai.commonexport.dto.CommonExportMapDTO;
|
||||
import com.newyetai.commonexport.dto.CommonExportPropertiesDTO;
|
||||
import com.newyetai.commonexport.service.ExportService;
|
||||
import com.newyetai.component.file.config.FileProperties;
|
||||
import com.newyetai.component.file.entity.FileRecord;
|
||||
import com.newyetai.component.file.service.FileService;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ExportByDtoServiceImpl implements ExportService {
|
||||
|
||||
@Autowired
|
||||
private FileProperties fileProperties;
|
||||
|
||||
@Autowired
|
||||
private FileService fileService;
|
||||
|
||||
public FileRecord export(CommonExportPropertiesDTO commonExportPropertiesDTO) {
|
||||
CommonExportMapDTO<T> objectCommonExportMapDTO = new CommonExportMapDTO<>();
|
||||
objectCommonExportMapDTO.setFileName(commonExportPropertiesDTO.getFileName());
|
||||
objectCommonExportMapDTO.setColumnWidthStyleStrategy(
|
||||
commonExportPropertiesDTO.getColumnWidthStyleStrategy());
|
||||
objectCommonExportMapDTO.setFileSuffix(commonExportPropertiesDTO.getFileSuffix());
|
||||
objectCommonExportMapDTO.setDatas(new HashMap<String, List<T>>() {
|
||||
{
|
||||
put(commonExportPropertiesDTO.getSheetName(), commonExportPropertiesDTO.getDatas());
|
||||
}
|
||||
});
|
||||
return this.export(objectCommonExportMapDTO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileRecord export(CommonExportMapDTO commonExportMapDTO) {
|
||||
Map<String, List> datas = commonExportMapDTO.getDatas();
|
||||
if (CollUtil.isEmpty(datas)) {
|
||||
return null;
|
||||
}
|
||||
Optional<List> first = datas.values().stream().findFirst();
|
||||
if (!first.isPresent() || CollUtil.isEmpty(first.get())) {
|
||||
return null;
|
||||
}
|
||||
Class<?> aClass = first.get().get(0).getClass();
|
||||
// 临时文件名
|
||||
String targetFileName = commonExportMapDTO.getFileName() + commonExportMapDTO.getFileSuffix();
|
||||
//3.调用excelUtil 生成临时文件
|
||||
try {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ExcelWriter writer = EasyExcel.write(os, aClass)
|
||||
.registerWriteHandler(commonExportMapDTO.getColumnWidthStyleStrategy()).build();
|
||||
|
||||
datas.forEach((key, value) -> writer.write(value, EasyExcel.writerSheet(key).build()));
|
||||
writer.finish();
|
||||
byte[] excelByte = os.toByteArray();
|
||||
return fileService.uploadTemp(targetFileName, excelByte);
|
||||
} catch (
|
||||
Exception e) {
|
||||
log.info("导出文件异常", e);
|
||||
throw new BusinessException("导出文件异常");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user