改造这段代码,使其能下载主数据模型对应的主数据包含在下载的excel中 @Operation(summary = "下载导入主数据的excel模板", description = "") @GetMapping("/download-excel-template") public ResponseEntity<Resource> downloadExcelTemplate(@RequestParam(name = "logicModelId", required = true) Long logicModelId) throws BizBaseException { LoginUserBO loginUser = ContextUtil.getLoginUser(); OperationInfo operationInfo = OperationInfoBuilder.build(loginUser); MasterdataModelBO masterdataModelBO = masterdataModelService.getMasterdataModelById(ShardingKey.ofContext(), logicModelId); // 创建 Excel 文件 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("sheet1"); // 添加字段 List<LogicalModelColumnBO> fieldInfos = masterdataModelBO.getFieldInfos(); if (fieldInfos != null && !fieldInfos.isEmpty()) { Row headerRow = sheet.createRow(0); for (int i = 0; i < fieldInfos.size(); i++) { LogicalModelColumnBO field = fieldInfos.get(i); if (ignoreField(field.getName())) continue; Cell headerCell = headerRow.createCell(i); headerCell.setCellValue(field.getCname()); } } // 写入到文件流中 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { workbook.write(outputStream); } catch (IOException e) { e.printStackTrace(); } // 构造下载文件的响应头 HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=template.xlsx"); // 将字节流封装成 Resource 对象 ByteArrayResource resource = new ByteArrayResource(outputStream.toByteArray()); // 返回文件下载响应 return ResponseEntity.ok() .headers(headers) .contentType(MediaType.parseMediaType("application/vnd.ms-excel")) .contentLength(resource.contentLength()) .body(resource); }
时间: 2023-06-24 07:06:44 浏览: 155
改进的代码如下所示:
```java
@Operation(summary = "下载导入主数据的excel模板", description = "")
@GetMapping("/download-excel-template")
public ResponseEntity<Resource> downloadExcelTemplate(@RequestParam(name = "logicModelId", required = true) Long logicModelId) throws BizBaseException {
LoginUserBO loginUser = ContextUtil.getLoginUser();
OperationInfo operationInfo = OperationInfoBuilder.build(loginUser);
// 获取主数据模型信息
MasterdataModelBO masterdataModelBO = masterdataModelService.getMasterdataModelById(ShardingKey.ofContext(), logicModelId);
// 创建 Excel 文件
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("sheet1");
// 添加字段
List<LogicalModelColumnBO> fieldInfos = masterdataModelBO.getFieldInfos();
if (fieldInfos != null && !fieldInfos.isEmpty()) {
Row headerRow = sheet.createRow(0);
for (int i = 0; i < fieldInfos.size(); i++) {
LogicalModelColumnBO field = fieldInfos.get(i);
if (ignoreField(field.getName())) continue;
Cell headerCell = headerRow.createCell(i);
headerCell.setCellValue(field.getCname());
}
}
// 添加主数据
List<MasterdataBO> masterdataList = masterdataService.getMasterdataListByModelId(ShardingKey.ofContext(), logicModelId);
if (masterdataList != null && !masterdataList.isEmpty()) {
int rowIndex = 1;
for (MasterdataBO masterdata : masterdataList) {
Row dataRow = sheet.createRow(rowIndex++);
for (int i = 0; i < fieldInfos.size(); i++) {
LogicalModelColumnBO field = fieldInfos.get(i);
if (ignoreField(field.getName())) continue;
Cell dataCell = dataRow.createCell(i);
Object value = masterdata.getAttrValue(field.getName());
if (value != null) {
if (value instanceof Date) {
dataCell.setCellValue((Date) value);
} else {
dataCell.setCellValue(value.toString());
}
}
}
}
}
// 写入到文件流中
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
// 构造下载文件的响应头
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=template.xlsx");
// 将字节流封装成 Resource 对象
ByteArrayResource resource = new ByteArrayResource(outputStream.toByteArray());
// 返回文件下载响应
return ResponseEntity.ok()
.headers(headers)
.contentType(MediaType.parseMediaType("application/vnd.ms-excel"))
.contentLength(resource.contentLength())
.body(resource);
}
```
修改后的代码在原有代码的基础上,增加了添加主数据的部分。首先获取主数据模型信息,然后获取该模型对应的主数据列表,按照字段信息的顺序将主数据添加到 Excel 文件中。最终返回文件下载响应。
阅读全文