Angular JS - Excel文件下载不起作用

时间:2016-12-29 13:24:51

标签: java angularjs spring

我可以下载excel文件,但我无法打开文件,收到以下错误

"文件格式和扩展名不匹配。该文件可能已损坏或不安全"

下载的excel文件大小为185kb。 响应来自服务器,没有任何问题。这是我的代码

    $scope.downloadExcel = function () {

            var searchSurvey = ((typeof($scope.searchSurveyFilterObj)!=undefined) && $scope.searchSurveyFilterObj!=null)?$scope.searchSurveyFilterObj:null;
            var divisionFilter  = (searchSurvey!=null && typeof(searchSurvey.divisionLookupId)!=undefined)?searchSurvey.divisionLookupId:null;
            var statusFilter    = (searchSurvey!=null && typeof(searchSurvey.currStatusLookupId)!=undefined)?searchSurvey.currStatusLookupId:null;

            var divisionValue = $scope.searchSurveyFilterObj.divisionLookupId;
            console.log("Division value :::" + divisionValue);
            var statusValue = $scope.searchSurveyFilterObj.currStatusLookupId;
            console.log("Status value :::" + statusValue);
            downloadExcelUrl = downloadExcelUrl + '?'+ (divisionFilter!=null?('&division='+divisionFilter):'')
            + (statusFilter!=null?('&status='+statusFilter):'');

            $http.post(downloadExcelUrl,{responseType: 'arraybuffer'}
            ).then(function (response) {
                    var header = response.headers('Content-disposition')
                    var fileName = header.split("=")[1].replace(/\"/gi,'');
                    console.log(fileName);

                    var blob = new Blob([response.data],
                        {type : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
                    var objectUrl = (window.URL || window.webkitURL).createObjectURL(blob);
                    var link = angular.element('<a/>');
                    link.attr({
                        href : objectUrl,
                        download : fileName
                    })[0].click();
                    //window.open(objectUrl);
                })
        };

Java代码

     @RequestMapping(value = Constants.URI_EXCEL, method = RequestMethod.POST)
@ResponseBody
public byte[] getExcel(@RequestParam(value="division", required=false, defaultValue="") String division,
        @RequestParam(value="status", required=false, defaultValue="") String status,
        HttpServletResponse response) throws IOException  
{  
    SearchCriteriaDto searchcriteriaDto = new SearchCriteriaDto();
    searchcriteriaDto.setDivision(division);
    searchcriteriaDto.setStatus(status);
    ReportUtil reportUtil = new ReportUtil();
    XSSFWorkbook workbook = null;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
     response.setHeader("Content-disposition","attachement;filename=" +  "Project_Details.xls");
     response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
     List<ProjectDetailDto> projectListDetails = null;
        projectListDetails = adminService.getProjectDetailForDownload(searchcriteriaDto);
     workbook = reportUtil.generateProjectListingReport(projectListDetails);
     workbook.write(bos);
     response.getOutputStream().write(bos.toByteArray());
     response.getOutputStream().flush();
     response.getOutputStream().close();
    return bos.toByteArray();
}

非常感谢任何帮助。

0 个答案:

没有答案