加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS+Jersey下载excel

发布时间:2020-12-17 09:36:44 所属栏目:安全 来源:网络整理
导读:AngularJS 代码: $scope .testDownload = function () { $http .post("rest/excel/down",$scope.req,{responseType: 'arraybuffer' }).success( function (data) { var blob = new Blob( [data] ,{type: "application/vnd.ms-excel" }); var fileName = "tt

AngularJS代码:

$scope.testDownload =function () {

$http.post("rest/excel/down",$scope.req,{responseType:'arraybuffer'}).success(function (data) {

varblob =new Blob([data],{type:"application/vnd.ms-excel"});

varfileName ="ttt2.xls";

vara = document.createElement("a");

document.body.appendChild(a);

a.download = fileName;

a.href = URL.createObjectURL(blob);

a.click();

}).error(function (data) {

$("#serverErrorModal").modal({show:true});

});

}

注意post的方法里要加responseType: 'arraybuffer'参数,不然下载的excel会乱码!!!

使用{type:"application/vnd.ms-excel"}的写法,可以保存为xls格式的excel文件(兼容老版本)。而使用“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”则会保存为xlsx。

Server端示例代码:

@POST

@Path("down")

@Produces(MediaType.APPLICATION_OCTET_STREAM)

publicbyte[] downloadExcel(@Context HttpServletResponse response)throws IOException {

//.....略

File file1= new File("D:/excel/ttt2.xls");

byte[] aa = Files.toByteArray(file1);

return aa;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读