🔥 Removing code or files.移除基于easyexcel的common-excel实现
This commit is contained in:
1 parent
5b9218c083
commit
5fc406b014
16 files changed
-925
No files matched your search
@@ -62,11 +62,6 @@
|
||||
<artifactId>pigx-common-minio</artifactId>
|
||||
<version>${pigx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-common-excel</artifactId>
|
||||
<version>${pigx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-common-security</artifactId>
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>pigx-common</artifactId>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<version>3.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>pigx-common-excel</artifactId>
|
||||
<description>pigx Excel导入导出工具,基于easyexcel</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>easyexcel</artifactId>
|
||||
<version>${easyexcel.version}</version>
|
||||
</dependency>
|
||||
<!--core 工具类-->
|
||||
<dependency>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-common-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
-24
@@ -1,24 +0,0 @@
|
||||
package com.pig4cloud.common.excel.annotation;
|
||||
|
||||
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 单元格字体
|
||||
*
|
||||
* @author lishangbu
|
||||
* @date 2019-06-11
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
public @interface CellFontFormat {
|
||||
|
||||
String fontName() default "宋体";
|
||||
|
||||
short fontHeightInPoints() default 11;
|
||||
|
||||
IndexedColors fontColor() default IndexedColors.BLACK;
|
||||
|
||||
}
|
||||
-69
@@ -1,69 +0,0 @@
|
||||
package com.pig4cloud.common.excel.annotation;
|
||||
|
||||
import org.apache.poi.ss.usermodel.BorderStyle;
|
||||
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
||||
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||
import org.apache.poi.ss.usermodel.VerticalAlignment;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 单元格样式
|
||||
*
|
||||
* @author lishangbu
|
||||
* @date 2019-06-11
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
public @interface CellStyleFormat {
|
||||
|
||||
/**
|
||||
* 水平居中方式 默认居中
|
||||
* @see HorizontalAlignment
|
||||
*/
|
||||
HorizontalAlignment horizontalalignment() default HorizontalAlignment.CENTER;
|
||||
|
||||
/**
|
||||
*
|
||||
* 垂直居中方式 默认居中
|
||||
* @see VerticalAlignment
|
||||
*/
|
||||
VerticalAlignment verticalAlignment() default VerticalAlignment.CENTER;
|
||||
|
||||
|
||||
/**
|
||||
* 边框方式 默认无
|
||||
* @see BorderStyle
|
||||
*/
|
||||
BorderStyle borderStyle() default BorderStyle.NONE;
|
||||
|
||||
|
||||
/**
|
||||
* 边框颜色 默认白
|
||||
* @see IndexedColors
|
||||
*/
|
||||
IndexedColors borderColor() default IndexedColors.WHITE;
|
||||
|
||||
|
||||
/**
|
||||
* 字体设置
|
||||
* @see org.apache.poi.xssf.usermodel.XSSFFont
|
||||
* @see org.apache.poi.hssf.usermodel.HSSFFont
|
||||
*/
|
||||
CellFontFormat cellFont() default @CellFontFormat();
|
||||
|
||||
|
||||
/**
|
||||
* 背景颜色
|
||||
* @see IndexedColors
|
||||
*/
|
||||
IndexedColors fillBackgroundColor() default IndexedColors.WHITE;
|
||||
|
||||
|
||||
/**
|
||||
* 单元格是否换行
|
||||
*/
|
||||
boolean warpText() default false;
|
||||
|
||||
}
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
package com.pig4cloud.common.excel.exception;
|
||||
|
||||
/**
|
||||
* Excel异常类
|
||||
*
|
||||
* @author lishangbu
|
||||
* @date 2019-06-11
|
||||
*/
|
||||
public class ExcelException extends RuntimeException {
|
||||
/**
|
||||
* Constructs a new exception with the specified detail message. The
|
||||
* cause is not initialized, and may subsequently be initialized by
|
||||
* a call to {@link #initCause}.
|
||||
*
|
||||
* @param message the detail message. The detail message is saved for
|
||||
* later retrieval by the {@link #getMessage()} method.
|
||||
*/
|
||||
public ExcelException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
-49
@@ -1,49 +0,0 @@
|
||||
package com.pig4cloud.common.excel.factory;
|
||||
|
||||
import com.alibaba.excel.ExcelWriter;
|
||||
import com.alibaba.excel.metadata.BaseRowModel;
|
||||
import com.alibaba.excel.metadata.Sheet;
|
||||
import com.alibaba.excel.support.ExcelTypeEnum;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* excel工厂
|
||||
*
|
||||
* @author lishangbu
|
||||
* @date 2019-06-11
|
||||
*/
|
||||
public class ExcelWriterFactory extends ExcelWriter {
|
||||
private OutputStream outputStream;
|
||||
private int sheetNo = 1;
|
||||
|
||||
public ExcelWriterFactory(OutputStream outputStream, ExcelTypeEnum typeEnum) {
|
||||
super(outputStream, typeEnum);
|
||||
this.outputStream = outputStream;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public ExcelWriterFactory write(List<? extends BaseRowModel> list, String sheetName,
|
||||
BaseRowModel object) {
|
||||
this.sheetNo++;
|
||||
Sheet sheet = new Sheet(sheetNo, 0, object.getClass());
|
||||
sheet.setSheetName(sheetName);
|
||||
this.write(list, sheet);
|
||||
outputStream.flush();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public void finish() {
|
||||
super.finish();
|
||||
outputStream.flush();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public void close() {
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
-116
@@ -1,116 +0,0 @@
|
||||
package com.pig4cloud.common.excel.handler;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import com.alibaba.excel.event.WriteHandler;
|
||||
import com.alibaba.excel.metadata.BaseRowModel;
|
||||
import com.pig4cloud.common.excel.annotation.CellStyleFormat;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFFont;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* excel写入预处理器,支持生成Excel2007版本样式
|
||||
* 参考 https://github.com/alibaba/easyexcel/issues/205
|
||||
*
|
||||
* @author lishangbu
|
||||
* @date 2019-06-12
|
||||
*/
|
||||
public class PreWriterHandler<T extends Class<? extends BaseRowModel>> implements WriteHandler {
|
||||
|
||||
private T rowModel;
|
||||
|
||||
private Map<Integer, CellStyleFormat> colStyleFormat = new HashMap<>();
|
||||
|
||||
private Map<Integer, CellStyle> colStyle = new HashMap<>();
|
||||
|
||||
private Map<String, String> infos = new HashMap<>();
|
||||
|
||||
public static final String TITLE_NUM = "title_num";
|
||||
|
||||
public PreWriterHandler(T rowModel) {
|
||||
this.rowModel = rowModel;
|
||||
init(rowModel);
|
||||
}
|
||||
|
||||
private void init(T rowModel) {
|
||||
Field[] declaredFields = rowModel.getDeclaredFields();
|
||||
//查找有CellStyle注解和ExcelProperty注解的Field加入cellStyle
|
||||
int titleNum = 0;
|
||||
for (Field field : declaredFields) {
|
||||
CellStyleFormat cellStyle = field.getAnnotation(CellStyleFormat.class);
|
||||
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
|
||||
if (excelProperty != null) {
|
||||
titleNum =Math.max(excelProperty.value().length, titleNum);
|
||||
}
|
||||
if (cellStyle == null || excelProperty == null) {
|
||||
continue;
|
||||
}
|
||||
colStyleFormat.put(excelProperty.index(), cellStyle);
|
||||
|
||||
}
|
||||
infos.put(TITLE_NUM, String.valueOf(titleNum));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sheet(int sheetNo, Sheet sheet) {
|
||||
// 只初始化一次
|
||||
if (sheetNo == 1) {
|
||||
colStyle = colStyleFormat.entrySet().stream()
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, x -> {
|
||||
CellStyle cellStyle = sheet.getWorkbook().createCellStyle();
|
||||
CellStyleFormat value = x.getValue();
|
||||
//居中方式
|
||||
cellStyle.setVerticalAlignment(value.verticalAlignment());
|
||||
cellStyle.setAlignment(value.horizontalalignment());
|
||||
// 设置边框
|
||||
cellStyle.setBorderBottom(value.borderStyle());
|
||||
cellStyle.setBorderLeft(value.borderStyle());
|
||||
cellStyle.setBorderRight(value.borderStyle());
|
||||
cellStyle.setBorderTop(value.borderStyle());
|
||||
cellStyle.setBottomBorderColor(value.borderColor().getIndex());
|
||||
cellStyle.setLeftBorderColor(value.borderColor().getIndex());
|
||||
cellStyle.setRightBorderColor(value.borderColor().getIndex());
|
||||
cellStyle.setTopBorderColor(value.borderColor().getIndex());
|
||||
// 设置字体
|
||||
XSSFFont font = (XSSFFont) sheet.getWorkbook().createFont();
|
||||
font.setFontName(value.cellFont().fontName());
|
||||
font.setColor(value.cellFont().fontColor().getIndex());
|
||||
font.setFontHeightInPoints(value.cellFont().fontHeightInPoints());
|
||||
cellStyle.setFont(font);
|
||||
// 设置单元格是否换行
|
||||
cellStyle.setWrapText(value.warpText());
|
||||
// 背景颜色
|
||||
cellStyle.setFillBackgroundColor(value.fillBackgroundColor().getIndex());
|
||||
return cellStyle;
|
||||
},
|
||||
(u, v) -> v));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void row(int rowNum, Row row) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cell(int cellNum, Cell cell) {
|
||||
CellStyle cellStyle = colStyle.get(cellNum);
|
||||
// 表头不操作 行序号是从0开始的
|
||||
if (cell.getRowIndex() <= Integer.valueOf(infos.get(TITLE_NUM))-1) {
|
||||
return;
|
||||
}
|
||||
// 没有不操作
|
||||
if (cellStyle == null) {
|
||||
return;
|
||||
}
|
||||
//设置样式
|
||||
cell.setCellStyle(cellStyle);
|
||||
}
|
||||
}
|
||||
-77
@@ -1,77 +0,0 @@
|
||||
package com.pig4cloud.common.excel.listener;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import com.alibaba.excel.context.AnalysisContext;
|
||||
import com.alibaba.excel.event.AnalysisEventListener;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Excel监听器
|
||||
* 可以根据需要,自定义处理获取到的数据
|
||||
*
|
||||
* @author lishangbu
|
||||
* @date 2019-06-11
|
||||
*/
|
||||
public class ExcelListener extends AnalysisEventListener {
|
||||
|
||||
/**
|
||||
* 自定义用于暂时存储data,可以通过实例获取该值
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
private List<Object> dataList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 通过 AnalysisContext 对象还可以获取当前 sheet,当前行等数据
|
||||
*/
|
||||
@Override
|
||||
public void invoke(Object object, AnalysisContext context) {
|
||||
// 数据存储到list,供批量处理,或后续自己业务逻辑处理。
|
||||
if (!checkObjAllFieldsIsNull(object)) {
|
||||
dataList.add(object);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAfterAllAnalysed(AnalysisContext context) {
|
||||
//do something
|
||||
}
|
||||
|
||||
private static final String SERIAL_VERSION_UID = "serialVersionUID";
|
||||
|
||||
/**
|
||||
* 判断对象中属性值是否全为空
|
||||
*
|
||||
* @param object
|
||||
* @return true为空false不为空
|
||||
*/
|
||||
private static boolean checkObjAllFieldsIsNull(Object object) {
|
||||
if (ObjectUtil.isNull(object)) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
for (Field f : object.getClass().getDeclaredFields()) {
|
||||
f.setAccessible(true);
|
||||
//只校验带ExcelProperty注解的属性
|
||||
ExcelProperty property = f.getAnnotation(ExcelProperty.class);
|
||||
if (ObjectUtil.isNull(property) || SERIAL_VERSION_UID.equals(f.getName())) {
|
||||
continue;
|
||||
}
|
||||
if (ObjectUtil.isNotNull(f.get(object)) && StrUtil.isNotBlank(f.get(object).toString())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
//do something
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
-171
@@ -1,171 +0,0 @@
|
||||
package com.pig4cloud.common.excel.util;
|
||||
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import org.springframework.beans.*;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.core.convert.support.GenericConversionService;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* bean拷贝
|
||||
*
|
||||
* @author lishangbu
|
||||
* @date 2019-06-11
|
||||
*/
|
||||
public class BeanCopyUtils {
|
||||
|
||||
private final class DateToStringConverter implements Converter<LocalDateTime, String> {
|
||||
@Override
|
||||
public String convert(LocalDateTime source) {
|
||||
return source.format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));
|
||||
}
|
||||
}
|
||||
private static Object convertForProperty(Wrapper wrapper, Object object, Object value, String propertyName)
|
||||
throws TypeMismatchException {
|
||||
Object result;
|
||||
if (wrapper == null) {
|
||||
result = null;
|
||||
} else {
|
||||
wrapper.setWrappedInstance(object);
|
||||
result = wrapper.getBeanWrapper().convertForProperty(value, propertyName);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Object copyProperties(Object source, Object target) throws BeansException {
|
||||
Wrapper wrapper = new BeanCopyUtils().new Wrapper(source);
|
||||
copyProperties(wrapper, source, target);
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the property values of the given source bean into the target bean.
|
||||
* <p>
|
||||
* Note: The source and target classes do not have to match or even be derived from each other, as long as the properties match. Any bean
|
||||
* properties that the source bean exposes but the target bean does not will silently be ignored.
|
||||
* <p>
|
||||
* This is just a convenience method. For more complex transfer needs, consider using a full BeanWrapper.
|
||||
*
|
||||
* @param source
|
||||
* the source bean
|
||||
* @param target
|
||||
* the target bean
|
||||
* @throws BeansException
|
||||
* if the copying failed
|
||||
*/
|
||||
private static void copyProperties(Wrapper wrapper, Object source, Object target) throws BeansException {
|
||||
Assert.notNull(source, "Source must not be null");
|
||||
Assert.notNull(target, "Target must not be null");
|
||||
|
||||
Class<?> actualEditable = target.getClass();
|
||||
PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(actualEditable);
|
||||
|
||||
for (PropertyDescriptor targetPd : targetPds) {
|
||||
if (targetPd.getWriteMethod() != null) {
|
||||
PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), targetPd.getName());
|
||||
if ( sourcePd.getReadMethod() != null) {
|
||||
try {
|
||||
Method readMethod = sourcePd.getReadMethod();
|
||||
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
|
||||
readMethod.setAccessible(true);
|
||||
}
|
||||
Object value = readMethod.invoke(source);
|
||||
// 判断是否类型不一致
|
||||
if (value != null && !(targetPd.getPropertyType().isInstance(value))) {
|
||||
// 数据转型
|
||||
value = convertForProperty(wrapper, target, value, targetPd.getName());
|
||||
}
|
||||
Method writeMethod = targetPd.getWriteMethod();
|
||||
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
|
||||
writeMethod.setAccessible(true);
|
||||
}
|
||||
writeMethod.invoke(target, value);
|
||||
} catch (Exception ex) {
|
||||
throw new FatalBeanException("Could not copy properties from source to target", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private final class Wrapper {
|
||||
|
||||
private GenericConversionService conversion;
|
||||
private BeanWrapperImpl bean;
|
||||
|
||||
private Wrapper(Object object) {
|
||||
conversion = initDefaultConversionService();
|
||||
bean = initDefaultBeanWrapper(conversion, object);
|
||||
}
|
||||
|
||||
private void setWrappedInstance(Object object) {
|
||||
bean.setWrappedInstance(object);
|
||||
}
|
||||
|
||||
private GenericConversionService initDefaultConversionService() {
|
||||
GenericConversionService conversionService = new DefaultConversionService();
|
||||
conversionService.addConverter(new DateToStringConverter());
|
||||
return conversionService;
|
||||
}
|
||||
|
||||
private BeanWrapperImpl initDefaultBeanWrapper(@SuppressWarnings("hiding") ConversionService conversion,
|
||||
Object object) {
|
||||
BeanWrapperImpl beanWrapper = new BeanWrapperImpl(object);
|
||||
beanWrapper.setConversionService(conversion);
|
||||
return beanWrapper;
|
||||
}
|
||||
|
||||
private BeanWrapperImpl getBeanWrapper() {
|
||||
return bean;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制源对象到目的对象
|
||||
*/
|
||||
private static void convert(Object source, Object target) {
|
||||
copyProperties(source, target);
|
||||
}
|
||||
|
||||
public static <T> List<T> convert(List<?> sources, Class<T> targetClass) {
|
||||
List<?> sourcesObj = sources;
|
||||
if (sourcesObj == null) {
|
||||
sourcesObj = Collections.emptyList();
|
||||
}
|
||||
List<T> targets = new ArrayList<>(sourcesObj.size());
|
||||
BeanCopyUtils.convert(sourcesObj, targets, targetClass);
|
||||
return targets;
|
||||
}
|
||||
|
||||
private static <T> void convert(List<?> sources, List<T> targets, Class<T> targetClass) {
|
||||
if (targets == null) {
|
||||
return;
|
||||
}
|
||||
targets.clear();
|
||||
if (sources == null) {
|
||||
return;
|
||||
}
|
||||
for (Object obj : sources) {
|
||||
try {
|
||||
T target = targetClass.newInstance();
|
||||
targets.add(target);
|
||||
convert(obj, target);
|
||||
} catch (Exception e) {
|
||||
//do something
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
-235
@@ -1,235 +0,0 @@
|
||||
package com.pig4cloud.common.excel.util;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.excel.EasyExcelFactory;
|
||||
import com.alibaba.excel.ExcelReader;
|
||||
import com.alibaba.excel.ExcelWriter;
|
||||
import com.alibaba.excel.metadata.BaseRowModel;
|
||||
import com.alibaba.excel.metadata.Sheet;
|
||||
import com.alibaba.excel.support.ExcelTypeEnum;
|
||||
import com.pig4cloud.common.excel.exception.ExcelException;
|
||||
import com.pig4cloud.common.excel.factory.ExcelWriterFactory;
|
||||
import com.pig4cloud.common.excel.handler.PreWriterHandler;
|
||||
import com.pig4cloud.common.excel.listener.ExcelListener;
|
||||
import lombok.Cleanup;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Excel工具类
|
||||
*
|
||||
* @author lishangbu
|
||||
* @date 2019-06-11
|
||||
*/
|
||||
@Slf4j
|
||||
@UtilityClass
|
||||
public class ExcelUtils {
|
||||
|
||||
private final String DEFAULT_EXCEL_SHEET_NAME = "Sheet1";
|
||||
|
||||
/**
|
||||
* 读取 Excel(多个 sheet)
|
||||
* 将多sheet合并成一个list数据集,通过自定义ExcelReader继承AnalysisEventListener
|
||||
* 重写invoke doAfterAllAnalysed方法
|
||||
* getExtendsBeanList 主要是做Bean的属性拷贝 ,可以通过ExcelReader中添加的数据集直接获取
|
||||
*
|
||||
* @param excel 文件
|
||||
* @param rowModel 实体类映射,继承 BaseRowModel 类
|
||||
* @return Excel 数据 list
|
||||
*/
|
||||
public <T extends BaseRowModel> List<T> readExcel(MultipartFile excel, Class<T> rowModel) {
|
||||
ExcelListener excelListener = new ExcelListener();
|
||||
ExcelReader reader = getReader(excel, excelListener);
|
||||
if (reader == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
for (Sheet sheet : reader.getSheets()) {
|
||||
sheet.setClazz(rowModel);
|
||||
reader.read(sheet);
|
||||
}
|
||||
return getExtendsBeanList(excelListener.getDataList(), rowModel);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取某个 sheet 的 Excel
|
||||
*
|
||||
* @param excel 文件
|
||||
* @param rowModel 实体类映射,继承 BaseRowModel 类
|
||||
* @param sheetNo sheet 的序号 从1开始
|
||||
* @return Excel 数据 list
|
||||
*/
|
||||
public <T extends BaseRowModel> List<T> readExcel(MultipartFile excel, Class<T> rowModel, int sheetNo) {
|
||||
return readExcel(excel, rowModel, sheetNo, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取某个 sheet 的 Excel
|
||||
*
|
||||
* @param excel 文件
|
||||
* @param rowModel 实体类映射,继承 BaseRowModel 类
|
||||
* @param sheetNo sheet 的序号 从1开始
|
||||
* @param headLineNum 表头行数,默认为1
|
||||
* @return Excel 数据 list
|
||||
*/
|
||||
public <T extends BaseRowModel> List<T> readExcel(MultipartFile excel, Class<T> rowModel, int sheetNo,
|
||||
int headLineNum) {
|
||||
ExcelListener excelListener = new ExcelListener();
|
||||
ExcelReader reader = getReader(excel, excelListener);
|
||||
if (reader == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
reader.read(new Sheet(sheetNo, headLineNum, rowModel));
|
||||
return getExtendsBeanList(excelListener.getDataList(), rowModel);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 导出 2007 Excel :一个 sheet,带表头,默认sheet名称
|
||||
*
|
||||
* @param response HttpServletResponse
|
||||
* @param list 数据 list,每个元素为一个 BaseRowModel
|
||||
* @param fileName 导出的文件名
|
||||
* @param classType 实体类映射,继承 BaseRowModel 类
|
||||
* @param <T>
|
||||
*/
|
||||
public <T extends BaseRowModel> void writeExcel(HttpServletResponse response, List<T> list,
|
||||
String fileName, Class<T> classType) {
|
||||
writeExcel(response, list, fileName, null, classType);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 导出 2007 Excel :一个 sheet,带表头
|
||||
*
|
||||
* @param response HttpServletResponse
|
||||
* @param list 数据 list,每个元素为一个 BaseRowModel
|
||||
* @param fileName 导出的文件名
|
||||
* @param sheetName 导入文件的 sheet 名
|
||||
* @param classType 实体类映射,继承 BaseRowModel 类
|
||||
* @param <T>
|
||||
*/
|
||||
public <T extends BaseRowModel> void writeExcel(HttpServletResponse response, List<T> list,
|
||||
String fileName, String sheetName,
|
||||
Class<T> classType) {
|
||||
writeExcel(response, list, fileName, sheetName, ExcelTypeEnum.XLSX, classType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出 Excel :一个 sheet,带表头
|
||||
* 自定义WriterHandler 可以定制行列数据进行灵活化操作
|
||||
*
|
||||
* @param response HttpServletResponse
|
||||
* @param list 数据 list,每个元素为一个 BaseRowModel
|
||||
* @param fileName 导出的文件名
|
||||
* @param sheetName 导入文件的 sheet 名
|
||||
* @param excelTypeEnum 导出Excel类型
|
||||
* @param classType 实体类映射,继承 BaseRowModel 类
|
||||
*/
|
||||
@SneakyThrows
|
||||
public <T extends BaseRowModel> void writeExcel(HttpServletResponse response, List<T> list,
|
||||
String fileName, String sheetName, ExcelTypeEnum excelTypeEnum,
|
||||
Class<T> classType) {
|
||||
if (StrUtil.isBlank(sheetName)) {
|
||||
sheetName = DEFAULT_EXCEL_SHEET_NAME;
|
||||
}
|
||||
response.reset();
|
||||
@Cleanup
|
||||
OutputStream outputStream = getOutputStream(fileName, response, excelTypeEnum);
|
||||
if (excelTypeEnum == ExcelTypeEnum.XLSX) {
|
||||
ExcelWriter writer = EasyExcelFactory.getWriterWithTempAndHandler(null, outputStream
|
||||
, excelTypeEnum, true, new PreWriterHandler<>(classType));
|
||||
Sheet sheet = new Sheet(1, 0, classType);
|
||||
sheet.setSheetName(sheetName);
|
||||
try {
|
||||
writer.write(list, sheet);
|
||||
} finally {
|
||||
writer.finish();
|
||||
}
|
||||
} else if (excelTypeEnum == ExcelTypeEnum.XLS) {
|
||||
// 其实也可以专门调03版的样式,或者直接套用
|
||||
ExcelWriterFactory writer = new ExcelWriterFactory(outputStream, excelTypeEnum);
|
||||
Sheet sheet = new Sheet(1, 0, classType);
|
||||
sheet.setSheetName(sheetName);
|
||||
try {
|
||||
writer.write(list, sheet);
|
||||
} finally {
|
||||
writer.finish();
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
outputStream.flush();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出 Excel :多个 sheet,带表头
|
||||
*
|
||||
* @param response HttpServletResponse
|
||||
* @param list 数据 list,每个元素为一个 BaseRowModel
|
||||
* @param fileName 导出的文件名
|
||||
* @param sheetName 导入文件的 sheet 名
|
||||
* @param object 映射实体类,Excel 模型
|
||||
*/
|
||||
public ExcelWriterFactory writeExcelWithSheets(HttpServletResponse response, List<? extends BaseRowModel> list,
|
||||
String fileName, String sheetName, BaseRowModel object, ExcelTypeEnum excelTypeEnum) {
|
||||
ExcelWriterFactory writer = new ExcelWriterFactory(getOutputStream(fileName, response, excelTypeEnum), excelTypeEnum);
|
||||
Sheet sheet = new Sheet(1, 0, object.getClass());
|
||||
sheet.setSheetName(sheetName);
|
||||
writer.write(list, sheet);
|
||||
return writer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出文件时为Writer生成OutputStream
|
||||
*/
|
||||
@SneakyThrows
|
||||
private OutputStream getOutputStream(String fileName, HttpServletResponse response, ExcelTypeEnum excelTypeEnum) {
|
||||
// 解决文件乱码问题
|
||||
String encodeFilename = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
|
||||
response.setContentType(MediaType.MULTIPART_FORM_DATA_VALUE);
|
||||
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||
response.setHeader("Content-disposition", "attachment;filename=" + encodeFilename + excelTypeEnum.getValue());
|
||||
return response.getOutputStream();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 ExcelReader
|
||||
*
|
||||
* @param excel 需要解析的 Excel 文件
|
||||
* @param excelListener new ExcelListener()
|
||||
*/
|
||||
@SneakyThrows
|
||||
private ExcelReader getReader(MultipartFile excel,
|
||||
ExcelListener excelListener) {
|
||||
String fileName = excel.getOriginalFilename();
|
||||
if (fileName == null) {
|
||||
throw new ExcelException("文件格式错误!");
|
||||
}
|
||||
if (!fileName.toLowerCase().endsWith(ExcelTypeEnum.XLS.getValue()) && !fileName.toLowerCase().endsWith(ExcelTypeEnum.XLSX.getValue())) {
|
||||
throw new ExcelException("文件格式错误!");
|
||||
}
|
||||
@Cleanup
|
||||
InputStream inputStream= excel.getInputStream();
|
||||
return new ExcelReader(inputStream, null, excelListener, false);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 利用BeanCopy转换list
|
||||
*/
|
||||
private <T extends BaseRowModel> List<T> getExtendsBeanList(List<?> list, Class<T> typeClazz) {
|
||||
return BeanCopyUtils.convert(list, typeClazz);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,6 @@
|
||||
<module>pigx-common-core</module>
|
||||
<module>pigx-common-data</module>
|
||||
<module>pigx-common-datasource</module>
|
||||
<module>pigx-common-excel</module>
|
||||
<module>pigx-common-gateway</module>
|
||||
<module>pigx-common-job</module>
|
||||
<module>pigx-common-log</module>
|
||||
|
||||
@@ -44,11 +44,5 @@
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-extension</artifactId>
|
||||
</dependency>
|
||||
<!--Excel导入导出-->
|
||||
<dependency>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-common-excel</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
-90
@@ -1,90 +0,0 @@
|
||||
package com.pig4cloud.pigx.admin.api.excel;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import com.alibaba.excel.metadata.BaseRowModel;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 公共参数Excel导出实体
|
||||
*
|
||||
* @author lishangbu
|
||||
* @date 2019-07-03
|
||||
*/
|
||||
@Data
|
||||
@ApiModel(value = "公共参数Excel导出实体")
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class SysPublicParamRowModel extends BaseRowModel {
|
||||
private static final long serialVersionUID = 1L;
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
@ExcelProperty(index = 0 ,value = "公共参数编号")
|
||||
@ApiModelProperty(value = "公共参数编号")
|
||||
private Long publicId;
|
||||
/**
|
||||
* 公共参数名称
|
||||
*/
|
||||
@ExcelProperty(index = 1 ,value = "公共参数名称")
|
||||
@ApiModelProperty(value = "公共参数名称", required = true, example = "公共参数名称")
|
||||
private String publicName;
|
||||
/**
|
||||
* 公共参数地址值,英文大写+下划线
|
||||
*/
|
||||
@ExcelProperty(index = 2,value = "公共参数地址")
|
||||
@ApiModelProperty(value = "键[英文大写+下划线]", required = true, example = "PIGX_PUBLIC_KEY")
|
||||
private String publicKey;
|
||||
/**
|
||||
* 值
|
||||
*/
|
||||
@ExcelProperty(index = 3,value = "值")
|
||||
@ApiModelProperty(value = "值", required = true, example = "999")
|
||||
private String publicValue;
|
||||
/**
|
||||
* 状态(1有效;2无效;)
|
||||
*/
|
||||
@ExcelProperty(index = 4,value = "状态")
|
||||
@ApiModelProperty(value = "标识[1有效;2无效]", example = "1")
|
||||
private String status;
|
||||
/**
|
||||
* 删除状态(0:正常 1:已删除)
|
||||
*/
|
||||
@ExcelProperty(index = 5,value = "删除状态")
|
||||
@ApiModelProperty(value = "状态[0-正常,1-删除]", example = "0")
|
||||
private String delFlag;
|
||||
/**
|
||||
* 公共参数编码
|
||||
*/
|
||||
@ExcelProperty(index = 6,value = "公共参数编码")
|
||||
@ApiModelProperty(value = "编码", example = "^(PIG|PIGX)$")
|
||||
private String validateCode;
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@ExcelProperty(index = 7,value = "创建时间")
|
||||
@ApiModelProperty(value = "创建时间", example = "2019-03-21 12:28:48")
|
||||
private Date createTime;
|
||||
/**
|
||||
* 修改时间
|
||||
*/
|
||||
@ExcelProperty(index = 8,value = "修改时间")
|
||||
@ApiModelProperty(value = "修改时间", example = "2019-03-21 12:28:48")
|
||||
private Date updateTime;
|
||||
/**
|
||||
* 是否是系统内置
|
||||
*/
|
||||
@ExcelProperty(index = 9,value = "系统内置标识")
|
||||
@ApiModelProperty(value = "是否是系统内置")
|
||||
private String system;
|
||||
/**
|
||||
* 配置类型:0-默认;1-检索;2-原文;3-报表;4-安全;5-文档;6-消息;9-其他
|
||||
*/
|
||||
@ExcelProperty(index = 10,value = "配置类型")
|
||||
@ApiModelProperty(value = "类型[1-检索;2-原文...]", example = "1")
|
||||
private String publicType;
|
||||
}
|
||||
@@ -57,11 +57,6 @@
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-common-minio</artifactId>
|
||||
</dependency>
|
||||
<!--Excel导入导出-->
|
||||
<dependency>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-common-excel</artifactId>
|
||||
</dependency>
|
||||
<!--注册中心客户端-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
|
||||
-27
@@ -19,9 +19,7 @@ package com.pig4cloud.pigx.admin.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.pig4cloud.common.excel.util.ExcelUtils;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysPublicParam;
|
||||
import com.pig4cloud.pigx.admin.api.excel.SysPublicParamRowModel;
|
||||
import com.pig4cloud.pigx.admin.service.SysPublicParamService;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import com.pig4cloud.pigx.common.log.annotation.SysLog;
|
||||
@@ -29,14 +27,9 @@ import com.pig4cloud.pigx.common.security.annotation.Inner;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* 公共参数
|
||||
@@ -133,24 +126,4 @@ public class SysPublicParamController {
|
||||
return sysPublicParamService.removeParam(publicId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出Excel
|
||||
*
|
||||
* @param response
|
||||
* @param sysPublicParam
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation("导出公共参数信息Excel")
|
||||
@GetMapping("/export")
|
||||
public void exportCustomerData(HttpServletResponse response, SysPublicParam sysPublicParam) {
|
||||
List<SysPublicParamRowModel> resultList = new ArrayList<>();
|
||||
List<SysPublicParam> sysPublicParamList = sysPublicParamService.list(Wrappers.query(sysPublicParam));
|
||||
sysPublicParamList.forEach(data -> {
|
||||
SysPublicParamRowModel publicParamRowModel = new SysPublicParamRowModel();
|
||||
BeanUtils.copyProperties(data, publicParamRowModel);
|
||||
resultList.add(publicParamRowModel);
|
||||
});
|
||||
ExcelUtils.writeExcel(response, resultList,"公共参数导出",SysPublicParamRowModel.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -51,7 +51,6 @@
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<spring-boot-admin.version>2.1.5</spring-boot-admin.version>
|
||||
<easyexcel.version>1.1.2-beta5</easyexcel.version>
|
||||
<hutool.version>4.5.15</hutool.version>
|
||||
<kaptcha.version>0.0.9</kaptcha.version>
|
||||
<swagger.fox.version>2.9.2</swagger.fox.version>
|
||||
|
||||
Reference in new issue
Block a user