|
|
|
|
@@ -8,19 +8,26 @@ import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
|
|
|
|
|
import com.alibaba.excel.write.handler.WriteHandler;
|
|
|
|
|
import com.alibaba.excel.write.metadata.WriteSheet;
|
|
|
|
|
import com.hccake.common.excel.annotation.ResponseExcel;
|
|
|
|
|
import com.hccake.common.excel.annotation.Sheet;
|
|
|
|
|
import com.hccake.common.excel.aop.DynamicNameAspect;
|
|
|
|
|
import com.hccake.common.excel.config.ExcelConfigProperties;
|
|
|
|
|
import com.hccake.common.excel.converters.LocalDateStringConverter;
|
|
|
|
|
import com.hccake.common.excel.converters.LocalDateTimeStringConverter;
|
|
|
|
|
|
|
|
|
|
import com.hccake.common.excel.enhance.WriterBuilderEnhancer;
|
|
|
|
|
import com.hccake.common.excel.head.HeadGenerator;
|
|
|
|
|
import com.hccake.common.excel.head.HeadMeta;
|
|
|
|
|
import com.hccake.common.excel.kit.ExcelException;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.SneakyThrows;
|
|
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
|
import org.springframework.beans.factory.ObjectProvider;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
|
|
import org.springframework.core.io.ClassPathResource;
|
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
import org.springframework.web.context.request.RequestAttributes;
|
|
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
|
@@ -28,6 +35,7 @@ import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.lang.reflect.Modifier;
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
@@ -39,7 +47,7 @@ import java.util.Objects;
|
|
|
|
|
* @date 2020/3/31
|
|
|
|
|
*/
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public abstract class AbstractSheetWriteHandler implements SheetWriteHandler {
|
|
|
|
|
public abstract class AbstractSheetWriteHandler implements SheetWriteHandler, ApplicationContextAware {
|
|
|
|
|
|
|
|
|
|
private final ExcelConfigProperties configProperties;
|
|
|
|
|
|
|
|
|
|
@@ -47,13 +55,15 @@ public abstract class AbstractSheetWriteHandler implements SheetWriteHandler {
|
|
|
|
|
|
|
|
|
|
private final WriterBuilderEnhancer excelWriterBuilderEnhance;
|
|
|
|
|
|
|
|
|
|
private ApplicationContext applicationContext;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void check(ResponseExcel responseExcel) {
|
|
|
|
|
if (!StringUtils.hasText(responseExcel.name())) {
|
|
|
|
|
throw new ExcelException("@ResponseExcel name 配置不合法");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (responseExcel.sheet().length == 0) {
|
|
|
|
|
if (responseExcel.sheets().length == 0) {
|
|
|
|
|
throw new ExcelException("@ResponseExcel sheet 配置不合法");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -135,37 +145,66 @@ public abstract class AbstractSheetWriteHandler implements SheetWriteHandler {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取 WriteSheet 对象
|
|
|
|
|
* @param sheetNo 页签号
|
|
|
|
|
* @param sheetName 页签名称
|
|
|
|
|
* @param sheet sheet annotation info
|
|
|
|
|
* @param dataClass 数据类型
|
|
|
|
|
* @param template 模板
|
|
|
|
|
* @param headEnhancerClass 自定义头处理器
|
|
|
|
|
* @return WriteSheet
|
|
|
|
|
*/
|
|
|
|
|
public WriteSheet sheet(Integer sheetNo, String sheetName, Class<?> dataClass, String template,
|
|
|
|
|
public WriteSheet sheet(Sheet sheet, Class<?> dataClass, String template,
|
|
|
|
|
Class<? extends HeadGenerator> headEnhancerClass) {
|
|
|
|
|
// 头信息增强
|
|
|
|
|
HeadGenerator headGenerator = null;
|
|
|
|
|
if (!headEnhancerClass.isInterface()) {
|
|
|
|
|
headGenerator = BeanUtils.instantiateClass(headEnhancerClass);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 是否模板写入
|
|
|
|
|
Integer sheetNo = sheet.sheetNo() >= 0 ? sheet.sheetNo() : null;
|
|
|
|
|
ExcelWriterSheetBuilder writerSheetBuilder = StringUtils.hasText(template) ? EasyExcel.writerSheet(sheetNo)
|
|
|
|
|
: EasyExcel.writerSheet(sheetNo, sheetName);
|
|
|
|
|
// 自定义头信息
|
|
|
|
|
if (headGenerator != null) {
|
|
|
|
|
writerSheetBuilder.head(headGenerator.head(dataClass));
|
|
|
|
|
: EasyExcel.writerSheet(sheetNo, sheet.sheetName());
|
|
|
|
|
|
|
|
|
|
Class<? extends HeadGenerator> headGenerateClass = sheet.headGenerateClass();
|
|
|
|
|
// 头信息增强
|
|
|
|
|
if (isNotInterface(headGenerateClass)) {
|
|
|
|
|
fillCustomHeadInfo(dataClass, headGenerateClass, writerSheetBuilder);
|
|
|
|
|
}
|
|
|
|
|
else if (isNotInterface(headEnhancerClass)) {
|
|
|
|
|
fillCustomHeadInfo(dataClass, headEnhancerClass, writerSheetBuilder);
|
|
|
|
|
}
|
|
|
|
|
else if (dataClass != null) {
|
|
|
|
|
writerSheetBuilder.head(dataClass);
|
|
|
|
|
if (sheet.excludes().length > 0) {
|
|
|
|
|
writerSheetBuilder.excludeColumnFiledNames(Arrays.asList(sheet.excludes()));
|
|
|
|
|
}
|
|
|
|
|
if (sheet.includes().length > 0) {
|
|
|
|
|
writerSheetBuilder.includeColumnFiledNames(Arrays.asList(sheet.includes()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sheetBuilder 增强
|
|
|
|
|
writerSheetBuilder = excelWriterBuilderEnhance.enhanceSheet(writerSheetBuilder, sheetNo, sheetName, dataClass,
|
|
|
|
|
template, headEnhancerClass);
|
|
|
|
|
writerSheetBuilder = excelWriterBuilderEnhance.enhanceSheet(writerSheetBuilder, sheetNo, sheet.sheetName(),
|
|
|
|
|
dataClass, template, headEnhancerClass);
|
|
|
|
|
|
|
|
|
|
return writerSheetBuilder.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void fillCustomHeadInfo(Class<?> dataClass, Class<? extends HeadGenerator> headEnhancerClass,
|
|
|
|
|
ExcelWriterSheetBuilder writerSheetBuilder) {
|
|
|
|
|
HeadGenerator headGenerator = this.applicationContext.getBean(headEnhancerClass);
|
|
|
|
|
Assert.notNull(headGenerator, "The header generated bean does not exist.");
|
|
|
|
|
HeadMeta head = headGenerator.head(dataClass);
|
|
|
|
|
writerSheetBuilder.head(head.getHead());
|
|
|
|
|
writerSheetBuilder.excludeColumnFiledNames(head.getIgnoreHeadFields());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否为Null Head Generator
|
|
|
|
|
* @param headGeneratorClass
|
|
|
|
|
* @return true 已指定 false 未指定(默认值)
|
|
|
|
|
*/
|
|
|
|
|
private boolean isNotInterface(Class<? extends HeadGenerator> headGeneratorClass) {
|
|
|
|
|
return !Modifier.isInterface(headGeneratorClass.getModifiers());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
|
|
this.applicationContext = applicationContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|