|
|
|
@ -1,12 +1,17 @@
|
|
|
|
|
package cn.estsh.i3plus.core.apiservice.util;
|
|
|
|
|
|
|
|
|
|
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.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.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.impp.framework.boot.exception.ImppExceptionBuilder;
|
|
|
|
|
import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
|
|
|
|
@ -22,15 +27,18 @@ import org.apache.poi.ss.usermodel.Workbook;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import javax.persistence.EntityManager;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
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.InvocationTargetException;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.sql.Ref;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -44,9 +52,6 @@ import java.util.*;
|
|
|
|
|
public class ExcelUtil {
|
|
|
|
|
public static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtil.class);
|
|
|
|
|
|
|
|
|
|
private ExcelUtil() throws IOException {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ISysConfigService sysConfigService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@ -61,6 +66,20 @@ public class ExcelUtil {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 导出
|
|
|
|
@ -69,15 +88,11 @@ public class ExcelUtil {
|
|
|
|
|
* @param exportClass 导出数据类型
|
|
|
|
|
* @param exportCol 需要导出的列
|
|
|
|
|
*/
|
|
|
|
|
public static byte[] exportData(List data, Class exportClass, String[] exportCol) {
|
|
|
|
|
File excelFile = null;
|
|
|
|
|
public static File exportData(File excelFile,List data, Class exportClass, String[] exportCol) {
|
|
|
|
|
FileOutputStream fos = null;
|
|
|
|
|
//创建HSSFWorkbook对象(excel的文档对象)
|
|
|
|
|
HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
|
|
try {
|
|
|
|
|
//创建临时文件
|
|
|
|
|
excelFile = File.createTempFile(exportClass.getSimpleName() + new Date().getTime(), ".xls");
|
|
|
|
|
LOGGER.info("临时文件所在的本地路径:" + excelFile.getCanonicalPath());
|
|
|
|
|
fos = new FileOutputStream(excelFile);
|
|
|
|
|
|
|
|
|
|
//建立新的sheet对象(excel的表单)
|
|
|
|
@ -88,22 +103,27 @@ public class ExcelUtil {
|
|
|
|
|
|
|
|
|
|
// 类数据
|
|
|
|
|
Field[] fields = new Field[exportCol.length];
|
|
|
|
|
|
|
|
|
|
Field[] allFields = ReflexTool.getAllField(exportClass.getName());
|
|
|
|
|
String colName;
|
|
|
|
|
|
|
|
|
|
// 获取字段中文名 优先使用 OutputColumn.name
|
|
|
|
|
for (int i = 0; i < exportCol.length; i++) {
|
|
|
|
|
fields[i] = exportClass.getDeclaredField(exportCol[i]);
|
|
|
|
|
colName = null;
|
|
|
|
|
for (int j = 0 ; j < allFields.length ; j++){
|
|
|
|
|
if(exportCol[i].equals(allFields[j].getName())){
|
|
|
|
|
fields[i] = allFields[j];
|
|
|
|
|
|
|
|
|
|
if (fields[i].isAnnotationPresent(AnnoOutputColumn.class)) {
|
|
|
|
|
colName = fields[i].getAnnotation(AnnoOutputColumn.class).name();
|
|
|
|
|
}
|
|
|
|
|
colName = null;
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
if (fields[i].isAnnotationPresent(ApiParam.class) && StringUtils.isBlank(colName)) {
|
|
|
|
|
colName = fields[i].getAnnotation(ApiParam.class).value();
|
|
|
|
|
}
|
|
|
|
|
tableHeader.createCell(i, CellType.STRING).setCellValue(colName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tableHeader.createCell(i, CellType.STRING).setCellValue(colName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//加载数据至excel对象
|
|
|
|
@ -115,7 +135,8 @@ public class ExcelUtil {
|
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
|
hssfRow = sheet.createRow(i + 1);
|
|
|
|
|
for (int j = 0; j < fields.length; j++) {
|
|
|
|
|
CellValue = exportClass.getDeclaredMethod("get" + StringTool.toUpperCaseFirstOne(fields[j].getName())).invoke(data.get(i));
|
|
|
|
|
fields[j].setAccessible(true);
|
|
|
|
|
CellValue = fields[j].get(data.get(i));
|
|
|
|
|
|
|
|
|
|
// 判断是否存在引用关系
|
|
|
|
|
if (fields[j].isAnnotationPresent(AnnoOutputColumn.class)) {
|
|
|
|
@ -128,18 +149,26 @@ public class ExcelUtil {
|
|
|
|
|
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);
|
|
|
|
|
}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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hssfRow.createCell(j, CellType.STRING).setCellValue(String.valueOf(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);
|
|
|
|
|
fos.flush();
|
|
|
|
|
return file2Byte(excelFile);
|
|
|
|
|
return excelFile;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
@ -150,8 +179,7 @@ public class ExcelUtil {
|
|
|
|
|
} finally {
|
|
|
|
|
try {
|
|
|
|
|
workbook.close();
|
|
|
|
|
fos.close();
|
|
|
|
|
excelFile.deleteOnExit();
|
|
|
|
|
fos.close();;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
LOGGER.error("IOException!", e);
|
|
|
|
|
}
|
|
|
|
@ -320,7 +348,7 @@ public class ExcelUtil {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
workbook.write(fos);
|
|
|
|
|
return file2Byte(excelFile);
|
|
|
|
|
return FileUtil.file2Byte(excelFile.getPath());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
@ -346,18 +374,25 @@ public class ExcelUtil {
|
|
|
|
|
*/
|
|
|
|
|
public static Map getColName(Class pojoClass) {
|
|
|
|
|
Map colName = new LinkedHashMap();
|
|
|
|
|
Field[] field = pojoClass.getDeclaredFields();
|
|
|
|
|
ApiParam fieldAnno;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < field.length; i++) {
|
|
|
|
|
if (field[i].isAnnotationPresent(ApiParam.class)) {
|
|
|
|
|
fieldAnno = field[i].getAnnotation(ApiParam.class);
|
|
|
|
|
if (!fieldAnno.hidden()) {
|
|
|
|
|
colName.put(field[i].getAnnotation(ApiParam.class).value(), field[i].getName());
|
|
|
|
|
Field[] fields = ReflexTool.getAllField(pojoClass.getName());
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < fields.length; i++) {
|
|
|
|
|
if (fields[i].isAnnotationPresent(ApiParam.class)) {
|
|
|
|
|
colName.put(fields[i].getName(),fields[i].getAnnotation(ApiParam.class).value());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -371,51 +406,69 @@ public class ExcelUtil {
|
|
|
|
|
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);
|
|
|
|
|
return entityManager.createQuery(queryString).setParameter(propertyName, value).getSingleResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
List<SysTool> sysTools = new ArrayList<>();
|
|
|
|
|
SysTool sysTool = new SysTool();
|
|
|
|
|
for (int i = 0; i < 14; i++) {
|
|
|
|
|
sysTool.setName("ddd");
|
|
|
|
|
sysTools.add(sysTool);
|
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
public static void sendStationLetter(List<SysFile> fileList, Long userId){
|
|
|
|
|
StringBuffer letter = new StringBuffer();
|
|
|
|
|
letter.append("导出文件列表:");
|
|
|
|
|
for (SysFile sysFile:fileList) {
|
|
|
|
|
letter.append("<a href='/impp/operate/sys-file/download/"+sysFile.getId() + "'>");
|
|
|
|
|
letter.append(sysFile.getFileOriginalName());
|
|
|
|
|
letter.append("</a>");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 消息提示
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
// List<SysTool> sysTools = new ArrayList<>();
|
|
|
|
|
// SysTool sysTool = new SysTool();
|
|
|
|
|
// for (int i = 0; i < 14; i++) {
|
|
|
|
|
// sysTool.setName("ddd");
|
|
|
|
|
// sysTools.add(sysTool);
|
|
|
|
|
// }
|
|
|
|
|
// 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();
|
|
|
|
|
// }
|
|
|
|
|
SysTool st = new SysTool();
|
|
|
|
|
st.setName("测试");
|
|
|
|
|
st.setId(1L);
|
|
|
|
|
|
|
|
|
|
public static byte[] file2Byte(File file) {
|
|
|
|
|
byte[] buffer = null;
|
|
|
|
|
try {
|
|
|
|
|
FileInputStream fis = new FileInputStream(file);
|
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
|
byte[] b = new byte[1024];
|
|
|
|
|
int n;
|
|
|
|
|
while ((n = fis.read(b)) != -1) {
|
|
|
|
|
bos.write(b, 0, n);
|
|
|
|
|
}
|
|
|
|
|
fis.close();
|
|
|
|
|
bos.close();
|
|
|
|
|
buffer = bos.toByteArray();
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
System.out.println(SysTool.class.getField("id").isAccessible());
|
|
|
|
|
System.out.println("取值:"+ SysTool.class.getField("id").get(st));
|
|
|
|
|
System.out.println(SysTool.class.getDeclaredField("name").isAccessible());
|
|
|
|
|
Field field = SysTool.class.getDeclaredField("name");
|
|
|
|
|
field.setAccessible(true);
|
|
|
|
|
System.out.println("取值:"+ field.get(st));
|
|
|
|
|
} catch (NoSuchFieldException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return buffer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|