1、将模板文件放到 resources 文件夹下
可以在resources下新建static文件夹放置模板文件
2、写读取模板文件和导出文件的接口
/**
* 下载导入模板
*/
@RequestMapping(value = "/downloadTemplate", method = RequestMethod.GET)
public void downloadTemplate(HttpServletResponse response) {
Resource resource = new ClassPathResource("static/导入模板.xls");
try (InputStream inputStream = resource.getInputStream(); OutputStream outputStream = response.getOutputStream()) {
response.setContentType("content-type:octet-stream;charset=UTF-8");
String fileName = URLEncoder.encode("导入模板", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
IOUtils.copy(inputStream, outputStream);
} catch (Exception e) {
logger.error("导入模板下载失败", e);
}
}
3、写前端导出入口
框架:element-ui + vue.js
<el-button style="margin-left: 10px;" size="small" type="success"
@click="downloadTemplate">模板下载
</el-button>
function downloadTemplate() {
window.open("/../downloadTemplate");
}
var vue = new Vue({
el : '#app',
...
methods: {
downloadTemplate: downloadTemplate
}
...
})