@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 17:06:46 浏览: 153
这段代码是一个下载 Excel 模板的接口,接口的请求路径为 "/download-excel-template",需要传递一个名为 "logicModelId" 的必填参数,表示要下载的 Excel 模板对应的逻辑模型 ID。接口响应的是一个 Excel 文件,响应头中包含了文件名和文件类型,文件内容是根据逻辑模型 ID 查询到的字段信息动态生成的。具体实现过程是:创建一个 Workbook 对象,然后根据逻辑模型 ID 查询到该模型对应的字段信息,根据字段信息动态生成 Excel 文件,最后将 Excel 文件转换成字节流并封装成 Resource 对象返回。
相关问题
改造这段代码,使其能下载主数据模型对应的主数据包含在下载的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); }
改进的代码如下所示:
```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 文件中。最终返回文件下载响应。
package bjtu.wms.wmstest.controller; import bjtu.wms.wmstest.entity.Orders; import bjtu.wms.wmstest.service.OrdersService; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; @RestController @RequestMapping("v1/orders") public class OrdersController { @Resource private OrdersService ordersService; @GetMapping("amount/{id}") public ResponseEntity<Orders> getOrderAmount(@PathVariable("id") Integer id) { return ResponseEntity.ok(this.ordersService.getOrderAmount(id)); } @GetMapping("state/{id}") public ResponseEntity<Orders> getOrderState(@PathVariable("id") Integer id) { return ResponseEntity.ok(this.ordersService.getOrderState(id)); } @PostMapping("/add") public ResponseEntity<Orders> add(@RequestBody Orders orders) { return ResponseEntity.ok(this.ordersService.insert(orders)); } }帮我写一个openapi document
好的,以下是根据你提供的代码编写的OpenAPI Document:
```yaml
openapi: "3.0.1"
info:
title: Orders API
version: 1.0.0
description: API for managing orders
paths:
/v1/orders/amount/{id}:
get:
summary: Get order amount by ID
parameters:
- in: path
name: id
required: true
description: Order ID
schema:
type: integer
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Orders'
/v1/orders/state/{id}:
get:
summary: Get order state by ID
parameters:
- in: path
name: id
required: true
description: Order ID
schema:
type: integer
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Orders'
/v1/orders/add:
post:
summary: Add new order
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Orders'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Orders'
components:
schemas:
Orders:
type: object
properties:
id:
type: integer
name:
type: string
amount:
type: number
state:
type: string
```
在上述文档中,我们定义了三个端点:
1. `/v1/orders/amount/{id}`:根据订单ID获取订单金额。
2. `/v1/orders/state/{id}`:根据订单ID获取订单状态。
3. `/v1/orders/add`:添加新订单。
对于每个端点,我们都定义了相应的HTTP方法、参数、请求体和响应。这些信息都包含在了OpenAPI Document中,可以用于生成API客户端、服务器存根和文档。
阅读全文