Pre Merge pull request !376 from lianghengyuan/ExcelUtil

This commit is contained in:
lianghengyuan
2024-11-06 07:55:03 +00:00
committed by Gitee

View File

@@ -172,6 +172,11 @@ public class ExcelUtil<T>
*/
public String[] excludeFields;
/**
* 需要排除列属性
*/
private String[] showFields;
public ExcelUtil(Class<T> clazz)
{
this.clazz = clazz;
@@ -445,6 +450,67 @@ public class ExcelUtil<T>
}
return list;
}
/**
* 对list数据源将其里面的数据导入到excel表单
*
* @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);
}
/**
* 对list数据源将其里面的数据导入到excel表单
*
* @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);
}
/**
* 对list数据源将其里面的数据导入到excel表单
*
* @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);
}
/**
* 对list数据源将其里面的数据导入到excel表单
*
* @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);
}
/**
* 对list数据源将其里面的数据导入到excel表单
@@ -991,7 +1057,7 @@ public class ExcelUtil<T>
* @param endCol 结束列
*/
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();
DataValidationConstraint constraint = textlist.length > 0 ? helper.createExplicitListConstraint(textlist) : helper.createCustomConstraint("DD1");
@@ -1276,7 +1342,7 @@ public class ExcelUtil<T>
*/
public List<Object[]> getFields()
{
List<Object[]> fields = new ArrayList<Object[]>();
List<Object[]> fields = new ArrayList<>();
List<Field> tempFields = new ArrayList<>();
tempFields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));
tempFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
@@ -1284,37 +1350,39 @@ public class ExcelUtil<T>
{
if (!ArrayUtils.contains(this.excludeFields, field.getName()))
{
// 单注解
if (field.isAnnotationPresent(Excel.class))
{
Excel attr = field.getAnnotation(Excel.class);
if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
if (this.showFields != null && this.showFields.length != 0 && ArrayUtils.contains(this.showFields,field.getName())) {
// 单注解
if (field.isAnnotationPresent(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))
{
Excels attrs = field.getAnnotation(Excels.class);
Excel[] excels = attrs.value();
for (Excel attr : excels)
{
if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr())
&& (attr != null && (attr.type() == Type.ALL || attr.type() == type)))
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);
ParameterizedType pt = (ParameterizedType) field.getGenericType();
Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0];
this.subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.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())
&& (attr != null && (attr.type() == Type.ALL || attr.type() == type)))
{
field.setAccessible(true);
fields.add(new Object[] { field, attr });
}
}
}
}
}