作者:影子张被占用 | 来源:互联网 | 2023-10-16 11:05
前端Angular的Post导出Excel,内容全部乱码(不是文件名),而Get方式导出没问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| $http({
url: "",
method: 'POST',
timeout: 60000,
headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf8'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data:{}
}).success(function (data) {
var blob = new Blob([data], {type: "application/vnd.ms-excel;charset=utf-8"}),
Temp = document.createElement("a");
Temp.href = window.URL.createObjectURL(blob);
Temp.download = "XXX.xlsx";
angular.element('body').append(Temp);
Temp.click();
}); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| @RequestMapping(value = "/downloadExcelTemplate.do")
public void downloadExcelTemplate(HttpServletRequest request, HttpServletResponse response) {
OutputStream os = null;
try {
XSSFWorkbook book = new XSSFWorkbook();
XSSFSheet sheet = book.createSheet("XXX");
XSSFRow rowTitle = sheet.createRow(0);
String fileName = "XXX"+"."+Constant.XLSX;
os = response.getOutputStream();
response.setCharacterEncoding(Constant.UTF);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, Constant.UTF));
book.write(os);
os.flush();
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
} finally {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} |