I want to implement this code example which is used to export data from 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");
}
}
Export button:
<button class="dropdown-item" (click)="export()">XLS</button>
Export functionality:
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' });
}
I want to generate the name of the file into the Java BE and download it from the Angular FE. How this functionality can be implemented?