com.zhongweixian.excel.imports.base.ImportBaseService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of poi-api Show documentation
Show all versions of poi-api Show documentation
poi-api project for Spring Boot
The newest version!
package com.zhongweixian.excel.imports.base;
import com.zhongweixian.excel.annotation.Excel;
import com.zhongweixian.excel.annotation.ExcelCollection;
import com.zhongweixian.excel.annotation.ExcelEntity;
import com.zhongweixian.excel.entity.ImportParams;
import com.zhongweixian.excel.entity.params.ExcelCollectionParams;
import com.zhongweixian.excel.entity.params.ExcelImportEntity;
import com.zhongweixian.excel.exception.ExcelImportEnum;
import com.zhongweixian.excel.exception.ExcelImportException;
import com.zhongweixian.excel.util.PoiPublicUtil;
import com.zhongweixian.excel.util.PoiReflectorUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @author : caoliang
* @date : 2017/11/13 下午6:51
*/
public class ImportBaseService {
/**
* 把这个注解解析放到类型对象中
*
* @param targetId
* @param field
* @param excelEntity
* @param pojoClass
* @param getMethods
* @param temp
* @throws Exception
*/
public void addEntityToMap(String targetId, Field field, ExcelImportEntity excelEntity,
Class> pojoClass, List getMethods,
Map temp, ExcelEntity excelEntityAnn) throws Exception {
Excel excel = field.getAnnotation(Excel.class);
excelEntity = new ExcelImportEntity();
excelEntity.setType(excel.type());
excelEntity.setSaveUrl(excel.savePath());
excelEntity.setSaveType(excel.imageType());
excelEntity.setReplace(excel.replace());
excelEntity.setDatabaseFormat(excel.databaseFormat());
excelEntity.setSuffix(excel.suffix());
excelEntity.setImportField(Boolean
.valueOf(PoiPublicUtil.getValueByTargetId(excel.isImportField(), targetId, "false")));
getExcelField(targetId, field, excelEntity, excel, pojoClass, excelEntityAnn);
if (getMethods != null) {
List newMethods = new ArrayList();
newMethods.addAll(getMethods);
newMethods.add(excelEntity.getMethod());
excelEntity.setMethods(newMethods);
}
temp.put(excelEntity.getName(), excelEntity);
}
/**
* 获取需要导出的全部字段
*
* @param targetId
* @param fields
* @param excelParams
* @param excelCollection
* @param pojoClass
* @param getMethods
* @throws Exception
*/
public void getAllExcelField(String targetId, Field[] fields,
Map excelParams,
List excelCollection, Class> pojoClass,
List getMethods, ExcelEntity excelEntityAnn) throws Exception {
ExcelImportEntity excelEntity = null;
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
if (PoiPublicUtil.isNotUserExcelUserThis(null, field, targetId)) {
continue;
}
if (PoiPublicUtil.isCollection(field.getType())) {
// 集合对象设置属性
ExcelCollection excel = field.getAnnotation(ExcelCollection.class);
ExcelCollectionParams collection = new ExcelCollectionParams();
collection.setName(field.getName());
Map temp = new HashMap();
ParameterizedType pt = (ParameterizedType) field.getGenericType();
Class> clz = (Class>) pt.getActualTypeArguments()[0];
collection.setType(clz);
getExcelFieldList(StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId,
PoiPublicUtil.getClassFields(clz), clz, temp, null);
collection.setExcelParams(temp);
collection.setExcelName(PoiPublicUtil.getValueByTargetId(
field.getAnnotation(ExcelCollection.class).name(), targetId, null));
additionalCollectionName(collection);
excelCollection.add(collection);
} else if (PoiPublicUtil.isJavaClass(field)) {
addEntityToMap(targetId, field, excelEntity, pojoClass, getMethods, excelParams, excelEntityAnn);
} 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 ExcelImportException("if use ExcelEntity ,name mus has value ,data: " + ReflectionToStringBuilder.toString(excel), ExcelImportEnum.PARAMETER_ERROR);
}
getAllExcelField(StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId,
PoiPublicUtil.getClassFields(field.getType()), excelParams, excelCollection,
field.getType(), newMethods, excel);
}
}
}
/**
* 追加集合名称到前面
*
* @param collection
*/
private void additionalCollectionName(ExcelCollectionParams collection) {
Set keys = new HashSet();
keys.addAll(collection.getExcelParams().keySet());
for (String key : keys) {
collection.getExcelParams().put(collection.getExcelName() + "_" + key,
collection.getExcelParams().get(key));
collection.getExcelParams().remove(key);
}
}
public void getExcelField(String targetId, Field field, ExcelImportEntity excelEntity,
Excel excel, Class> pojoClass, ExcelEntity excelEntityAnn) throws Exception {
excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null));
if (StringUtils.isNoneEmpty(excel.groupName())) {
excelEntity.setName(excel.groupName() + "_" + excelEntity.getName());
}
if (excelEntityAnn != null && excelEntityAnn.show()) {
excelEntity.setName(excelEntityAnn.name() + "_" + excelEntity.getName());
}
String fieldname = field.getName();
excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getSetMethod(fieldname));
if (StringUtils.isNotEmpty(excel.importFormat())) {
excelEntity.setFormat(excel.importFormat());
} else {
excelEntity.setFormat(excel.format());
}
}
public void getExcelFieldList(String targetId, Field[] fields, Class> pojoClass,
Map temp,
List getMethods) throws Exception {
ExcelImportEntity excelEntity = null;
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
if (PoiPublicUtil.isNotUserExcelUserThis(null, field, targetId)) {
continue;
}
if (PoiPublicUtil.isJavaClass(field)) {
addEntityToMap(targetId, field, excelEntity, pojoClass, getMethods, temp, null);
} else {
List newMethods = new ArrayList();
if (getMethods != null) {
newMethods.addAll(getMethods);
}
ExcelEntity excel = field.getAnnotation(ExcelEntity.class);
newMethods.add(PoiReflectorUtil.fromCache(pojoClass).getSetMethod(field.getName()));
getExcelFieldList(StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId,
PoiPublicUtil.getClassFields(field.getType()), field.getType(), temp,
newMethods);
}
}
}
public Object getFieldBySomeMethod(List list, Object t) throws Exception {
Method m;
for (int i = 0; i < list.size() - 1; i++) {
m = list.get(i);
t = m.invoke(t, new Object[]{});
}
return t;
}
public void saveThisExcel(ImportParams params, Class> pojoClass, boolean isXSSFWorkbook,
Workbook book) throws Exception {
String path = PoiPublicUtil.getWebRootPath(getSaveExcelUrl(params, pojoClass));
File savefile = new File(path);
if (!savefile.exists()) {
savefile.mkdirs();
}
SimpleDateFormat format = new SimpleDateFormat("yyyMMddHHmmss");
FileOutputStream fos = new FileOutputStream(
path + "/" + format.format(new Date()) + "_" + Math.round(Math.random() * 100000)
+ (isXSSFWorkbook == true ? ".xlsx" : ".xls"));
book.write(fos);
IOUtils.closeQuietly(fos);
}
/**
* 获取保存的Excel 的真实路径
*
* @param params
* @param pojoClass
* @return
* @throws Exception
*/
public String getSaveExcelUrl(ImportParams params, Class> pojoClass) throws Exception {
String url = "";
if ("upload/excelUpload".equals(params.getSaveUrl())) {
url = pojoClass.getName().split("\\.")[pojoClass.getName().split("\\.").length - 1];
return params.getSaveUrl() + "/" + url;
}
return params.getSaveUrl();
}
/**
* 多个get 最后再set
*
* @param setMethods
* @param object
*/
public void setFieldBySomeMethod(List setMethods, Object object,
Object value) throws Exception {
Object t = getFieldBySomeMethod(setMethods, object);
setMethods.get(setMethods.size() - 1).invoke(t, value);
}
/**
* @param entity
* @param object
* @param value
* @throws Exception
*/
public void setValues(ExcelImportEntity entity, Object object, Object value) throws Exception {
if (entity.getMethods() != null) {
setFieldBySomeMethod(entity.getMethods(), object, value);
} else {
entity.getMethod().invoke(object, value);
}
}
}