|
|
|
@ -20,10 +20,7 @@ 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;
|
|
|
|
|
import org.apache.poi.ss.usermodel.CellType;
|
|
|
|
|
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.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
@ -80,7 +77,6 @@ public class ExcelUtil {
|
|
|
|
|
this.rabbitTemplate = rabbitTemplate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 导出
|
|
|
|
|
*
|
|
|
|
@ -130,13 +126,13 @@ public class ExcelUtil {
|
|
|
|
|
HSSFRow hssfRow;
|
|
|
|
|
Method method;
|
|
|
|
|
AnnoOutputColumn outputColumn;
|
|
|
|
|
Object CellValue;
|
|
|
|
|
Object cellValue;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
|
hssfRow = sheet.createRow(i + 1);
|
|
|
|
|
for (int j = 0; j < fields.length; j++) {
|
|
|
|
|
fields[j].setAccessible(true);
|
|
|
|
|
CellValue = fields[j].get(data.get(i));
|
|
|
|
|
cellValue = fields[j].get(data.get(i));
|
|
|
|
|
|
|
|
|
|
// 判断是否存在引用关系
|
|
|
|
|
if (fields[j].isAnnotationPresent(AnnoOutputColumn.class)) {
|
|
|
|
@ -148,19 +144,19 @@ public class ExcelUtil {
|
|
|
|
|
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();
|
|
|
|
|
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);
|
|
|
|
|
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));
|
|
|
|
|
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));
|
|
|
|
|
hssfRow.createCell(j, CellType.STRING).setCellValue(String.valueOf(cellValue));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -228,11 +224,12 @@ public class ExcelUtil {
|
|
|
|
|
|
|
|
|
|
// excel列名与字段名映射
|
|
|
|
|
Map<String, Field> colName = new HashMap<>();
|
|
|
|
|
ApiParam fieldAnno;
|
|
|
|
|
for (Field field : importClass.getDeclaredFields()) {
|
|
|
|
|
if (field.isAnnotationPresent(ApiParam.class)) {
|
|
|
|
|
fieldAnno = field.getAnnotation(ApiParam.class);
|
|
|
|
|
colName.put(fieldAnno.value(), field);
|
|
|
|
|
for (Field field : ReflexTool.getAllField(importClass.getName())) {
|
|
|
|
|
if (field.isAnnotationPresent(AnnoOutputColumn.class)
|
|
|
|
|
&& !StringUtils.isBlank(field.getAnnotation(AnnoOutputColumn.class).name())) {
|
|
|
|
|
colName.put(field.getAnnotation(AnnoOutputColumn.class).name(), field);
|
|
|
|
|
} else if (field.isAnnotationPresent(ApiParam.class)) {
|
|
|
|
|
colName.put(field.getAnnotation(ApiParam.class).value(), field);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -247,61 +244,42 @@ public class ExcelUtil {
|
|
|
|
|
Row row;
|
|
|
|
|
Object obj;
|
|
|
|
|
Object cellValue = null;
|
|
|
|
|
AnnoOutputColumn inputColumn;
|
|
|
|
|
Method method;
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
|
|
|
|
row = sheet.getRow(i);
|
|
|
|
|
obj = importClass.newInstance();
|
|
|
|
|
for (int j = 0; j < fields.length; j++) {
|
|
|
|
|
row.getCell(j).setCellType(CellType.STRING);
|
|
|
|
|
if ("".equals(row.getCell(j).getStringCellValue())) {
|
|
|
|
|
cellValue = null;
|
|
|
|
|
} else if (fields[j].getType() == String.class) {
|
|
|
|
|
// 判断是否存在引用关系
|
|
|
|
|
if (fields[j].isAnnotationPresent(AnnoOutputColumn.class)) {
|
|
|
|
|
inputColumn = fields[j].getAnnotation(AnnoOutputColumn.class);
|
|
|
|
|
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) {
|
|
|
|
|
cellValue = Long.parseLong(row.getCell(j).getStringCellValue());
|
|
|
|
|
|
|
|
|
|
// 判断是否为枚举字段
|
|
|
|
|
if (inputColumn.refClass().isEnum()) {
|
|
|
|
|
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())
|
|
|
|
|
.invoke(obj, cellValue);
|
|
|
|
|
fields[j].setAccessible(true);
|
|
|
|
|
fields[j].set(obj,cellValue);
|
|
|
|
|
}
|
|
|
|
|
dataList.add(obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.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();
|
|
|
|
|
} catch (Exception e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return dataList;
|
|
|
|
|
}
|
|
|
|
@ -328,20 +306,28 @@ public class ExcelUtil {
|
|
|
|
|
|
|
|
|
|
// 创建表头
|
|
|
|
|
HSSFRow tableHeader = sheet.createRow(0);
|
|
|
|
|
HSSFRow tableData = sheet.createRow(1);
|
|
|
|
|
|
|
|
|
|
// 类数据
|
|
|
|
|
Field[] declaredFields = exportClass.getDeclaredFields();
|
|
|
|
|
ApiParam fieldAnno;
|
|
|
|
|
Field[] fields = ReflexTool.getAllField(exportClass.getName());
|
|
|
|
|
int col = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < declaredFields.length; i++) {
|
|
|
|
|
if (declaredFields[i].isAnnotationPresent(ApiParam.class)) {
|
|
|
|
|
fieldAnno = declaredFields[i].getAnnotation(ApiParam.class);
|
|
|
|
|
if (!fieldAnno.hidden()) {
|
|
|
|
|
tableHeader.createCell(col, CellType.STRING).setCellValue(fieldAnno.value());
|
|
|
|
|
for (int i = 0; i < fields.length; i++) {
|
|
|
|
|
if (fields[i].isAnnotationPresent(AnnoOutputColumn.class)) {
|
|
|
|
|
// 是否隐藏列
|
|
|
|
|
if(!fields[i].getAnnotation(AnnoOutputColumn.class).hidden()){
|
|
|
|
|
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++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -368,7 +354,6 @@ public class ExcelUtil {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取实体列集合
|
|
|
|
|
*
|
|
|
|
|
* @param pojoClass
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
@ -381,7 +366,6 @@ public class ExcelUtil {
|
|
|
|
|
colName.put(fields[i].getName(),fields[i].getAnnotation(ApiParam.class).value());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fields[i].isAnnotationPresent(AnnoOutputColumn.class)) {
|
|
|
|
|
// 判断是否隐藏
|
|
|
|
|
if(fields[i].getAnnotation(AnnoOutputColumn.class).hidden()){
|
|
|
|
@ -397,6 +381,7 @@ public class ExcelUtil {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询外键信息
|
|
|
|
|
* @param persistentClass
|
|
|
|
|
* @param colName
|
|
|
|
|
* @param propertyName
|
|
|
|
@ -409,7 +394,36 @@ public class ExcelUtil {
|
|
|
|
|
return entityManager.createQuery(queryString).setParameter(propertyName, value).getSingleResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送包含文件列表的站内信
|
|
|
|
|
* @param fileList
|
|
|
|
|
* @param userId
|
|
|
|
|
*/
|
|
|
|
|
public static void sendStationLetter(List<SysFile> fileList, Long userId){
|
|
|
|
|
StringBuffer letter = new StringBuffer();
|
|
|
|
|
letter.append("导出文件列表:");
|
|
|
|
@ -433,42 +447,4 @@ public class ExcelUtil {
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
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 (IllegalAccessException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|