[英]Downloading an excel file in angular client (angular v5.2) received as a response from WebAPI
[英]Returning Excel file in POST request response and Downloading it on client side using Angular 5
我需要使用Spring boot創建一個excel文件(.xlsx),並發送它以響應Angular前端所做的POST調用,然后通過立即下載文件來處理客戶端的響應。
我該如何實現?
還是發送JSON並在Angular端使用XLSX之類的東西來生成excel文件更好?
編輯1:
因此,我能夠創建一個excel文件,並以byteArray形式發送它作為響應,如下所示:
@PostMapping("/excel")
@CrossOrigin("*")
public ResponseEntity<byte[]> fetchFile(@RequestBody Employee employee) throws IOException, InvalidFormatException {
ByteArrayOutputStream outputStream = excelService.getExcelSheet();
byte[] response = outputStream.toByteArray();
HttpHeaders headers = new HttpHeaders();
headers.setAccessControlExposeHeaders(Collections.singletonList("Content-Disposition"));
headers.set("Content-Disposition", "attachment; filename=employee.xlsx");
headers.setAccessControlExposeHeaders(Collections.singletonList("Content-Type"));
headers.set("Content-Type","application/vnd.ms-excel");
return new ResponseEntity<>(response, headers, HttpStatus.OK);
}
使用郵遞員下載文件時,東西很不錯。 但是,如何在Angular前端使用此文件並將其下載為.xlsx文件?
如果您是新手,可以使用apache poi庫來處理此問題,您可以在這里找到關於此的很好的教程: https : //www.mkyong.com/java/apache-poi-reading-and-writing-excel-file -在Java /
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.