下载导入模板

简单描述:需求要求做下载Excel模板,在模板上填充数据,然后在做导入功能。

//html代码
<div class="btn-group">
<button class="btn sbold green" id="submitRelease" onclick="downloadTemplate();">
   <span class="ladda-label">下载模板</span>
</button>
</div> 

//js代码
function downloadTemplate() {
window.location.href = rootPath + "/vrxx/abc/downloadTemplate"
} 
//后台java代码
@ResponseBody
@RequestMapping("/downloadTemplate")
public void downloadTemplate(HttpServletResponse response, HttpSession session) {
try {
InputStream inputStream = (InputStream) this.getClass().getClassLoader().getResourceAsStream("excelexport/rights.xlsx");
response.setContentType("application/zip");
OutputStream out = response.getOutputStream();
response.setHeader("Content-Disposition", "attachment; filename=" + "base" + ".xlsx");
int b = 0;
byte[] buffer = new byte[1000000];
while (b != -1) {
b = inputStream.read(buffer);
if (b != -1) out.write(buffer, 0, b);
}
inputStream.close();
out.close();
out.flush();
} catch (IOException e) {
e.printStackTrace();
logger.error("模板下载失败");
}
}

 说明:首先,括号里的参数session,没有用到,可以去掉。

    其次,excelexport/rights.xlsx这个模板excel文件是存放在项目根目录的resources下

原文地址:https://www.cnblogs.com/xuchao0506/p/9994699.html