导入导出支持父类字段

dep_wangsr
RuoYi 6 years ago committed by Limy
parent fcb34930a6
commit fdbf6d3ebf

@ -7,8 +7,10 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -221,6 +223,10 @@ public class ExcelUtil<T>
{ {
val = Convert.toFloat(val); val = Convert.toFloat(val);
} }
else if (BigDecimal.class == fieldType)
{
val = Convert.toBigDecimal(val);
}
else if (Date.class == fieldType) else if (Date.class == fieldType)
{ {
if (val instanceof String) if (val instanceof String)
@ -665,14 +671,29 @@ public class ExcelUtil<T>
private void createExcelField() private void createExcelField()
{ {
this.fields = new ArrayList<Field>(); this.fields = new ArrayList<Field>();
Field[] allFields = clazz.getDeclaredFields(); List<Field> tempFields = new ArrayList<>();
// 得到所有field并存放到一个list中. Class<?> tempClass = clazz;
for (Field field : allFields) tempFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
while (tempClass != null)
{
tempClass = tempClass.getSuperclass();
if (tempClass != null)
tempFields.addAll(Arrays.asList(tempClass.getDeclaredFields()));
}
putToFields(tempFields);
}
/**
*
*/
private void putToFields(List<Field> fields)
{
for (Field field : fields)
{ {
Excel attr = field.getAnnotation(Excel.class); Excel attr = field.getAnnotation(Excel.class);
if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
{ {
fields.add(field); this.fields.add(field);
} }
} }
} }

Loading…
Cancel
Save