@Override
public void accountStatCollectExport(AccountStatPageListVO accountStatPageListVO, HttpServletResponse response) throws Exception {
//应收项目
R<List<BilledProject>> billedProjectListByTenantId = smartPropertyClient.getBilledProjectListByTenantId(accountStatPageListVO.getTenantId());
List<BilledProject> accountReceivableData = billedProjectListByTenantId.getData();
//实收项目
R<List<ProjectRealIncome>> projectRealIncomeByTenantId = smartPropertyClient.getProjectRealIncomeByTenantId(accountStatPageListVO.getTenantId());
List<ProjectRealIncome> actualData = projectRealIncomeByTenantId.getData();
String fileName = "汇总合同账单统计"; // 设置导出的文件名
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
//插入的数据
List<List<Object>> contentList = Lists.newArrayList();
List<AccountStatPageListRespVO> dataList = accountStatCollectList(accountStatPageListVO);
for (AccountStatPageListRespVO vo : dataList) {
ArrayList<Object> data = new ArrayList<>();
data.add(vo.getAccountSubject());
data.add(vo.getLeasee());
data.add(vo.getElectricOpenBalance());
data.add(vo.getRentOpenBalance());
data.add(vo.getOpenBalance());
List<ReceivableStat> receivableStat = vo.getReceivableStat();
for (ReceivableStat stat : receivableStat) {
data.add(stat.getFeeBillTotalAmt());
}
data.add(vo.getReceivableAmt());
List<ReceiveStat> receiveStat = vo.getReceiveStat();
for (ReceiveStat stat : receiveStat) {
data.add(stat.getReceivedTotalAmt());
}
data.add(vo.getReceiveAmt());
data.add(vo.getElectricCloseBalance());
data.add(vo.getRentLoseBalance());
data.add(vo.getCloseBalance());
contentList.add(data);
}
// 设置导出的表头结构
List<List<String>> head = createCollectHead(accountReceivableData, actualData);
MyEasyExcelUtil.download(response, fileName, head, contentList);
}
private List<List<String>> createCollectHead(List<BilledProject> billedProjects,
List<ProjectRealIncome> projectRealIncomes) {
List<List<String>> head = new ArrayList<>();
String should = "本期借方(应收)", borrow = "本期贷方(收回)";
//第一列,1/2行
head.add(Lists.newArrayList("合同号", "合同号"));
//第二列,1/2行
head.add(Lists.newArrayList("承租方", "承租方"));
head.add(Lists.newArrayList("期初余额", "期初余额", "电费"));
head.add(Lists.newArrayList("期初余额", "期初余额", "房租"));
head.add(Lists.newArrayList("期初余额", "期初余额", "期初余额"));
List<String> list = new ArrayList<>();
for (BilledProject project : billedProjects) {
list.add(project.getName());
}
list.forEach(title -> {
head.add(Lists.newArrayList(should, should, title));
});
head.add(Lists.newArrayList(should, should, "小计"));
List<String> list2 = new ArrayList<>();
for (ProjectRealIncome project : projectRealIncomes) {
list2.add(project.getName());
}
list2.forEach(title -> {
head.add(Lists.newArrayList(borrow, borrow, title));
});
head.add(Lists.newArrayList(borrow, borrow, "小计"));
head.add(Lists.newArrayList("本期余额", "本期余额", "智能电费"));
head.add(Lists.newArrayList("本期余额", "本期余额", "房租等"));
head.add(Lists.newArrayList("本期余额", "本期余额", "期末余额"));
return head;
}
/**
* 导出execl
*
* @param response 输出流
* @param fileName 文件名
* @param headList 表头
* @param data 数据
* @throws IOException
*/
public static void download(HttpServletResponse response, String fileName, List<List<String>> headList, List data) throws IOException {
String excelName = StringUtil.isNotBlank(fileName) ? fileName : "" + DateUtil.format(new java.util.Date(), "yyyy-MM");
response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
excelName = URLEncoder.encode(excelName, "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + excelName + ".xls");
EasyExcel.write(response.getOutputStream()).head(headList).excelType(ExcelTypeEnum.XLS).sheet("sheet1").doWrite(data);
}
}