Pre Merge pull request !376 from lianghengyuan/ExcelUtil

pull/376/MERGE
lianghengyuan 11 months ago committed by Gitee
commit e77e4f0448
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

@ -172,6 +172,11 @@ public class ExcelUtil<T>
*/ */
public String[] excludeFields; public String[] excludeFields;
/**
*
*/
private String[] showFields;
public ExcelUtil(Class<T> clazz) public ExcelUtil(Class<T> clazz)
{ {
this.clazz = clazz; this.clazz = clazz;
@ -258,7 +263,7 @@ public class ExcelUtil<T>
/** /**
* excellist * excellist
* *
* @param is * @param is
* @return * @return
*/ */
@ -283,7 +288,7 @@ public class ExcelUtil<T>
/** /**
* excellist * excellist
* *
* @param is * @param is
* @param titleNum * @param titleNum
* @return * @return
@ -295,7 +300,7 @@ public class ExcelUtil<T>
/** /**
* excellist * excellist
* *
* @param sheetName * @param sheetName
* @param titleNum * @param titleNum
* @param is * @param is
@ -445,6 +450,67 @@ public class ExcelUtil<T>
} }
return list; return list;
} }
/**
* listexcel
*
* @param response
* @param list
* @param sheetName
* @param title
* @param showFields fieldName
* @return
*/
public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title, List<String> showFields)
{
this.showFields = showFields.toArray(new String[0]);
exportExcel(response, list, sheetName, title);
}
/**
* listexcel
*
* @param response
* @param list
* @param sheetName
* @param showFields fieldName
* @return
*/
public void exportExcel(HttpServletResponse response, List<T> list, String sheetName,List<String> showFields)
{
this.showFields = showFields.toArray(new String[0]);
exportExcel(response, list, sheetName, StringUtils.EMPTY);
}
/**
* listexcel
*
* @param response
* @param list
* @param sheetName
* @param title
* @param showFields fieldName
* @return
*/
public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title, String[] showFields)
{
this.showFields = showFields;
exportExcel(response, list, sheetName, title);
}
/**
* listexcel
*
* @param response
* @param list
* @param sheetName
* @param showFields fieldName
* @return
*/
public void exportExcel(HttpServletResponse response, List<T> list, String sheetName,String[] showFields)
{
this.showFields = showFields;
exportExcel(response, list, sheetName, StringUtils.EMPTY);
}
/** /**
* listexcel * listexcel
@ -504,7 +570,7 @@ public class ExcelUtil<T>
/** /**
* listexcel * listexcel
* *
* @return * @return
*/ */
public void exportExcel(HttpServletResponse response) public void exportExcel(HttpServletResponse response)
@ -566,7 +632,7 @@ public class ExcelUtil<T>
/** /**
* excel * excel
* *
* @param index * @param index
* @param row * @param row
*/ */
@ -637,7 +703,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param wb * @param wb
* @return * @return
*/ */
@ -692,7 +758,7 @@ public class ExcelUtil<T>
/** /**
* Excel * Excel
* *
* @param wb * @param wb
* @return * @return
*/ */
@ -728,7 +794,7 @@ public class ExcelUtil<T>
/** /**
* Excel * Excel
* *
* @param wb * @param wb
* @return * @return
*/ */
@ -760,7 +826,7 @@ public class ExcelUtil<T>
/** /**
* Excel * Excel
* *
* @param styles * @param styles
* @param field * @param field
* @param excel * @param excel
@ -822,7 +888,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param value * @param value
* @param attr * @param attr
* @param cell * @param cell
@ -981,7 +1047,7 @@ public class ExcelUtil<T>
/** /**
* POI XSSFSheet * POI XSSFSheet
* *
* @param sheet * @param sheet
* @param textlist * @param textlist
* @param promptContent * @param promptContent
@ -991,7 +1057,7 @@ public class ExcelUtil<T>
* @param endCol * @param endCol
*/ */
public void setPromptOrValidation(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow, public void setPromptOrValidation(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow,
int firstCol, int endCol) int firstCol, int endCol)
{ {
DataValidationHelper helper = sheet.getDataValidationHelper(); DataValidationHelper helper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = textlist.length > 0 ? helper.createExplicitListConstraint(textlist) : helper.createCustomConstraint("DD1"); DataValidationConstraint constraint = textlist.length > 0 ? helper.createExplicitListConstraint(textlist) : helper.createCustomConstraint("DD1");
@ -1018,7 +1084,7 @@ public class ExcelUtil<T>
/** /**
* ,. * ,.
* *
* @param sheet sheet. * @param sheet sheet.
* @param textlist * @param textlist
* @param promptContent * @param promptContent
@ -1070,7 +1136,7 @@ public class ExcelUtil<T>
/** /**
* 0=,1=,2= * 0=,1=,2=
* *
* @param propertyValue * @param propertyValue
* @param converterExp * @param converterExp
* @param separator * @param separator
@ -1107,7 +1173,7 @@ public class ExcelUtil<T>
/** /**
* =0,=1,=2 * =0,=1,=2
* *
* @param propertyValue * @param propertyValue
* @param converterExp * @param converterExp
* @param separator * @param separator
@ -1276,7 +1342,7 @@ public class ExcelUtil<T>
*/ */
public List<Object[]> getFields() public List<Object[]> getFields()
{ {
List<Object[]> fields = new ArrayList<Object[]>(); List<Object[]> fields = new ArrayList<>();
List<Field> tempFields = new ArrayList<>(); List<Field> tempFields = new ArrayList<>();
tempFields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields())); tempFields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));
tempFields.addAll(Arrays.asList(clazz.getDeclaredFields())); tempFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
@ -1284,36 +1350,38 @@ public class ExcelUtil<T>
{ {
if (!ArrayUtils.contains(this.excludeFields, field.getName())) if (!ArrayUtils.contains(this.excludeFields, field.getName()))
{ {
// 单注解 if (this.showFields != null && this.showFields.length != 0 && ArrayUtils.contains(this.showFields,field.getName())) {
if (field.isAnnotationPresent(Excel.class)) // 单注解
{ if (field.isAnnotationPresent(Excel.class))
Excel attr = field.getAnnotation(Excel.class);
if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
{
field.setAccessible(true);
fields.add(new Object[] { field, attr });
}
if (Collection.class.isAssignableFrom(field.getType()))
{ {
subMethod = getSubMethod(field.getName(), clazz); Excel attr = field.getAnnotation(Excel.class);
ParameterizedType pt = (ParameterizedType) field.getGenericType(); if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0]; {
this.subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class); field.setAccessible(true);
fields.add(new Object[] { field, attr });
}
if (Collection.class.isAssignableFrom(field.getType()))
{
subMethod = getSubMethod(field.getName(), clazz);
ParameterizedType pt = (ParameterizedType) field.getGenericType();
Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0];
this.subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class);
}
} }
}
// 多注解 // 多注解
if (field.isAnnotationPresent(Excels.class)) if (field.isAnnotationPresent(Excels.class))
{
Excels attrs = field.getAnnotation(Excels.class);
Excel[] excels = attrs.value();
for (Excel attr : excels)
{ {
if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr()) Excels attrs = field.getAnnotation(Excels.class);
&& (attr != null && (attr.type() == Type.ALL || attr.type() == type))) Excel[] excels = attrs.value();
for (Excel attr : excels)
{ {
field.setAccessible(true); if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr())
fields.add(new Object[] { field, attr }); && (attr != null && (attr.type() == Type.ALL || attr.type() == type)))
{
field.setAccessible(true);
fields.add(new Object[] { field, attr });
}
} }
} }
} }
@ -1349,7 +1417,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param sheetNo sheet * @param sheetNo sheet
* @param index * @param index
*/ */
@ -1366,7 +1434,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param row * @param row
* @param column * @param column
* @return * @return
@ -1426,7 +1494,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param row * @param row
* @return * @return
*/ */
@ -1449,7 +1517,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param dateFormat * @param dateFormat
* @param val * @param val
* @return * @return
@ -1515,7 +1583,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param name * @param name
* @param pojoClass * @param pojoClass
* @return * @return

Loading…
Cancel
Save