前端:
JSP:
<div id="insertBtn" class="MyCssBtn leftBtn" onclick="download();"><span>测试下载</span></div>
JS:
function excelOut(){ var url = basePath+"/meals/excelOut"; //mybatis+spring url = encodeURI(url); location.href = url; }
后台:
Controllor:
@RequestMapping( value = "/excelOut") public void excelStandardTemplateOut(HttpServletRequest request, HttpServletResponse response) throws IOException{ URL save = Thread.currentThread().getContextClassLoader().getResource(""); String str = save.toString(); str=str.substring(6,str.length()); str=str.replaceAll("%20", " "); int num = str.lastIndexOf("wgbs"); //wgbs 为项目名,应用到不同的项目中,这个需要修改! str=str.substring(0, num+"wgbs".length()); str = str +"/demo/template.xlsx"; //Excel模板所在的路径。 File f = new File(str); // 设置response参数,可以打开下载页面 response.reset(); response.setContentType("application/vnd.ms-excel;charset=utf-8"); try { response.setHeader("Content-Disposition", "attachment;filename="+ new String(("泊位信息标准模板" + ".xlsx").getBytes(), "iso-8859-1")); //下载文件的名称 } catch (UnsupportedEncodingException e) { e.printStackTrace(); } ServletOutputStream out = response.getOutputStream(); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(new FileInputStream(f)); bos = new BufferedOutputStream(out); byte[] buff = new byte[2048]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } } catch (final IOException e) { throw e; } finally { if (bis != null) bis.close(); if (bos != null) bos.close(); } }
项目目录(注意模板所在路径位置):