
cn.afterturn.easypoi.excel.export.base.ExportCommonService Maven / Gradle / Ivy
/**
* Copyright 2013-2015 JueYue ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.afterturn.easypoi.excel.export.base;
import cn.afterturn.easypoi.entity.BaseTypeConstants;
import cn.afterturn.easypoi.entity.ImageEntity;
import cn.afterturn.easypoi.entity.SpecialSymbolsEntity;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import cn.afterturn.easypoi.excel.annotation.ExcelEntity;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.entity.PoiBaseConstants;
import cn.afterturn.easypoi.exception.excel.ExcelExportException;
import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum;
import cn.afterturn.easypoi.handler.inter.ICommentHandler;
import cn.afterturn.easypoi.handler.inter.IExcelDataHandler;
import cn.afterturn.easypoi.handler.inter.IExcelDictHandler;
import cn.afterturn.easypoi.handler.inter.II18nHandler;
import cn.afterturn.easypoi.util.PoiDataDesensitizationUtil;
import cn.afterturn.easypoi.util.PoiPublicUtil;
import cn.afterturn.easypoi.util.PoiReflectorUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.*;
/**
* 导出基础处理,不涉及POI,只涉及对象,保证复用性
*
* @author JueYue 2014年8月9日 下午11:01:32
*/
@SuppressWarnings("rawtypes")
public class ExportCommonService {
protected static final Logger LOGGER = LoggerFactory.getLogger(ExportCommonService.class);
protected IExcelDataHandler dataHandler;
protected IExcelDictHandler dictHandler;
protected II18nHandler i18nHandler;
protected ICommentHandler commentHandler;
protected List needHandlerList;
/**
* 创建导出实体对象
*/
private ExcelExportEntity createExcelExportEntity(Field field, String targetId,
Class> pojoClass,
List getMethods, ExcelEntity excelGroup) throws Exception {
Excel excel = field.getAnnotation(Excel.class);
ExcelExportEntity excelEntity = new ExcelExportEntity();
excelEntity.setType(excel.type());
getExcelField(targetId, field, excelEntity, excel, pojoClass, excelGroup);
if (getMethods != null) {
List newMethods = new ArrayList();
newMethods.addAll(getMethods);
newMethods.add(excelEntity.getMethod());
excelEntity.setMethods(newMethods);
}
return excelEntity;
}
private Object dateFormatValue(Object value, ExcelExportEntity entity) throws Exception {
Date temp = null;
if (value instanceof String && StringUtils.isNoneEmpty(value.toString())) {
SimpleDateFormat format = new SimpleDateFormat(entity.getDatabaseFormat());
temp = format.parse(value.toString());
} else if (value instanceof Date) {
temp = (Date) value;
} else if (value instanceof Instant) {
Instant instant = (Instant) value;
temp = Date.from(instant);
} else if (value instanceof LocalDate) {
LocalDate localDate = (LocalDate) value;
temp = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
} else if (value instanceof LocalDateTime) {
LocalDateTime localDateTime = (LocalDateTime) value;
temp = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
} else if (value instanceof ZonedDateTime) {
ZonedDateTime zonedDateTime = (ZonedDateTime) value;
temp = Date.from(zonedDateTime.toInstant());
} else if (value instanceof OffsetDateTime) {
OffsetDateTime offsetDateTime = (OffsetDateTime) value;
temp = Date.from(offsetDateTime.toInstant());
} else if (value instanceof java.sql.Date) {
temp = new Date(((java.sql.Date) value).getTime());
} else if (value instanceof java.sql.Time) {
temp = new Date(((java.sql.Time) value).getTime());
} else if (value instanceof java.sql.Timestamp) {
temp = new Date(((java.sql.Timestamp) value).getTime());
}
if (temp != null) {
SimpleDateFormat format = new SimpleDateFormat(entity.getFormat());
if (StringUtils.isNotEmpty(entity.getTimezone())) {
format.setTimeZone(TimeZone.getTimeZone(entity.getTimezone()));
}
value = format.format(temp);
}
return value;
}
private Object numFormatValue(Object value, ExcelExportEntity entity) {
if (value == null) {
return null;
}
if (!NumberUtils.isNumber(value.toString())) {
LOGGER.error("data want num format ,but is not num, value is:" + value);
return null;
}
Double d = Double.parseDouble(value.toString());
DecimalFormat df = new DecimalFormat(entity.getNumFormat());
return df.format(d);
}
/**
* 获取需要导出的全部字段
*
* @param targetId 目标ID
*/
public void getAllExcelField(String[] exclusions, String targetId, Field[] fields,
List excelParams, Class> pojoClass,
List getMethods, ExcelEntity excelGroup) throws Exception {
List exclusionsList = exclusions != null ? Arrays.asList(exclusions) : null;
ExcelExportEntity excelEntity;
// 遍历整个filed
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
// 先判断是不是collection,在判断是不是java自带对象,之后就是我们自己的对象了
if (PoiPublicUtil.isNotUserExcelUserThis(exclusionsList, field, targetId)) {
continue;
}
// 首先判断Excel 可能一下特殊数据用户回自定义处理
if (field.getAnnotation(Excel.class) != null) {
Excel excel = field.getAnnotation(Excel.class);
String name = PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null);
if (StringUtils.isNotBlank(name)) {
excelParams.add(createExcelExportEntity(field, targetId, pojoClass, getMethods, excelGroup));
}
} else if (PoiPublicUtil.isCollection(field.getType())) {
ExcelCollection excel = field.getAnnotation(ExcelCollection.class);
ParameterizedType pt = (ParameterizedType) field.getGenericType();
Class> clz = (Class>) pt.getActualTypeArguments()[0];
List list = new ArrayList();
getAllExcelField(exclusions,
StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId,
PoiPublicUtil.getClassFields(clz), list, clz, null, null);
excelEntity = new ExcelExportEntity();
excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null));
if (i18nHandler != null) {
excelEntity.setName(i18nHandler.getLocaleName(excelEntity.getName()));
}
excelEntity.setOrderNum(Integer
.valueOf(PoiPublicUtil.getValueByTargetId(excel.orderNum(), targetId, "0")));
excelEntity
.setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName()));
excelEntity.setList(list);
excelParams.add(excelEntity);
} else {
List newMethods = new ArrayList();
if (getMethods != null) {
newMethods.addAll(getMethods);
}
newMethods.add(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName()));
ExcelEntity excel = field.getAnnotation(ExcelEntity.class);
if (excel.show() && StringUtils.isEmpty(excel.name())) {
throw new ExcelExportException("if use ExcelEntity ,name mus has value ,data: " + ReflectionToStringBuilder.toString(excel), ExcelExportEnum.PARAMETER_ERROR);
}
getAllExcelField(exclusions,
StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId,
PoiPublicUtil.getClassFields(field.getType()), excelParams, field.getType(),
newMethods, excel.show() ? excel : null);
}
}
}
/**
* 获取填如这个cell的值,提供一些附加功能
*/
@SuppressWarnings("unchecked")
public Object getCellValue(ExcelExportEntity entity, Object obj) throws Exception {
Object value;
if (obj instanceof Map) {
value = ((Map, ?>) obj).get(entity.getKey());
} else {
// 考虑直接用对象导出只能每次获取值的办法
if (entity.getMethods() == null && entity.getMethod() == null) {
value = PoiPublicUtil.getParamsValue(entity.getKey().toString(), obj);
} else {
value = entity.getMethods() != null ? getFieldBySomeMethod(entity.getMethods(), obj,entity.getMethodsParams())
: getFieldByMethod(entity.getMethod(),obj,entity.getMethodParams());
}
}
if (value instanceof ImageEntity) {
entity.setType(BaseTypeConstants.IMAGE_TYPE);
return value;
}
if (value instanceof SpecialSymbolsEntity) {
entity.setType(BaseTypeConstants.Symbol_TYPE);
return value;
}
if (StringUtils.isNotEmpty(entity.getFormat())) {
value = dateFormatValue(value, entity);
}
if (entity.getReplace() != null && entity.getReplace().length > 0) {
value = replaceValue(entity.getReplace(), String.valueOf(value));
}
if (StringUtils.isNotEmpty(entity.getNumFormat())) {
value = numFormatValue(value, entity);
}
if (StringUtils.isNotEmpty(entity.getDict()) && dictHandler != null) {
value = dictHandler.toName(entity.getDict(), obj, entity.getName(), value);
}
if (needHandlerList != null && needHandlerList.contains(entity.getName())) {
value = dataHandler.exportHandler(obj, entity.getName(), value);
}
if (StringUtils.isNotEmpty(entity.getSuffix()) && value != null) {
value = value + entity.getSuffix();
}
if (StringUtils.isNotEmpty(entity.getDesensitizationRule()) && value != null) {
value = PoiDataDesensitizationUtil.desensitization(entity.getDesensitizationRule(), value);
}
if (value != null && StringUtils.isNotEmpty(entity.getEnumExportField())) {
value = PoiReflectorUtil.fromCache(value.getClass()).getValue(value, entity.getEnumExportField());
}
return value == null ? "" : value.toString();
}
/**
* 获取集合的值
*/
public Collection> getListCellValue(ExcelExportEntity entity, Object obj) throws Exception {
Object value;
if (obj instanceof Map) {
value = ((Map, ?>) obj).get(entity.getKey());
} else {
value = (Collection>) entity.getMethod().invoke(obj, new Object[]{});
}
return (Collection>) value;
}
/**
* 注解到导出对象的转换
*/
private void getExcelField(String targetId, Field field, ExcelExportEntity excelEntity,
Excel excel, Class> pojoClass, ExcelEntity excelGroup) throws Exception {
excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null));
excelEntity.setKey(field.getName());
excelEntity.setWidth(excel.width());
excelEntity.setHeight(excel.height());
excelEntity.setNeedMerge(excel.needMerge());
excelEntity.setMergeVertical(excel.mergeVertical());
excelEntity.setMergeRely(excel.mergeRely());
excelEntity.setReplace(excel.replace());
excelEntity.setOrderNum(
Integer.valueOf(PoiPublicUtil.getValueByTargetId(excel.orderNum(), targetId, "0")));
excelEntity.setWrap(excel.isWrap());
excelEntity.setExportImageType(excel.imageType());
excelEntity.setSuffix(excel.suffix());
excelEntity.setDatabaseFormat(excel.databaseFormat());
excelEntity.setFormat(
StringUtils.isNotEmpty(excel.exportFormat()) ? excel.exportFormat() : excel.format());
excelEntity.setStatistics(excel.isStatistics());
excelEntity.setHyperlink(excel.isHyperlink());
excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName()));
excelEntity.setNumFormat(excel.numFormat());
excelEntity.setColumnHidden(excel.isColumnHidden());
excelEntity.setDict(excel.dict());
excelEntity.setEnumExportField(excel.enumExportField());
excelEntity.setTimezone(excel.timezone());
excelEntity.setAddressList(excel.addressList());
excelEntity.setDesensitizationRule(excel.desensitizationRule());
if (excelGroup != null) {
excelEntity.setGroupName(PoiPublicUtil.getValueByTargetId(excelGroup.name(), targetId, null));
} else {
excelEntity.setGroupName(excel.groupName());
}
if (i18nHandler != null) {
excelEntity.setName(i18nHandler.getLocaleName(excelEntity.getName()));
excelEntity.setGroupName(i18nHandler.getLocaleName(excelEntity.getGroupName()));
}
}
/**
* 多个反射获取值
*/
public Object getFieldBySomeMethod(List list, Object t,List> methodsParams) throws Exception {
for (int i = 0; i < list.size(); i++) {
if (t == null) {
t = "";
break;
}
Method m = list.get(i);
if(m.getParameterCount() == 0){
t = m.invoke(t);
}else{
t = m.invoke(t,methodsParams.get(i).toArray());
}
}
return t;
}
public Object getFieldByMethod(Method method, Object t,List