因为jaxa的返回类型问题(text,json,xml),不能将文件从浏览器下载
所以需要在前台用form表单的方式提交
前台代码:
HTML部分: <form action="/marketOperation/reportStatistic/getExcel.do" method=post name=form1 style='display:none'> <input type="hidden" name="type" value="111"> </form>
JS部分:
document.form1.submit();
JAVA部分:
@PostMapping("/getExcel") @ResponseBody public void getExcel( @RequestParam String type,HttpServletResponse response) { Map map = new HashMap(); try{ String fileName="新闻.xls"; response.setContentType("application/excel"); response.setHeader("Content-disposition","attachment;filename=" +fileName +";filename*=utf-8''"+ URLEncoder.encode(fileName,"UTF-8")); // response.flushBuffer(); HSSFWorkbook workbook=new HSSFWorkbook(); HSSFSheet sheet=workbook.createSheet("sheet1"); int rowNum=0; //添加标题 String[] headers={"标题","板块","作者","创建时间","内容"}; HSSFRow row=sheet.createRow(rowNum); for (int i =0; i <headers.length;i++){ HSSFCell cell=row.createCell(i); HSSFRichTextString text=new HSSFRichTextString(headers[i]); cell.setCellValue(text); } //添加数据 for (int i = 0;i<5;i++) { rowNum++; HSSFRow row1=sheet.createRow(rowNum); row1.createCell(0).setCellValue("杀人啦"); row1.createCell(1).setCellValue(2); row1.createCell(2).setCellValue(3); row1.createCell(3).setCellValue(4); row1.createCell(4).setCellValue(5); } //logger.info("==============================="+"开始写入================"); workbook.write(response.getOutputStream()); }catch(Exception e){} }