请详细描述您的问题。暂时先关闭问题
不过我已经在项目中用反射解决这问题了
`
/** 动态列实现代码,方案:通过获取easyexcel内的writeContext剔除不需要的字段列 **/
public static ExcelWriter modifyWriter(ExcelWriter writer, Sheet sheetParam, List noExportFields) throws
NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
if(null == noExportFields || noExportFields.isEmpty()){
return writer;
}
// 将不需要导出的字段名转成Sst集合
Set noExportFieldsSet = new HashSet(noExportFields);
//
Field excelBuilderField = writer.getClass().getDeclaredField("excelBuilder");
excelBuilderField.setAccessible(true);
ExcelBuilderImpl excelBuilderImplValObj = (ExcelBuilderImpl) excelBuilderField.get(writer);
Field contextField = excelBuilderImplValObj.getClass().getDeclaredField("context");
contextField.setAccessible(true);
WriteContext contextValObj = (WriteContext)contextField.get(excelBuilderImplValObj);
Field excelHeadPropertyFiled = contextValObj.getClass().getDeclaredField("excelHeadProperty");
excelHeadPropertyFiled.setAccessible(true);
Field workbookFiled = contextValObj.getClass().getDeclaredField("workbook");
workbookFiled.setAccessible(true);
Workbook workbookValObj = (Workbook) workbookFiled.get(contextValObj);
//
// 用反射实现 WriteContext 内 currentSheet方法逻辑,并加入剔除不需导出字段逻辑
if(null == contextValObj.getCurrentSheetParam()||
contextValObj.getCurrentSheetParam().getSheetNo() != sheetParam.getSheetNo()) {
// 调用 WriteContext 的cleanCurrentTable方法
ReflectUtils.invokeMethod(contextValObj, "cleanCurrentTable", null, null);
// 将sheetParam 赋值给 WriteContext 的 currentSheetParam 属性
contextValObj.setCurrentSheetParam(sheetParam);
// 初始化 WriteContext 的currentSheet属性
try {
contextValObj.setCurrentSheet(workbookValObj.getSheetAt(sheetParam.getSheetNo()-1));
} catch (Exception var3) {
contextValObj.setCurrentSheet( WorkBookUtil.createSheet(workbookValObj, sheetParam));
if (null != contextValObj.getAfterWriteHandler()) {
contextValObj.getAfterWriteHandler().sheet(sheetParam.getSheetNo(), contextValObj.getCurrentSheet());
}
}
StyleUtil.buildSheetStyle(contextValObj.getCurrentSheet(), sheetParam.getColumnWidthMap());
// 调用 WriteContext 的 initExcelHeadProperty 方法
ReflectUtils.invokeMethod(contextValObj, "initExcelHeadProperty", new Class[] {List.class, Class.class},
new Object[]{sheetParam.getHead(), sheetParam.getClazz()});
// 调用 WriteContext 的 initTableStyle 方法
ReflectUtils.invokeMethod(contextValObj, "initTableStyle", new Class[] {TableStyle.class},
new Object[]{sheetParam.getTableStyle()});
// 获取ExcelHeadProperty对象
ExcelHeadProperty excelHeadPropertyObj = (ExcelHeadProperty) excelHeadPropertyFiled.get(contextValObj);
Field columnPropertyListField = excelHeadPropertyObj.getClass().getDeclaredField("columnPropertyList");
columnPropertyListField.setAccessible(true);
Field headListField = excelHeadPropertyObj.getClass().getDeclaredField("head");
headListField.setAccessible(true);
List headListObj = (List) headListField.get(excelHeadPropertyObj);
List columnPropertyListObj = (List) columnPropertyListField.get(excelHeadPropertyObj);
Iterator it = columnPropertyListObj.iterator();
// 剔除不需要导出的字段
int index = 0;
while(it.hasNext()){
ExcelColumnProperty tmpObj = it.next();
if(noExportFieldsSet.contains(tmpObj.getField().getName())){
it.remove();
headListObj.remove(index);
index --;
} else {
index ++;
}
}
// 调用 WriteContext 内initTableHead方法 创建表头
ReflectUtils.invokeMethod(contextValObj, "initTableHead", null,null);
}
return writer;
}