public JsonResult exportContractInfo1(Pager<BasicContractInformationSearchRecultVo> pager, BasicContractInformationSearchVo searchVo,
HttpServletRequest request,
HttpServletResponse response) {
Pager<BasicContractInformationSearchRecultVo> search = search(pager, searchVo);
if (search.getDatas() == null || search.getDatas().size() == 0) {
return JsonResult.error("暂无信息");
}
List<KVEntity> kVlist = new ArrayList<>();
for (BasicContractInformationSearchRecultVo entity : search.getDatas()) {
LinkedHashMap<String, String> map = new LinkedHashMap<>();
// 将属性名和值--拼接为key:value
map.put("合同类型", entity.getTypeContractCode());
map.put("合同状态", entity.getContractStatusCode());
map.put("合同id", entity.getEntity().getId().toString());
map.put("项目id", String.valueOf(entity.getEntity().getProjectId()));
map.put("合同编号", entity.getEntity().getContractNo());
map.put("合同名", entity.getEntity().getContractName());
map.put("合同描述", entity.getEntity().getContractDescriPtione());
String msg = ResultStatus.getMsgByCode(entity.getEntity().getParty()).getMsg();
map.put(" 是否是甲方 1.是 2.否", String.valueOf(msg));
map.put("合同类型", entity.getEntity().getTypeContract().toString());
map.put("合同金额(元)", entity.getEntity().getContractAmount());
map.put("支出(元)", entity.getEntity().getExpenditure());
map.put("收入(元)", entity.getEntity().getIncome());
map.put("执行金额(元)", entity.getEntity().getExecutionAmount());
map.put("合同状态", String.valueOf(entity.getEntity().getContractStatus()));
map.put("甲方id", String.valueOf(entity.getEntity().getAInformationId()));
map.put("甲方信息", entity.getEntity().getAInformation());
map.put("乙方id", String.valueOf(entity.getEntity().getBInformationId()));
map.put("乙方信息", entity.getEntity().getBInformation());
String taxMsg = ResultStatus.getMsgByCode(entity.getEntity().getTaxexemption()).getMsg();
map.put("是否申请免税 1.是 2.否", taxMsg);
map.put("合同签订时间", String.valueOf(entity.getEntity().getContractSigningTime()));
map.put("合同到期时间", String.valueOf(entity.getEntity().getContractExpirationTime()));
map.put("合同超期", String.valueOf(entity.getEntity().getContractOverdue()));
map.put("合同余额预警", String.valueOf(entity.getEntity().getContractBalance()));
map.put("备注", entity.getEntity().getRemarks());
map.put("解约原因", entity.getEntity().getReasonTermination());
kVlist.add(new KVEntity().setKeyValue(JSON.toJSONString(map)));
}
String fileName = "合同管理信息表.xlsx";
response.reset();
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
//解决不同浏览器压缩包名字含有中文时乱码的问题
String agent = request.getHeader("USER-AGENT");
try {
if (agent.contains("MSIE") || agent.contains("Trident")) {
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
} else {
fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
}
} catch (Exception e) {
throw new BusinessException("转码异常");
}
response.setHeader("Content-Disposition",
String.format("attachment; filename=\"%s\"", fileName));
try {
EasyExcel.write(response.getOutputStream()).head(head(kVlist)).
sheet("模板").doWrite(dataList(kVlist));
} catch (IOException e) {
log.error("写入浏览器异常", e);
throw new BusinessException("写入浏览器异常");
}
return JsonResult.ok();
}
private List<List<String>> head(List<KVEntity> kVlist) {
List<List<String>> list = new ArrayList<>();
if (ObjectUtils.isEmpty(kVlist)) {
return list;
}
LinkedHashMap<String, String> map = JSON.parseObject(kVlist.get(0).getKeyValue(),
new TypeReference<LinkedHashMap<String, String>>() {
});
Set<String> headSet = map.keySet();
HashSet<String> hashSet2 = new HashSet<>(headSet);
for (String headCol : hashSet2) {
List<String> headColList = new ArrayList<>();
headColList.add(headCol);
list.add(headColList);
}
return list;
}
private List<List<Object>> dataList(List<KVEntity> kVlist) {
List<List<Object>> list = new ArrayList<>();
if (ObjectUtils.isEmpty(kVlist)) {
return list;
}
for (KVEntity KVEntity : kVlist) {
List<Object> data = new ArrayList<>();
Map<String, String> map = JSON.parseObject(KVEntity.getKeyValue(), new TypeReference<Map<String, String>>() {
});
for (String key : map.keySet()) {
data.add(map.get(key));
}
list.add(data);
}
return list;
}
2.实体类
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* @author TG
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class KVEntity {
/**
* 字段和值key;value对应
*/
private String keyValue;
}