[英]How to download excel using Angular JS
My Java REST service(POST call) is providing an excel file as response using hssfWorkbook and returning excel in xls format. 我的Java REST服务(POST调用)使用hssfWorkbook提供了一个excel文件作为响应,并以xls格式返回了excel。
response.getOutputStream();
hssfWorkbook.write(out);
I have tried Filesaver but it will work for JSON as reponse. 我已经尝试过Filesaver,但是它可以作为JSON响应。 I didn't find any way to implement excel file download in angular JS.
我没有找到任何方法在Angle JS中实现Excel文件下载。 Please suggest any ways to do using angular JS.
请提出使用角度JS的任何方法。
Try this it will work. 试试这个,它将起作用。
API 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 call $ 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 的HTML
<button ng-click="download()"> Download Excel </button>
For IE you can try msSaveOrOpenBlob
for other browsers hint with invisible anchor link should solve it, code like this works good for me: 对于IE,您可以尝试将
msSaveOrOpenBlob
用于其他浏览器,提示带有不可见的锚链msSaveOrOpenBlob
解决该问题,类似这样的代码对我msSaveOrOpenBlob
:
$http( /*your request details */ ).
success(function(data) {
var blob = new Blob([data], { type: 'application/vnd.ms-excel' }),
fileName = 'Sales_Summary_Report.xls';
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {
var anchor = angular.element('<a/>');
anchor.css({ display: 'none' });
angular.element(document.body).append(anchor);
anchor.attr({
href: (window.URL || window.webkitURL).createObjectURL(blob),
target: '_blank',
download: fileName
})[0].click();
anchor.remove();
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.