前言:
项目架构:
前端框架:Angular6 + ng-zorro-antd
后端架构:SSM + Dubbo:springMVC + spring + myBatis-plus
接下来请跟随小编来看看下载excel模板是如何实现的呢?
正文:
前端
一。html,下载按钮实现
<button nz-button [ngStyle]="table_button_downTemplate" (click)="downTemplate()">下载模板</button>
二。component.ts,交互代码
downTemplate() {
const url = 'demo-web/foo/downFooTemplate';
const downURL = this.http.getserverUrl(url);
window.open(downURL);
URL.revokeObjectURL(downURL);
}
后端
一。核心代码实现
public boolean downTemplate(HttpServletResponse response) {
Map<Serializable, Serializable> map = new HashMap<>(16);
List<FooTemplate> templateList = new ArrayList<>();
FooTemplate fooTemplate = new FooTemplate();
fooTemplate.setUserCode("111111141021");
fooTemplate.setUserName("张三");
fooTemplate.setTelNum("1573262524913");
fooTemplate.setSex("女");
fooTemplate.setStartTime("2018/10/20");
fooTemplate.setFooGroupName("财务部");
templateList.add(fooTemplate);
//excel设置
map.put(SHEET_NAME, "用户信息");
map.put(COLUMNS, new String[]{"userCode", "userName", "telNum", "sex", "startTime", "fooGroupName"});
map.put(TITLE, "用户信息模板:入职日期格式需为日期格式");
map.put(NAME, "用户信息模板");
map.put(DATA_LIST, (Serializable) templateList);
try {
ExcelUtil.exportExcel(map, response);
return true;
}catch (Exception e){
return false;
}
}
结语: