前言:
项目架构:
前端框架:Angular6 + ng-zorro-antd
后端架构:SSM + Dubbo:springMVC + spring + myBatis-plus
接下来请随小编一起来看看excel导出是如何实现的呢?
正文:
前端
一。具体技术实现步骤
1.html,按钮为触发事件
<button nz-button nzType="primary" nzGhost (click)="downTaleData()">导出</button>
2.component.ts,前后端交互
downTaleData() {
const url = 'http://localhost:8085/local-web/foo/exportFooByGroupId/{gruopId}';
window.open(url);
URL.revokeObjectURL(url);
}
}
后端
一。具体技术实现
1.在pom文件中引入相关jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.jplus</groupId>
<artifactId>J-Excel</artifactId>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
</dependency>
2.核心业务逻辑代码
2.1 serviceImpl
@Override
public ItooResult exportByGroupId(HttpServletResponse response, String groupId) {
Map<Serializable, Serializable> map = new HashMap<>(16);
//日期转换字符串的格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
map.put(SHEET_NAME, "Foo信息");
//要导出的字段
map.put(COLUMNS, new String[]{"userName", "sex", "telNum", "startTime", "fooGroupName"});
//导出表格的字段
map.put(TITLE, "Foo信息");
map.put(NAME, "Foo信息表");
//要导出的数据
List<FooTemplate> fooTemplates = new ArrayList<>();
List<FooModel> fooModels = fooDao.queryFooByGroupId(groupId);
for (FooModel fooModel: fooModels){
FooTemplate fooTemplate = new FooTemplate();
BeanUtils.copyProperties(fooModel, fooTemplate);
fooTemplate.setFooGroupName(fooModel.getDeptName());
fooTemplate.setStartTime(simpleDateFormat.format(fooModel.getStartTime()));
fooTemplates.add(fooTemplate);
}
//为空不进行导出
if (fooTemplates.size() == 0){
return ItooResult.build(ItooResult.FAIL, "没有要导出的数据!");
}
map.put(DATA_LIST, (Serializable)fooTemplates);
//导出
try {
ExcelUtil.exportExcel(map, response);
}catch (Exception e){
log.error("导出失败,未知的异常" + e);
return ItooResult.build(ItooResult.FAIL, "导出信息失败!");
}
return ItooResult.build(ItooResult.SUCCESS, "导出信息成功");
}
2.2 sql
SELECT
f.id,
f.user_code,
f.user_name,
f.sex,
f.tel_num,
f.start_time,
f.end_time,
fg.id AS fooGroupId,
fg.dept_name
FROM
t_foo f
LEFT JOIN t_foo_group fg ON f.foo_group_id = fg.id
WHERE
f.is_delete = 0
AND fg.is_delete = 0
AND f.foo_group_id = #{fooGroupId}
ORDER BY
f.create_time DESC
结语: