前端按钮chick()事件js模仿a标签点击
function dakai(){
window.open('localhost:8080/project/excelDown');
}
后端
@RequestMapping(value = "/excelDown", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
public void createExcelToDisk(HttpServletResponse response,DetailReportQuery dr) {
try {
response.reset();
String fname = "未命名";
String houzui = ".xlsx";
XSSFWorkbook workbook = null;
switch (dr.getId()){
case 1:
fname = "屋明细账"+System.currentTimeMillis()+houzui;
workbook = detailReportService.excelExportStoried(dr);
break;
case 2:
fname = "物明细账"+System.currentTimeMillis()+houzui;
workbook = detailReportService.excelExportStructure(dr);
break;
case 3:
fname = "地明细账"+System.currentTimeMillis()+houzui;
workbook = detailReportService.excelExportGround(dr);
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
}
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fname.getBytes("gb2312"), "ISO8859-1"));
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
OutputStream output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
bufferedOutPut.flush();
workbook.write(bufferedOutPut);
bufferedOutPut.close();
}catch (Exception e){
e.getMessage();
}
}
防止乱码2
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("自动统计导出", "UTF-8")
.replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" +
fileName + ".xlsx");