|
|
|
@ -1,14 +1,17 @@
|
|
|
|
|
package cn.estsh.i3plus.core.apiservice.util;
|
|
|
|
|
|
|
|
|
|
import cn.estsh.i3plus.core.api.iservice.busi.ISysConfigService;
|
|
|
|
|
import cn.estsh.i3plus.platform.common.tool.ReflexTool;
|
|
|
|
|
import cn.estsh.i3plus.platform.common.tool.StringTool;
|
|
|
|
|
import cn.estsh.i3plus.platform.common.tool.TimeTool;
|
|
|
|
|
import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
|
|
|
|
|
import cn.estsh.i3plus.pojo.base.annotation.AnnoOutputColumn;
|
|
|
|
|
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
|
|
|
|
|
import cn.estsh.i3plus.pojo.platform.bean.SysTool;
|
|
|
|
|
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
|
|
|
|
|
import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
|
|
|
|
|
import io.swagger.annotations.ApiParam;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
|
@ -22,10 +25,12 @@ import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import javax.persistence.EntityManager;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.sql.Ref;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -49,6 +54,14 @@ public class ExcelUtil {
|
|
|
|
|
ExcelUtil.sysConfigService = sysConfigService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static EntityManager entityManager;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
public void setEntityManager(EntityManager entityManager) {
|
|
|
|
|
ExcelUtil.entityManager = entityManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 导出
|
|
|
|
|
*
|
|
|
|
@ -74,45 +87,61 @@ public class ExcelUtil {
|
|
|
|
|
HSSFRow tableHeader = sheet.createRow(0);
|
|
|
|
|
|
|
|
|
|
// 类数据
|
|
|
|
|
Field[] declaredFields = exportClass.getDeclaredFields();
|
|
|
|
|
Field[] fields = new Field[exportCol.length];
|
|
|
|
|
|
|
|
|
|
String colName;
|
|
|
|
|
|
|
|
|
|
// 获取字段中文名 优先使用 OutputColumn.name
|
|
|
|
|
for (int i = 0; i < exportCol.length; i++) {
|
|
|
|
|
for (Field field : declaredFields) {
|
|
|
|
|
if (field.getName().equals(exportCol[i])) {
|
|
|
|
|
fields[i] = field;
|
|
|
|
|
fields[i] = exportClass.getDeclaredField(exportCol[i]);
|
|
|
|
|
colName = null;
|
|
|
|
|
|
|
|
|
|
if (field.isAnnotationPresent(ApiParam.class)) {
|
|
|
|
|
ApiParam fieldAnno = field.getAnnotation(ApiParam.class);
|
|
|
|
|
tableHeader.createCell(i, CellType.STRING).setCellValue(fieldAnno.value());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (fields[i].isAnnotationPresent(AnnoOutputColumn.class)) {
|
|
|
|
|
colName = fields[i].getAnnotation(AnnoOutputColumn.class).name();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fields[i].isAnnotationPresent(ApiParam.class) && StringUtils.isBlank(colName)) {
|
|
|
|
|
colName = fields[i].getAnnotation(ApiParam.class).value();
|
|
|
|
|
}
|
|
|
|
|
tableHeader.createCell(i, CellType.STRING).setCellValue(colName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//加载数据至excel对象
|
|
|
|
|
HSSFRow hssfRow;
|
|
|
|
|
Method method;
|
|
|
|
|
AnnoOutputColumn outputColumn;
|
|
|
|
|
Object CellValue;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
|
hssfRow = sheet.createRow(i + 1);
|
|
|
|
|
for (int j = 0; j < fields.length; j++) {
|
|
|
|
|
method = exportClass.getDeclaredMethod("get" + StringTool.toUpperCaseFirstOne(fields[j].getName()));
|
|
|
|
|
hssfRow.createCell(j, CellType.STRING).setCellValue(
|
|
|
|
|
String.valueOf(method.invoke(data.get(i)))
|
|
|
|
|
);
|
|
|
|
|
CellValue = exportClass.getDeclaredMethod("get" + StringTool.toUpperCaseFirstOne(fields[j].getName())).invoke(data.get(i));
|
|
|
|
|
|
|
|
|
|
// 判断是否存在引用关系
|
|
|
|
|
if (fields[j].isAnnotationPresent(AnnoOutputColumn.class)) {
|
|
|
|
|
outputColumn = fields[j].getAnnotation(AnnoOutputColumn.class);
|
|
|
|
|
|
|
|
|
|
// 判断是否为枚举字段
|
|
|
|
|
if (outputColumn.refClass().isEnum()) {
|
|
|
|
|
method = outputColumn.refClass().getDeclaredMethod(
|
|
|
|
|
outputColumn.refForeignKey() + "Of" + StringTool.toUpperCaseFirstOne(outputColumn.value()),
|
|
|
|
|
outputColumn.refClass().getDeclaredMethod("get"
|
|
|
|
|
+ StringTool.toUpperCaseFirstOne(outputColumn.refForeignKey())).getReturnType());
|
|
|
|
|
CellValue = method.invoke(data.get(i), CellValue);
|
|
|
|
|
} else {
|
|
|
|
|
selectByProperty(outputColumn.refClass(), outputColumn.value(), outputColumn.refForeignKey(), CellValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hssfRow.createCell(j, CellType.STRING).setCellValue(String.valueOf(CellValue));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
workbook.write(fos);
|
|
|
|
|
fos.flush();
|
|
|
|
|
return file2Byte(excelFile);
|
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
|
LOGGER.error("属性get方法不存在!", e);
|
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
|
LOGGER.error("Excel 导出异常!", e);
|
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
|
LOGGER.error("Excel 导出异常!", e);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.IO_EXCEPTION_FILE.getCode())
|
|
|
|
@ -127,7 +156,6 @@ public class ExcelUtil {
|
|
|
|
|
LOGGER.error("IOException!", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -196,9 +224,9 @@ public class ExcelUtil {
|
|
|
|
|
obj = importClass.newInstance();
|
|
|
|
|
for (int j = 0; j < fields.length; j++) {
|
|
|
|
|
row.getCell(j).setCellType(CellType.STRING);
|
|
|
|
|
if("".equals(row.getCell(j).getStringCellValue())){
|
|
|
|
|
if ("".equals(row.getCell(j).getStringCellValue())) {
|
|
|
|
|
cellValue = null;
|
|
|
|
|
}else if (fields[j].getType() == String.class) {
|
|
|
|
|
} else if (fields[j].getType() == String.class) {
|
|
|
|
|
cellValue = row.getCell(j).getStringCellValue();
|
|
|
|
|
} else if (fields[j].getType() == Integer.class) {
|
|
|
|
|
cellValue = Integer.parseInt(row.getCell(j).getStringCellValue());
|
|
|
|
@ -333,6 +361,21 @@ public class ExcelUtil {
|
|
|
|
|
return colName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param persistentClass
|
|
|
|
|
* @param colName
|
|
|
|
|
* @param propertyName
|
|
|
|
|
* @param value
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static Object selectByProperty(Class persistentClass, String colName, String propertyName, Object value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
|
|
|
|
String queryString = "select model." + colName + " from " + persistentClass.getSimpleName()
|
|
|
|
|
+ " as model where model." + propertyName + "= :" + propertyName;
|
|
|
|
|
Object result = entityManager.createQuery(queryString).setParameter(propertyName, value).getSingleResult();
|
|
|
|
|
|
|
|
|
|
return persistentClass.getDeclaredMethod("get" + StringTool.toUpperCaseFirstOne(colName)).invoke(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
List<SysTool> sysTools = new ArrayList<>();
|
|
|
|
|
SysTool sysTool = new SysTool();
|
|
|
|
|