Java Angular JS-Excel文件下载不起作用
我可以下载excel文件,但我无法打开该文件,错误如下 “文件格式和扩展名不匹配。文件可能已损坏或不安全” 下载的excel文件大小为185kb。 响应来自服务器,没有任何问题。这是我的密码Java Angular JS-Excel文件下载不起作用,java,angularjs,spring,Java,Angularjs,Spring,我可以下载excel文件,但我无法打开该文件,错误如下 “文件格式和扩展名不匹配。文件可能已损坏或不安全” 下载的excel文件大小为185kb。 响应来自服务器,没有任何问题。这是我的密码 $scope.downloadExcel = function () { var searchSurvey = ((typeof($scope.searchSurveyFilterObj)!=undefined) && $scope.searchSurvey
$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);
})
};
$scope.downloadExcel=函数(){
var searchSurvey=(typeof($scope.searchSurveyFilterObj)!=未定义)和&$scope.searchSurveyFilterObj!=null)?$scope.searchSurveyFilterObj:null;
var divisionFilter=(searchSurvey!=null&&typeof(searchSurvey.divisionLookupId)!=未定义)?searchSurvey.divisionLookupId:null;
var statusFilter=(searchSurvey!=null&&typeof(searchSurvey.currStatusLookupId)!=undefined)?searchSurvey.currStatusLookupId:null;
var divisionValue=$scope.searchSurveyFilterObj.divisionLookupId;
console.log(“分区值::”+分区值);
var statusValue=$scope.searchSurveyFilterObj.currStatusLookupId;
console.log(“状态值::”+statusValue);
downloadExcelUrl=downloadExcelUrl+'?'+(divisionFilter!=null?('&division='+divisionFilter):'')
+(statusFilter!=null?(“&status=”+statusFilter):”);
$http.post(下载ExcelURL,{responseType:'arraybuffer'}
).然后(功能(响应){
var header=response.headers('Content-disposition')
var fileName=header.split(“=”[1]。替换(/\”/gi,”);
log(文件名);
var blob=新blob([response.data],
{type:'application/vnd.openxmlformats officedocument.spreadsheetml.sheet'});
var objectUrl=(window.URL | | window.webkitURL).createObjectURL(blob);
变量链接=角度元素(“”);
link.attr({
href:objectUrl,
下载:文件名
})[0]。单击();
//window.open(objectUrl);
})
};
Java代码
@RequestMapping(value=Constants.URI\u EXCEL,method=RequestMethod.POST)
@应答器
公共字节[]getExcel(@RequestParam(value=“division”,required=false,defaultValue=“”)字符串分割,
@RequestParam(value=“status”,required=false,defaultValue=“”)字符串状态,
HttpServletResponse(响应)引发IOException
{
SearchCriteriaDto SearchCriteriaDto=新的SearchCriteriaDto();
搜索标准到setDivision(division);
搜索标准至设置状态(状态);
ReportUtil ReportUtil=new ReportUtil();
XSSF工作簿=空;
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
response.setHeader(“内容处置”、“附件;文件名=“+”项目_Details.xls”);
setContentType(“application/vnd.openxmlformats of icedocument.spreadsheetml.sheet”);
List projectListDetails=null;
projectListDetails=adminService.getProjectDetailForDownload(searchcriteriaDto);
工作簿=reportUtil.generateProjectListingReport(projectListDetails);
工作簿。编写(bos);
response.getOutputStream().write(bos.toByteArray());
response.getOutputStream().flush();
response.getOutputStream().close();
返回bos.toByteArray();
}
任何帮助都将不胜感激
@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();
}