easypoi导出Excel遇到的坑


当对象属性名称第二个字母为大写时会导致导出对应属性数据为空情况。

举例:public String aBTest;

easypoi通过PoiReflectorUtil.class工具类(具体调用的方法见下面的代码)依据对象的方法名获取其对应的属性,结果为:ABTest,与实际属性不一致(实际属性应为:aBTest),所以会导致写入Excel文件数据为空。

导致对应属性导出为空的原因:

easypoi在处理属性时判断不够全面,使用时需要注意

判断逻辑:

(
name.length() == 1 || name.length() > 1 && !Character.isUpperCase(name.charAt(1))
)
出现问题的具体方法见下面代码:
PoiReflectorUtil.class
private static String methodToProperty(String name) {
if (name.startsWith("is")) {
name = name.substring(2);
} else {
if (!name.startsWith("get") && !name.startsWith("set")) {
throw new RuntimeException("Error parsing property name '" + name + "'. Didn't start with 'is', 'get' or 'set'.");
}

name = name.substring(3);
}

if (name.length() == 1 || name.length() > 1 && !Character.isUpperCase(name.charAt(1))) {
name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
}

return name;
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM