|
|
@ -1,28 +1,38 @@
|
|
|
|
package cn.estsh.i3plus.core.apiservice.util;
|
|
|
|
package cn.estsh.i3plus.core.apiservice.util;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.estsh.i3plus.core.api.iservice.busi.ISysConfigService;
|
|
|
|
import cn.estsh.i3plus.core.api.iservice.busi.ISysConfigService;
|
|
|
|
|
|
|
|
import cn.estsh.i3plus.core.api.iservice.busi.ISysDictionaryService;
|
|
|
|
|
|
|
|
import cn.estsh.i3plus.platform.common.tool.ReflexTool;
|
|
|
|
import cn.estsh.i3plus.platform.common.tool.StringTool;
|
|
|
|
import cn.estsh.i3plus.platform.common.tool.StringTool;
|
|
|
|
import cn.estsh.i3plus.platform.common.tool.TimeTool;
|
|
|
|
import cn.estsh.i3plus.platform.common.tool.TimeTool;
|
|
|
|
import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
|
|
|
|
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.base.enumutil.CommonEnumUtil;
|
|
|
|
|
|
|
|
import cn.estsh.i3plus.pojo.base.enumutil.ImppEnumUtil;
|
|
|
|
|
|
|
|
import cn.estsh.i3plus.pojo.platform.bean.SysDictionary;
|
|
|
|
|
|
|
|
import cn.estsh.i3plus.pojo.platform.bean.SysFile;
|
|
|
|
|
|
|
|
import cn.estsh.i3plus.pojo.platform.bean.SysMessage;
|
|
|
|
import cn.estsh.i3plus.pojo.platform.bean.SysTool;
|
|
|
|
import cn.estsh.i3plus.pojo.platform.bean.SysTool;
|
|
|
|
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
|
|
|
|
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
|
|
|
|
import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
|
|
|
|
import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
|
|
|
|
import io.swagger.annotations.ApiParam;
|
|
|
|
import io.swagger.annotations.ApiParam;
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
|
import org.apache.poi.ss.usermodel.CellType;
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import javax.persistence.EntityManager;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
@ -39,9 +49,6 @@ import java.util.*;
|
|
|
|
public class ExcelUtil {
|
|
|
|
public class ExcelUtil {
|
|
|
|
public static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtil.class);
|
|
|
|
public static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtil.class);
|
|
|
|
|
|
|
|
|
|
|
|
private ExcelUtil() throws IOException {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static ISysConfigService sysConfigService;
|
|
|
|
private static ISysConfigService sysConfigService;
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
@ -49,6 +56,27 @@ public class ExcelUtil {
|
|
|
|
ExcelUtil.sysConfigService = sysConfigService;
|
|
|
|
ExcelUtil.sysConfigService = sysConfigService;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static EntityManager entityManager;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
public void setEntityManager(EntityManager entityManager) {
|
|
|
|
|
|
|
|
ExcelUtil.entityManager = entityManager;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static ISysDictionaryService sysDictionaryService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
public void setSysDictionaryService(ISysDictionaryService sysDictionaryService){
|
|
|
|
|
|
|
|
ExcelUtil.sysDictionaryService = sysDictionaryService;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static RabbitTemplate rabbitTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
|
|
|
|
|
|
|
|
this.rabbitTemplate = rabbitTemplate;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 导出
|
|
|
|
* 导出
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -56,15 +84,11 @@ public class ExcelUtil {
|
|
|
|
* @param exportClass 导出数据类型
|
|
|
|
* @param exportClass 导出数据类型
|
|
|
|
* @param exportCol 需要导出的列
|
|
|
|
* @param exportCol 需要导出的列
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static byte[] exportData(List data, Class exportClass, String[] exportCol) {
|
|
|
|
public static File exportData(File excelFile,List data, Class exportClass, String[] exportCol) {
|
|
|
|
File excelFile = null;
|
|
|
|
|
|
|
|
FileOutputStream fos = null;
|
|
|
|
FileOutputStream fos = null;
|
|
|
|
//创建HSSFWorkbook对象(excel的文档对象)
|
|
|
|
//创建HSSFWorkbook对象(excel的文档对象)
|
|
|
|
HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
|
HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
//创建临时文件
|
|
|
|
|
|
|
|
excelFile = File.createTempFile(exportClass.getSimpleName() + new Date().getTime(), ".xls");
|
|
|
|
|
|
|
|
LOGGER.info("临时文件所在的本地路径:" + excelFile.getCanonicalPath());
|
|
|
|
|
|
|
|
fos = new FileOutputStream(excelFile);
|
|
|
|
fos = new FileOutputStream(excelFile);
|
|
|
|
|
|
|
|
|
|
|
|
//建立新的sheet对象(excel的表单)
|
|
|
|
//建立新的sheet对象(excel的表单)
|
|
|
@ -74,18 +98,26 @@ public class ExcelUtil {
|
|
|
|
HSSFRow tableHeader = sheet.createRow(0);
|
|
|
|
HSSFRow tableHeader = sheet.createRow(0);
|
|
|
|
|
|
|
|
|
|
|
|
// 类数据
|
|
|
|
// 类数据
|
|
|
|
Field[] declaredFields = exportClass.getDeclaredFields();
|
|
|
|
|
|
|
|
Field[] fields = new Field[exportCol.length];
|
|
|
|
Field[] fields = new Field[exportCol.length];
|
|
|
|
|
|
|
|
Field[] allFields = ReflexTool.getAllField(exportClass.getName());
|
|
|
|
|
|
|
|
String colName;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取字段中文名 优先使用 OutputColumn.name
|
|
|
|
for (int i = 0; i < exportCol.length; i++) {
|
|
|
|
for (int i = 0; i < exportCol.length; i++) {
|
|
|
|
for (Field field : declaredFields) {
|
|
|
|
for (int j = 0 ; j < allFields.length ; j++){
|
|
|
|
if (field.getName().equals(exportCol[i])) {
|
|
|
|
if(exportCol[i].equals(allFields[j].getName())){
|
|
|
|
fields[i] = field;
|
|
|
|
fields[i] = allFields[j];
|
|
|
|
|
|
|
|
|
|
|
|
if (field.isAnnotationPresent(ApiParam.class)) {
|
|
|
|
colName = null;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -93,26 +125,48 @@ public class ExcelUtil {
|
|
|
|
//加载数据至excel对象
|
|
|
|
//加载数据至excel对象
|
|
|
|
HSSFRow hssfRow;
|
|
|
|
HSSFRow hssfRow;
|
|
|
|
Method method;
|
|
|
|
Method method;
|
|
|
|
|
|
|
|
AnnoOutputColumn outputColumn;
|
|
|
|
|
|
|
|
Object cellValue;
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
hssfRow = sheet.createRow(i + 1);
|
|
|
|
hssfRow = sheet.createRow(i + 1);
|
|
|
|
for (int j = 0; j < fields.length; j++) {
|
|
|
|
for (int j = 0; j < fields.length; j++) {
|
|
|
|
method = exportClass.getDeclaredMethod("get" + StringTool.toUpperCaseFirstOne(fields[j].getName()));
|
|
|
|
fields[j].setAccessible(true);
|
|
|
|
hssfRow.createCell(j, CellType.STRING).setCellValue(
|
|
|
|
cellValue = fields[j].get(data.get(i));
|
|
|
|
String.valueOf(method.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 if(outputColumn.refClass().equals(SysDictionary.class) && cellValue != null){
|
|
|
|
|
|
|
|
cellValue = sysDictionaryService.getSysDictionaryByParentCodeAndValue(outputColumn.refForeignKey(), String.valueOf(cellValue)).getName();
|
|
|
|
|
|
|
|
} else if(!outputColumn.refClass().equals(Object.class) && !outputColumn.refClass().equals(SysDictionary.class)){
|
|
|
|
|
|
|
|
cellValue = selectByProperty(outputColumn.refClass(), outputColumn.value(), outputColumn.refForeignKey(), cellValue);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// excel 文本框最大长度
|
|
|
|
|
|
|
|
if(String.valueOf(cellValue).length() > 30000){
|
|
|
|
|
|
|
|
hssfRow.createCell(j, CellType.STRING).setCellValue(String.valueOf(cellValue).substring(0,30000));
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
hssfRow.createCell(j, CellType.STRING).setCellValue(String.valueOf(cellValue));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
workbook.write(fos);
|
|
|
|
workbook.write(fos);
|
|
|
|
fos.flush();
|
|
|
|
fos.flush();
|
|
|
|
return file2Byte(excelFile);
|
|
|
|
return excelFile;
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOGGER.error("属性get方法不存在!", e);
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
|
|
|
|
LOGGER.error("Excel 导出异常!", e);
|
|
|
|
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
|
|
|
|
LOGGER.error("Excel 导出异常!", e);
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
.setErrorCode(ImppExceptionEnum.IO_EXCEPTION_FILE.getCode())
|
|
|
|
.setErrorCode(ImppExceptionEnum.IO_EXCEPTION_FILE.getCode())
|
|
|
@ -121,13 +175,11 @@ public class ExcelUtil {
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
workbook.close();
|
|
|
|
workbook.close();
|
|
|
|
fos.close();
|
|
|
|
fos.close();;
|
|
|
|
excelFile.deleteOnExit();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOGGER.error("IOException!", e);
|
|
|
|
LOGGER.error("IOException!", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -172,11 +224,12 @@ public class ExcelUtil {
|
|
|
|
|
|
|
|
|
|
|
|
// excel列名与字段名映射
|
|
|
|
// excel列名与字段名映射
|
|
|
|
Map<String, Field> colName = new HashMap<>();
|
|
|
|
Map<String, Field> colName = new HashMap<>();
|
|
|
|
ApiParam fieldAnno;
|
|
|
|
for (Field field : ReflexTool.getAllField(importClass.getName())) {
|
|
|
|
for (Field field : importClass.getDeclaredFields()) {
|
|
|
|
if (field.isAnnotationPresent(AnnoOutputColumn.class)
|
|
|
|
if (field.isAnnotationPresent(ApiParam.class)) {
|
|
|
|
&& !StringUtils.isBlank(field.getAnnotation(AnnoOutputColumn.class).name())) {
|
|
|
|
fieldAnno = field.getAnnotation(ApiParam.class);
|
|
|
|
colName.put(field.getAnnotation(AnnoOutputColumn.class).name(), field);
|
|
|
|
colName.put(fieldAnno.value(), field);
|
|
|
|
} else if (field.isAnnotationPresent(ApiParam.class)) {
|
|
|
|
|
|
|
|
colName.put(field.getAnnotation(ApiParam.class).value(), field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -191,61 +244,42 @@ public class ExcelUtil {
|
|
|
|
Row row;
|
|
|
|
Row row;
|
|
|
|
Object obj;
|
|
|
|
Object obj;
|
|
|
|
Object cellValue = null;
|
|
|
|
Object cellValue = null;
|
|
|
|
|
|
|
|
AnnoOutputColumn inputColumn;
|
|
|
|
|
|
|
|
Method method;
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
|
|
|
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
|
|
|
row = sheet.getRow(i);
|
|
|
|
row = sheet.getRow(i);
|
|
|
|
obj = importClass.newInstance();
|
|
|
|
obj = importClass.newInstance();
|
|
|
|
for (int j = 0; j < fields.length; j++) {
|
|
|
|
for (int j = 0; j < fields.length; j++) {
|
|
|
|
row.getCell(j).setCellType(CellType.STRING);
|
|
|
|
// 判断是否存在引用关系
|
|
|
|
if("".equals(row.getCell(j).getStringCellValue())){
|
|
|
|
if (fields[j].isAnnotationPresent(AnnoOutputColumn.class)) {
|
|
|
|
cellValue = null;
|
|
|
|
inputColumn = fields[j].getAnnotation(AnnoOutputColumn.class);
|
|
|
|
}else if (fields[j].getType() == String.class) {
|
|
|
|
|
|
|
|
cellValue = row.getCell(j).getStringCellValue();
|
|
|
|
cellValue = row.getCell(j).getStringCellValue();
|
|
|
|
} else if (fields[j].getType() == Integer.class) {
|
|
|
|
|
|
|
|
cellValue = Integer.parseInt(row.getCell(j).getStringCellValue());
|
|
|
|
// 判断是否为枚举字段
|
|
|
|
} else if (fields[j].getType() == Long.class) {
|
|
|
|
if (inputColumn.refClass().isEnum()) {
|
|
|
|
cellValue = Long.parseLong(row.getCell(j).getStringCellValue());
|
|
|
|
method = inputColumn.refClass().getDeclaredMethod(
|
|
|
|
|
|
|
|
inputColumn.value() + "Of" + StringTool.toUpperCaseFirstOne(inputColumn.refForeignKey()),
|
|
|
|
|
|
|
|
inputColumn.refClass().getDeclaredMethod("get"
|
|
|
|
|
|
|
|
+ StringTool.toUpperCaseFirstOne(inputColumn.value())).getReturnType());
|
|
|
|
|
|
|
|
cellValue = method.invoke(null,cellValue);
|
|
|
|
|
|
|
|
}else if(inputColumn.refClass().equals(SysDictionary.class) && cellValue != null){
|
|
|
|
|
|
|
|
cellValue = sysDictionaryService.getSysDictionaryByParentCodeAndName(inputColumn.refForeignKey(), String.valueOf(cellValue)).getName();
|
|
|
|
|
|
|
|
} else if(!inputColumn.refClass().equals(Object.class) && !inputColumn.refClass().equals(SysDictionary.class)){
|
|
|
|
|
|
|
|
cellValue = selectByProperty(inputColumn.refClass(), inputColumn.refForeignKey(), inputColumn.value(), cellValue);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
cellValue = getExcelCell(row.getCell(j),fields[j].getType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
importClass.getDeclaredMethod("set" + StringTool.toUpperCaseFirstOne(fields[j].getName()), fields[j].getType())
|
|
|
|
fields[j].setAccessible(true);
|
|
|
|
.invoke(obj, cellValue);
|
|
|
|
fields[j].set(obj,cellValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dataList.add(obj);
|
|
|
|
dataList.add(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (Exception e){
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
e.printStackTrace();
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.IO_EXCEPTION.getCode())
|
|
|
|
|
|
|
|
.setErrorDetail("IO输入输出异常")
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.REFLEX_EXCEPTION.getCode())
|
|
|
|
|
|
|
|
.setErrorDetail("无法访问导入类")
|
|
|
|
|
|
|
|
.setErrorSolution("请检查导入类访问修饰符")
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
} catch (InstantiationException e) {
|
|
|
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.REFLEX_EXCEPTION.getCode())
|
|
|
|
|
|
|
|
.setErrorDetail("无法实例化导入类")
|
|
|
|
|
|
|
|
.setErrorSolution("请检查导入类是拥有无参构造方法")
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.REFLEX_EXCEPTION.getCode())
|
|
|
|
|
|
|
|
.setErrorDetail("属性set方法实现错误")
|
|
|
|
|
|
|
|
.setErrorSolution("请检查属性set方法参数类型是否正确")
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.REFLEX_EXCEPTION.getCode())
|
|
|
|
|
|
|
|
.setErrorDetail("没有找到属性set方法")
|
|
|
|
|
|
|
|
.setErrorSolution("请检查属性set方法是否存在")
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dataList;
|
|
|
|
return dataList;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -272,27 +306,35 @@ public class ExcelUtil {
|
|
|
|
|
|
|
|
|
|
|
|
// 创建表头
|
|
|
|
// 创建表头
|
|
|
|
HSSFRow tableHeader = sheet.createRow(0);
|
|
|
|
HSSFRow tableHeader = sheet.createRow(0);
|
|
|
|
HSSFRow tableData = sheet.createRow(1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 类数据
|
|
|
|
// 类数据
|
|
|
|
Field[] declaredFields = exportClass.getDeclaredFields();
|
|
|
|
Field[] fields = ReflexTool.getAllField(exportClass.getName());
|
|
|
|
ApiParam fieldAnno;
|
|
|
|
|
|
|
|
int col = 0;
|
|
|
|
int col = 0;
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < declaredFields.length; i++) {
|
|
|
|
for (int i = 0; i < fields.length; i++) {
|
|
|
|
if (declaredFields[i].isAnnotationPresent(ApiParam.class)) {
|
|
|
|
if (fields[i].isAnnotationPresent(AnnoOutputColumn.class)) {
|
|
|
|
fieldAnno = declaredFields[i].getAnnotation(ApiParam.class);
|
|
|
|
// 是否隐藏列
|
|
|
|
if (!fieldAnno.hidden()) {
|
|
|
|
if(!fields[i].getAnnotation(AnnoOutputColumn.class).hidden()){
|
|
|
|
tableHeader.createCell(col, CellType.STRING).setCellValue(fieldAnno.value());
|
|
|
|
if (fields[i].isAnnotationPresent(AnnoOutputColumn.class)) {
|
|
|
|
|
|
|
|
tableHeader.createCell(col, CellType.STRING).setCellValue(fields[i].getAnnotation(AnnoOutputColumn.class).name());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tableData.createCell(col, CellType.STRING).setCellValue(fieldAnno.example());
|
|
|
|
// 优先使用 AnnoOutputColumn.name()
|
|
|
|
|
|
|
|
if (fields[i].isAnnotationPresent(ApiParam.class) && StringUtils.isBlank(fields[i].getAnnotation(AnnoOutputColumn.class).name())) {
|
|
|
|
|
|
|
|
tableHeader.createCell(col, CellType.STRING).setCellValue(fields[i].getAnnotation(ApiParam.class).value());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
col++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (fields[i].isAnnotationPresent(ApiParam.class)) {
|
|
|
|
|
|
|
|
tableHeader.createCell(col, CellType.STRING).setCellValue(fields[i].getAnnotation(ApiParam.class).value());
|
|
|
|
col++;
|
|
|
|
col++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
workbook.write(fos);
|
|
|
|
workbook.write(fos);
|
|
|
|
return file2Byte(excelFile);
|
|
|
|
return FileUtil.file2Byte(excelFile.getPath());
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
@ -312,67 +354,97 @@ public class ExcelUtil {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取实体列集合
|
|
|
|
* 获取实体列集合
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param pojoClass
|
|
|
|
* @param pojoClass
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static Map getColName(Class pojoClass) {
|
|
|
|
public static Map getColName(Class pojoClass) {
|
|
|
|
Map colName = new LinkedHashMap();
|
|
|
|
Map colName = new LinkedHashMap();
|
|
|
|
Field[] field = pojoClass.getDeclaredFields();
|
|
|
|
Field[] fields = ReflexTool.getAllField(pojoClass.getName());
|
|
|
|
ApiParam fieldAnno;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < field.length; i++) {
|
|
|
|
for (int i = 0; i < fields.length; i++) {
|
|
|
|
if (field[i].isAnnotationPresent(ApiParam.class)) {
|
|
|
|
if (fields[i].isAnnotationPresent(ApiParam.class)) {
|
|
|
|
fieldAnno = field[i].getAnnotation(ApiParam.class);
|
|
|
|
colName.put(fields[i].getName(),fields[i].getAnnotation(ApiParam.class).value());
|
|
|
|
if (!fieldAnno.hidden()) {
|
|
|
|
}
|
|
|
|
colName.put(field[i].getAnnotation(ApiParam.class).value(), field[i].getName());
|
|
|
|
|
|
|
|
|
|
|
|
if (fields[i].isAnnotationPresent(AnnoOutputColumn.class)) {
|
|
|
|
|
|
|
|
// 判断是否隐藏
|
|
|
|
|
|
|
|
if(fields[i].getAnnotation(AnnoOutputColumn.class).hidden()){
|
|
|
|
|
|
|
|
colName.remove(fields[i].getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 如果AnnoOutputColumn存在name属性则优先使用
|
|
|
|
|
|
|
|
if(!StringUtils.isBlank(fields[i].getAnnotation(AnnoOutputColumn.class).name())){
|
|
|
|
|
|
|
|
colName.put(fields[i].getName(),fields[i].getAnnotation(AnnoOutputColumn.class).name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return colName;
|
|
|
|
return colName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
/**
|
|
|
|
List<SysTool> sysTools = new ArrayList<>();
|
|
|
|
* 查询外键信息
|
|
|
|
SysTool sysTool = new SysTool();
|
|
|
|
* @param persistentClass
|
|
|
|
for (int i = 0; i < 14; i++) {
|
|
|
|
* @param colName
|
|
|
|
sysTool.setName("ddd");
|
|
|
|
* @param propertyName
|
|
|
|
sysTools.add(sysTool);
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
return entityManager.createQuery(queryString).setParameter(propertyName, value).getSingleResult();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String[] colName = new String[]{"name", "toolTypeNameRdd", "toolStatus", "toolIp", "toolPort", "toolConnType", "toolDataType", "toolOperating", "toolDescription"};
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
FileOutputStream ds = new FileOutputStream("E://testOut.xls");
|
|
|
|
|
|
|
|
ds.write(ExcelUtil.exportData(sysTools, SysTool.class, colName));
|
|
|
|
|
|
|
|
ds.flush();
|
|
|
|
|
|
|
|
ds.close();
|
|
|
|
|
|
|
|
// ExcelUtil.importData("testOut.xls", new FileInputStream("E://testOut.xls"), SysTool.class);
|
|
|
|
|
|
|
|
// ExcelUtil.importTemplate(new FileOutputStream("E://testOut.xls"), SysTool.class, colName);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
/**
|
|
|
|
e.printStackTrace();
|
|
|
|
* 获取excel单元格值
|
|
|
|
|
|
|
|
* @param cell
|
|
|
|
|
|
|
|
* @param cellClass
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private static Object getExcelCell(Cell cell, Class cellClass) {
|
|
|
|
|
|
|
|
if (cell != null) {
|
|
|
|
|
|
|
|
cell.setCellType(CellType.STRING);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ("".equals(cell.getStringCellValue()) || "null".equals(cell.getStringCellValue())) {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
} else if (cellClass == String.class) {
|
|
|
|
|
|
|
|
return cell.getStringCellValue();
|
|
|
|
|
|
|
|
} else if (cellClass == Integer.class) {
|
|
|
|
|
|
|
|
return Integer.parseInt(cell.getStringCellValue());
|
|
|
|
|
|
|
|
} else if (cellClass == Long.class) {
|
|
|
|
|
|
|
|
return Long.parseLong(cell.getStringCellValue());
|
|
|
|
|
|
|
|
} else if (cellClass == Double.class) {
|
|
|
|
|
|
|
|
return Long.parseLong(cell.getStringCellValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
public static byte[] file2Byte(File file) {
|
|
|
|
* 发送包含文件列表的站内信
|
|
|
|
byte[] buffer = null;
|
|
|
|
* @param fileList
|
|
|
|
try {
|
|
|
|
* @param userId
|
|
|
|
FileInputStream fis = new FileInputStream(file);
|
|
|
|
*/
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
public static void sendStationLetter(List<SysFile> fileList, Long userId){
|
|
|
|
byte[] b = new byte[1024];
|
|
|
|
StringBuffer letter = new StringBuffer();
|
|
|
|
int n;
|
|
|
|
letter.append("导出文件列表:");
|
|
|
|
while ((n = fis.read(b)) != -1) {
|
|
|
|
for (SysFile sysFile:fileList) {
|
|
|
|
bos.write(b, 0, n);
|
|
|
|
letter.append("<a href='/impp/operate/sys-file/download/"+sysFile.getId() + "'>");
|
|
|
|
}
|
|
|
|
letter.append(sysFile.getFileOriginalName());
|
|
|
|
fis.close();
|
|
|
|
letter.append("</a>");
|
|
|
|
bos.close();
|
|
|
|
|
|
|
|
buffer = bos.toByteArray();
|
|
|
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
|
|
|
|
|
|
|
|
// 消息提示
|
|
|
|
|
|
|
|
SysMessage sysMessage = new SysMessage();
|
|
|
|
|
|
|
|
sysMessage.setMessageSenderNameRdd(CommonEnumUtil.SOFT_TYPE.CORE.getDescription());
|
|
|
|
|
|
|
|
sysMessage.setMessageTitle("数据导出");
|
|
|
|
|
|
|
|
sysMessage.setMessageContent(letter.toString());
|
|
|
|
|
|
|
|
sysMessage.setMessageType(ImppEnumUtil.MESSAGE_TYPE.LETTER.getValue());
|
|
|
|
|
|
|
|
sysMessage.setMessageContentType(ImppEnumUtil.MESSAGE_TYPE_CONTENT.HTML.getValue());
|
|
|
|
|
|
|
|
sysMessage.setMessageSendTime(TimeTool.getNowTime(true));
|
|
|
|
|
|
|
|
sysMessage.setIsSystem(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
|
|
|
|
|
|
|
|
sysMessage.setMessageReceiversId(String.valueOf(userId));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rabbitTemplate.convertAndSend(PlatformConstWords.IMPP_MESSAGE_LETTER_QUEUE,sysMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|