gpt4 book ai didi

javascript - 将 Exel 响应从 JAX-RS 发送到 AngularJS

转载 作者:行者123 更新时间:2023-11-30 06:20:06 24 4
gpt4 key购买 nike

我正在尝试在我的 JAX-RS 服务中使用 apache-poi 生成 Exel,并将 application/vnd.ms-excel 响应发送到 angularJs 应用程序。文件下载成功,但里面的数据被编码。


@POST
@Path("getExelStreamDocDB")
@Produces("application/vnd.ms-excel")
public Response getExelStreamDoc(String req) {
File file = new File("Excel.xls");
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("report");
for (int i = 0; i < 6; i++) {
Row newRow = sheet.createRow(i);
for (int j = 0; j < 1; j++) {
newRow.createCell(j).setCellValue("Ankush");
}
}
for(int i = 0; i < 1; i++) {
sheet.autoSizeColumn(i);
}

FileOutputStream fos=null;

try {
fos = new FileOutputStream(file);
workbook.write(fos);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}

ResponseBuilder response = Response.ok((Object)file);
response.header("Content-Disposition","attachment; filename=poi-generated-file.xlsx");
return response.build();
}

AngularJS

$scope.downloadExcel = function() {
exelService.getExcel({}).then(function(data) {
console.log(data);
var blob = new Blob([data], {type: "application/vnd.ms-excel"});
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl);

})

在使用 SOAP UI 测试服务时,我得到以下响应

<data contentType="application/vnd.ms-excel" contentLength="3368">UEsDBBQACAgIAJVbi00AAAAAAAAAAAAAAAALAAAAX3JlbHMvLnJlbHOtksFqwzAMhl/F6N447WCMUbeXMuhtjO4BNFtJTGLL2NqWvf3MLltLChvsKCR9/wfSdj+HSb1RLp6jP3XTgqJo2fnYG3g+PazuQBXB6HDiSAYiw363faIJpW6UwaeiKiIWA4NIute62D8CloYTxdrpOAeUWuZeJ7Qj9qQ3bXur808GnDPV0RnIR7cGdcLckxiYJ/3OeXxhHpuKrT8/RL8J5a7zlg5sXwNFWci+mAC97LL5dnFsHzPXTUzpv2VoFoqO3CrVBMriqVwzulkwspzpb0rXP6IDCToU/KJeCOmzH9h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3iRtlY/Y7lBmi1tN/KTroxBsN6jlhp6RqPTQoU/4syeVxlsNP2v1u9gfK3ZD1BLBwggYRwlXgEAAOECAABQSwECFAAUAAgICACVW4tNp4x6veMAAABJAgAACwAAAAAAAAAAAAAAAAAAAAAAX3JlbHMvLnJlbHNQSwECFAAUAAgICACVW4tNepTKcTsBAAAcBAAAEwAAAAAAAAAAAAAAAAAcAQAAW0NvbnRlbnRfVHlwZXNdLnhtbFBLAQIUABQACAgIAJVbi002boMhkwAAALgAAAAQAAAAAAAAAAAAAAAAAJgCAABkb2NQcm9wcy9hcHAueG1sUEsBAhQAFAAICAgAlVuLTYQ1P6YFAQAAsAEAABEAAAAAAAAAAAAAAAAAaQMAAGRvY1Byb3BzL2NvcmUueG1sUEsBAhQAFAAICAgAlVuLTW+4538/AAAApQAAABQAAAAAAAAAAAAAAAAArQQAAHhsL3NoYXJlZFN0cmluZ3MueG1sUEsBAhQAFAAICAgAlVuLTa6Rk9ZFAQAAowIAAA0AAAAAAAAAAAAAAAAAfAUAAHhsL3N0eWxlcy54bWxQSwECFAAUAAgICACVW4tNeD+yMtoAAABeAQAADwAAAAAAAAAAAAAAAAD8BgAAeGwvd29ya2Jvb2sueG1sUEsBAhQAFAAICAgAlVuLTYYDO5HUAAAAMwIAABoAAAAAAAAAAAAAAAAAEwgAAHhsL19yZWxzL3dvcmtib29rLnhtbC5yZWxzUEsBAhQAFAAICAgAlVuLTSBhHCVeAQAA4QIAABgAAAAAAAAAAAAAAAAALwkAAHhsL3dvcmtzaGVldHMvc2hlZXQxLnhtbFBLBQYAAAAACQAJAD8CAADTCgAAAAA=</data>

最佳答案

你能试试这个……吗?

      $scope.downloadExcel = function() {
exelService.getExcel({}).then(function(data) {
var data = window.atob(data);
var buf = new ArrayBuffer(data.length * 2);
var bufView = new Uint8Array(buf);
var file;

for (var i = 0, strLen = data.length; i < strLen; i++) {
bufView[i] = data.charCodeAt(i);
}

file = new Blob([buf], { type: 'application/vnd.ms-excel' });
this.fileURL = URL.createObjectURL(file);
window.open(this.fileURL);
})

关于javascript - 将 Exel 响应从 JAX-RS 发送到 AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53717873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com