若依树型报表Excel导出分列
分层树形页面Excel导出
·
Controller导出内容修改
/**
* 导出机构表列表
*/
@SaCheckPermission("bk:bkJgbTmp:export")
@Log(title = "机构表", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void exportTest(TBkJgbTmpBo bo, HttpServletResponse response) {
List<TDbdkKhbVo> dbdkKhbVoTotalList = itDbdkKhbService.queryList(new TDbdkKhbBo());
//使用stream遍历tDbdkKhbVos中sjbh为0的机构
List<TDbdkKhbVo> tDbdkKhbVoList = dbdkKhbVoTotalList.stream().filter(tDbdkKhbVo -> "0".equals(tDbdkKhbVo.getVcSjbh())).collect(Collectors.toList());
//循环遍历tDbdkKhbVoList,将每个机构的子机构放入到tDbdkKhbVoList中
List<TDbdkKhbVo> listResult = new ArrayList<>();
List<TDbdkKhbVo> jgbList = getJGBTree(tDbdkKhbVoList, dbdkKhbVoTotalList, listResult);
//查询客户表dbdkKhbVoTotalList的子集有多深
Integer depth = getDepth(dbdkKhbVoTotalList, "0");
String fileName = "D:\\test.xlsx";
EasyExcel.write(fileName).head(head(depth)).sheet("模板").doWrite(dataList(jgbList, depth));
}
private List<List<String>> head(Integer depth) {
List<List<String>> list = ListUtils.newArrayList();
List<String> head_1 = ListUtils.newArrayList();
head_1.add("序号");
List<String> head0 = ListUtils.newArrayList();
head0.add("数据1");
List<String> head1 = ListUtils.newArrayList();
head1.add("数据2");
List<String> head2 = ListUtils.newArrayList();
head2.add("数据3");
list.add(head_1);
for (int i = 1; i < depth; i++) {
List<String> headTemp = ListUtils.newArrayList();
headTemp.add("客户" + i);
list.add(headTemp);
}
list.add(head0);
list.add(head1);
list.add(head2);
return list;
}
private List<List<Object>> dataList(List<TDbdkKhbVo> jgbList, Integer depth) {
List<List<Object>> list = ListUtils.newArrayList();
for (int i = 0; i < jgbList.size(); i++) {
List<Object> data = ListUtils.newArrayList();
data.add(i + 1);
String[] jgbListStr = jgbList.get(i).getVcKhmc().split("@");
Collections.addAll(data, jgbListStr);
while (data.size() < depth)
{
data.add("");
}
data.add(0.56);
data.add(new Date());
list.add(data);
}
return list;
}
private List<TDbdkKhbVo> getJGBTree(List<TDbdkKhbVo> tDbdkKhbVos, List<TDbdkKhbVo> dbdkKhbVoTotalList, List<TDbdkKhbVo> listResult) {
for (TDbdkKhbVo tDbdkKhbVo : tDbdkKhbVos) {
listResult.add(tDbdkKhbVo);
//使用stream遍历tDbdkKhbVos中sjbh为tDbdkKhbVo.getVcJgh()的机构
List<TDbdkKhbVo> tDbdkKhbVoList1 = dbdkKhbVoTotalList.stream().filter(tDbdkKhbVo1 -> tDbdkKhbVo.getVcKhbh().equals(tDbdkKhbVo1.getVcSjbh()))
.map(item -> {
item.setVcKhmc(tDbdkKhbVo.getVcKhmc() + "@" + item.getVcKhmc()); // Modify the khmc value
return item;
})
.collect(Collectors.toList());
if (!tDbdkKhbVoList1.isEmpty()) {
getJGBTree(tDbdkKhbVoList1, dbdkKhbVoTotalList, listResult);
}
}
return listResult;
}
private Integer getDepth(List<TDbdkKhbVo> dbdkKhbVoTotalList, String sjbh) {
//递归查询客户表dbdkKhbVoTotalList的子集有多深
int max = 0;
List<TDbdkKhbVo> tDbdkKhbVoList = dbdkKhbVoTotalList.stream().filter(tDbdkKhbVo -> sjbh.equals(tDbdkKhbVo.getVcSjbh())).collect(Collectors.toList());
if (!tDbdkKhbVoList.isEmpty()) {
for (TDbdkKhbVo tDbdkKhbVo : tDbdkKhbVoList) {
int depth = getDepth(dbdkKhbVoTotalList, tDbdkKhbVo.getVcKhbh());
if (depth > max) {
max = depth;
}
}
}
return max + 1;
}
更多推荐
已为社区贡献1条内容
所有评论(0)