【发布时间】:2020-06-22 14:16:44
【问题描述】:
我想实现这个用于从 Spring BE 导出数据的代码示例。
@GetMapping("export/{ids}")
public void export(HttpServletResponse response, @PathVariable List<Integer> ids) {
List<Transactions> transactions = (List<Transactions>) transactionService.findAll(ids);
List<TransactionExcelEntry> entries = transactions.stream().map(payment_transaction_mapper::toExcel).collect(Collectors.toList());
List<String> headers = Arrays.asList("Id", "Name", "Type", "Created at");
try {
response.addHeader("Content-disposition", "attachment; filename=Transactions.xlsx");
response.setContentType("application/vnd.ms-excel");
new SimpleExporter().gridExport(headers, entries, "id, name", response.getOutputStream());
response.flushBuffer();
} catch (IOException ex) {
LOG.debug("parsing of transactions failed");
}
}
导出按钮:
<button class="dropdown-item" (click)="export()">XLS</button>
导出功能:
export() {
var newPagination = new Pagination();
newPagination.size = this.pagination.size * this.pagination.total
this.transactionService.search(newPagination, this.formGroup.value)
.subscribe(result => {
this.formGroup.enable();
const query = result.content.map(t => t.id).join(',');
this.transactionService.exportRows(query).subscribe(data => {
const a = document.createElement('a');
a.href = window.URL.createObjectURL(data);
a.download = 'export.xls';
a.click();
});
}, (error) => {
this.formGroup.enable();
});
}
exportRows(query) {
return this.http.get(`/api/transactions/export`, { responseType: 'blob' });
}
我想将文件名生成到 Java BE 中并从 Angular FE 下载。如何实现此功能?
【问题讨论】:
-
澄清一下:您的意思是想让 Angular 自动下载名称为
Transactions.xlsx的文件,因为它是后端设置的文件吗? -
正确。这个想法是 BE 应该使用当前时间戳设置名称。
-
Thierry 的回答有什么问题?
标签: angular spring angular7 angular8