我的 Java REST 服务(POST 调用)正在使用 hssfWorkbook 提供 excel 文件作为响应,并以 xls 格式返回 excel。
response.getOutputStream();
hssfWorkbook.write(out);
我已经尝试过 Filesaver,但它适用于 JSON 作为响应。我没有找到任何方法在 Angular JS 中实现 Excel 文件下载。请建议使用 Angular JS 的任何方法。
最佳答案
试试这个,它会起作用。
API
httpServletResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
httpServletResponse.setHeader("Content-Disposition",
"attachment; filename=sample.xlsx");
workbook.write(httpServletResponse.getOutputStream());
workbook.close();
httpServletResponse.getOutputStream().close();
$http 调用
$scope.download = function() {
$http({
url: '/download',
method: "POST",
data: $scope.pagination,
headers: {
'Content-type': 'application/json'
},
responseType: 'arraybuffer'
}).success(function(data, status, headers, config) {
var blob = new Blob([data], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
});
saveAs(blob, "Sales_Summary_Report.xls");
}).error(function(data, status, headers, config) {
});
}
HTML
<button ng-click="download()"> Download Excel </button>
关于javascript - 如何使用 Angular JS 下载 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40304097/