一、需求
公司项目的一个功能,导出账户信息excel表格,不同的账户类型除开所要展示的固定表头字段,还要展示每个账户类型所有的特殊字段,并且将所有账户类型的数据全部展示在同一个的excel文件的不同sheet表单中,由于项目在内网系统开发,没有相关图片展示,需求和这篇博客相似—>需求详情。
二、代码实现
要实现具体功能,要确定表头,表数据
1、获取账户所特有的表头字段
项目中有张模板表单独存储各个账户类型所特有的字段
/**
* 根据账户类型代码和业务类型代码获取模板库中的key作为表头
* @param accountType 账户类型代码
* @param businessTypeCode 业务类型代码
* @return List<string>
*/
public List<String> getHeadKeys(String accountType, String businessTypeCode) {
//存放模板库中所有的key作为特殊表头
List<String> listKey = new ArrayList<>();
//根据账户类型代码和业务类型代码集合找到所有的模板
List<AccountAttributeDO> accountAttributeDOS = accountAttributeMapper.selectoneAccountAttribute(accountType, businessTypecode);
if(CollectionUtils.isEmpty(accountAttiibutedos)) {
return listkey;
}
try {
for (AccountAttributeDO accountAttributeDO : accountAttributeDOS) {
//表头字段存放在attributeModel字段中,采用json数组字符串方式存储,需要先转为json,其中indexName为表头名字
String attributeModel = accountAttributeDO.getAttributeModel();
JSONArray jsonArray = JSON.parseArray(attributeModel);
for (int i = 0; i < jsonArray.size(); i++) {
String indexName jsonArray.getJSONObject(i).get("indexName").toString();
if (!listKey.contains(indexName)) {
listKey.add(indexName);
}
}
}
} catch (Exception e) {
log.error("string数
本文记录了一个后端Java项目的需求,即导出包含不同账户类型信息的Excel表格,每个类型有固定字段和特殊字段。通过EasyExcel库,实现了获取特殊字段、拼接表头、数据提取和生成Excel的功能。
最低0.47元/天 解锁文章
556

被折叠的 条评论
为什么被折叠?



