|
|
@ -1,12 +1,13 @@
|
|
|
|
package ${basePackage}.frame.excel;
|
|
|
|
package ${basePackage}.frame.excel;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import ${basePackage}.frame.excel.converter.Converter;
|
|
|
|
|
|
|
|
import ${basePackage}.frame.excel.exception.ValueConverterException;
|
|
|
|
import ${basePackage}.frame.excel.style.DataCellStyle;
|
|
|
|
import ${basePackage}.frame.excel.style.DataCellStyle;
|
|
|
|
import ${basePackage}.frame.excel.style.ErrorCellStyle;
|
|
|
|
import ${basePackage}.frame.excel.style.ErrorCellStyle;
|
|
|
|
import ${basePackage}.frame.excel.style.HeadCellStyle;
|
|
|
|
import ${basePackage}.frame.excel.style.HeadCellStyle;
|
|
|
|
import ${basePackage}.frame.excel.style.RedFont;
|
|
|
|
import ${basePackage}.frame.excel.style.RedFont;
|
|
|
|
import ${basePackage}.frame.utils.ClassUtil;
|
|
|
|
import ${basePackage}.frame.utils.ClassUtil;
|
|
|
|
import ${basePackage}.frame.utils.LogUtil;
|
|
|
|
import ${basePackage}.frame.utils.LogUtil;
|
|
|
|
import ${basePackage}.frame.utils.StringUtil;
|
|
|
|
|
|
|
|
import ${basePackage}.frame.utils.ValidationUtil;
|
|
|
|
import ${basePackage}.frame.utils.ValidationUtil;
|
|
|
|
import ${basePackage}.frame.excel.annotation.*;
|
|
|
|
import ${basePackage}.frame.excel.annotation.*;
|
|
|
|
import ${basePackage}.frame.excel.converter.*;
|
|
|
|
import ${basePackage}.frame.excel.converter.*;
|
|
|
@ -64,7 +65,7 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 根据模板类对DataTable添加相应的列。
|
|
|
|
* 根据模板类对DataTable添加相应的列。
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return 包含@Name标记的字段Set
|
|
|
|
* @return WColumn集合
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private List<WColumn> initColumns(Class<?> clazz) {
|
|
|
|
private List<WColumn> initColumns(Class<?> clazz) {
|
|
|
|
//获取工作簿名称,没有则以类名为默认工作簿名称
|
|
|
|
//获取工作簿名称,没有则以类名为默认工作簿名称
|
|
|
@ -87,6 +88,9 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
WColumn WColumn = new WColumn();
|
|
|
|
WColumn WColumn = new WColumn();
|
|
|
|
WColumn.setField(field);
|
|
|
|
WColumn.setField(field);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Method set = ClassUtil.setMethod(field.getName(), clazz, field.getType());
|
|
|
|
|
|
|
|
WColumn.setSetMethod(set);
|
|
|
|
|
|
|
|
|
|
|
|
//获取列名称
|
|
|
|
//获取列名称
|
|
|
|
if (!field.isAnnotationPresent(ColumnName.class)) {
|
|
|
|
if (!field.isAnnotationPresent(ColumnName.class)) {
|
|
|
|
WColumn.setName(field.getName());
|
|
|
|
WColumn.setName(field.getName());
|
|
|
@ -110,7 +114,7 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
${basePackage}.frame.excel.annotation.Converter converter = field.getAnnotation(${basePackage}.frame.excel.annotation.Converter.class);
|
|
|
|
${basePackage}.frame.excel.annotation.Converter converter = field.getAnnotation(${basePackage}.frame.excel.annotation.Converter.class);
|
|
|
|
Class target = converter.target();
|
|
|
|
Class target = converter.target();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
WColumn.setConverter((${basePackage}.frame.excel.converter.Converter) target.newInstance());
|
|
|
|
WColumn.setConverter((Converter) target.newInstance());
|
|
|
|
} catch (InstantiationException e) {
|
|
|
|
} catch (InstantiationException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
@ -162,9 +166,6 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
* 并不会对数据的格式和合法性进行检验。
|
|
|
|
* 并不会对数据的格式和合法性进行检验。
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param list 需要导出的对象列表
|
|
|
|
* @param list 需要导出的对象列表
|
|
|
|
* @throws NoSuchMethodException
|
|
|
|
|
|
|
|
* @throws InvocationTargetException
|
|
|
|
|
|
|
|
* @throws IllegalAccessException
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public WSheet(List<T> list) {
|
|
|
|
public WSheet(List<T> list) {
|
|
|
|
if (list == null || list.size() == 0) {
|
|
|
|
if (list == null || list.size() == 0) {
|
|
|
@ -180,19 +181,7 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Field field = column.getField();
|
|
|
|
Field field = column.getField();
|
|
|
|
String att = StringUtil.upperFirstWord(field.getName());
|
|
|
|
Method method = ClassUtil.setMethod(field.getName(), t.getClass(), field.getType());
|
|
|
|
Method method = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 尝试获取get方法
|
|
|
|
|
|
|
|
method = t.getClass().getMethod("get" + att);
|
|
|
|
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
|
|
|
|
// 尝试获取is方法,工具生成布尔值可能是is而不是get
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
method = t.getClass().getMethod("is" + att);
|
|
|
|
|
|
|
|
} catch (NoSuchMethodException e1) {
|
|
|
|
|
|
|
|
LogUtil.w(field.getName()+"can not find get method");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Object value = method.invoke(t);
|
|
|
|
Object value = method.invoke(t);
|
|
|
|
if (null == value) {
|
|
|
|
if (null == value) {
|
|
|
@ -202,7 +191,7 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
row.put(column.getName(), new WCell(string));
|
|
|
|
row.put(column.getName(), new WCell(string));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
LogUtil.w( "can not invoke get method!");
|
|
|
|
LogUtil.w("can not invoke get method!");
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
LogUtil.w(method.getName() + " unexpected exception!");
|
|
|
|
LogUtil.w(method.getName() + " unexpected exception!");
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -315,17 +304,17 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
T t = transferOneObject(clazz, i);
|
|
|
|
T t = clazz.newInstance();
|
|
|
|
List<String> validate = ValidationUtil.validate(t);
|
|
|
|
List<String> errs = transferMap(row, t);
|
|
|
|
validate.addAll(validator != null ? validator.validate(t) : null);
|
|
|
|
errs.addAll(ValidationUtil.validate(t));
|
|
|
|
if (validate.size() > 0) {
|
|
|
|
errs.addAll(validator != null ? validator.validate(t) : Collections.emptyList());
|
|
|
|
for (String s : validate) {
|
|
|
|
if (errs.size() > 0) {
|
|
|
|
|
|
|
|
for (String s : errs) {
|
|
|
|
row.addError(s);
|
|
|
|
row.addError(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (InstantiationException | IllegalAccessException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
row.addError("模板对象默认构造函数错误");
|
|
|
|
row.addError("数据检查错误");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -333,67 +322,38 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 转换某行为一个对象
|
|
|
|
* 转换某行为一个对象
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param clazz 类型
|
|
|
|
* @param row 行对象
|
|
|
|
* @param rowIndex 行号
|
|
|
|
* @param target 模板对象
|
|
|
|
* @return 对象
|
|
|
|
* @return 模板对象
|
|
|
|
* @throws IllegalAccessException
|
|
|
|
|
|
|
|
* @throws InstantiationException
|
|
|
|
|
|
|
|
* @throws NoSuchMethodException
|
|
|
|
|
|
|
|
* @throws InvocationTargetException
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public T transferOneObject(Class<T> clazz, int rowIndex) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
|
|
|
|
public List<String> transferMap(WRow row, T target) {
|
|
|
|
T object = clazz.newInstance();
|
|
|
|
List<String> err = new ArrayList<>();
|
|
|
|
ParentFirst parentFirstAnnotation = clazz.getAnnotation(ParentFirst.class);
|
|
|
|
for (String key : row.keySet()) {
|
|
|
|
boolean parentFirst = parentFirstAnnotation != null && parentFirstAnnotation.value();
|
|
|
|
|
|
|
|
Field[] fields = ClassUtil.getFields(clazz, parentFirst);
|
|
|
|
|
|
|
|
Set<Field> set = new HashSet<>();
|
|
|
|
|
|
|
|
for (Field field : fields) {
|
|
|
|
|
|
|
|
set.add(field);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < this.columnList.size(); j++) {
|
|
|
|
|
|
|
|
String key = this.columnList.get(j).getName();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (WColumn column : columnList) {
|
|
|
|
for (WColumn column : columnList) {
|
|
|
|
Field field = column.getField();
|
|
|
|
String name = column.getName();
|
|
|
|
ColumnName fieldColumnName = field.getAnnotation(ColumnName.class);
|
|
|
|
Method setMethod = column.getSetMethod();
|
|
|
|
|
|
|
|
|
|
|
|
if (key.equals(fieldColumnName.value())) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String att = StringUtil.upperFirstWord(field.getName());
|
|
|
|
if (key.equals(name)) {
|
|
|
|
|
|
|
|
WCell WCell = row.get(column.getName());
|
|
|
|
WCell WCell = this.rowList.get(rowIndex).get(column.getName());
|
|
|
|
|
|
|
|
if (null != WCell) {
|
|
|
|
if (null != WCell) {
|
|
|
|
String value = WCell.getValue();
|
|
|
|
String value = WCell.getValue();
|
|
|
|
Method method = clazz.getMethod("set" + att, field.getType());
|
|
|
|
|
|
|
|
//获取转换器
|
|
|
|
//获取转换器
|
|
|
|
${basePackage}.frame.excel.converter.Converter converter = column.getConverter();
|
|
|
|
Converter converter = column.getConverter();
|
|
|
|
method.invoke(object, converter.convert(value));
|
|
|
|
try {
|
|
|
|
}
|
|
|
|
setMethod.invoke(target, converter.convert(value));
|
|
|
|
break;
|
|
|
|
} catch (ValueConverterException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
err.add(e.getMessage());
|
|
|
|
|
|
|
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
err.add("[" + setMethod.getName() + "]执行错误");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 将DataTable转为T型的List<br/><br/>
|
|
|
|
|
|
|
|
* 如果你试图这么做,请确保在每个字段上都加上了@Name注解<br/>
|
|
|
|
|
|
|
|
* 并确保该注解的值与DataTable中列的名字一致。<br/>
|
|
|
|
|
|
|
|
* 注解的值在这里作为唯一的标识。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return T型列表
|
|
|
|
|
|
|
|
* @see WColumn 列名称
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public List<T> transferList(Class<T> clazz) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
|
|
|
|
|
|
|
|
List<T> list = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < this.rowList.size(); i++) {
|
|
|
|
|
|
|
|
T object = transferOneObject(clazz, i);
|
|
|
|
|
|
|
|
list.add(object);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -508,7 +468,6 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < this.columnList.size(); j++) {
|
|
|
|
for (int j = 0; j < this.columnList.size(); j++) {
|
|
|
|
WColumn column = this.columnList.get(j);
|
|
|
|
WColumn column = this.columnList.get(j);
|
|
|
|
Field field = column.getField();
|
|
|
|
|
|
|
|
Cell firstCell = firstRow.createCell(j + offset);
|
|
|
|
Cell firstCell = firstRow.createCell(j + offset);
|
|
|
|
String columnName = column.getName();
|
|
|
|
String columnName = column.getName();
|
|
|
|
XSSFRichTextString textString;
|
|
|
|
XSSFRichTextString textString;
|
|
|
@ -522,13 +481,6 @@ public class WSheet<T> implements Serializable, Cloneable {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.append(column.getDescription()).append("\n");
|
|
|
|
sb.append(column.getDescription()).append("\n");
|
|
|
|
// 添加数据说明信息。
|
|
|
|
|
|
|
|
if (field.isAnnotationPresent(ColumnDescription.class)) {
|
|
|
|
|
|
|
|
// 获取声明字段上的Description信息。
|
|
|
|
|
|
|
|
ColumnDescription columnDescription = field.getAnnotation(ColumnDescription.class);
|
|
|
|
|
|
|
|
sb.append(columnDescription.value());
|
|
|
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果填写了注释信息
|
|
|
|
// 如果填写了注释信息
|
|
|
|
if (sb.length() > 1) {
|
|
|
|
if (sb.length() > 1) {
|
|
|
|