poi导出动态行动态列_使用JavaModel时候怎么做动态列导出的实现

请详细描述您的问题。暂时先关闭问题

不过我已经在项目中用反射解决这问题了

`

/** 动态列实现代码,方案:通过获取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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值