8000 1.0.6 新增excel导出示例 · HyperGenm/springboot-vue@bd0b9fa · GitHub
[go: up one dir, main page]

Skip to content

Commit bd0b9fa

Browse files
author
WeiziPlus
committed
1.0.6 新增excel导出示例
1 parent 67948b2 commit bd0b9fa

File tree

6 files changed

+138
-1
lines changed

6 files changed

+138
-1
lines changed

springboot/demo-pc/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
<artifactId>spring-boot-test</artifactId>
6161
<scope>test</scope>
6262
</dependency>
63+
<!-- excel导出 -->
64+
<dependency>
65+
<groupId>com.alibaba</groupId>
66+
<artifactId>easyexcel</artifactId>
67+
</dependency>
6368
</dependencies>
6469

6570
<build>

springboot/demo-pc/src/main/java/com/weiziplus/pc/core/system/controller/SysUserLogController.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,17 @@ public ResultUtils<PageUtils<List<SysUserLogVo>>> getPageList(
5656
createTimeSort);
5757
}
5858

59+
@ApiOperation(value = "导出excel")
60+
@ApiImplicitParams({
61+
@ApiImplicitParam(name = "startTime", value = "开始时间", required = true, dataType = "String", paramType = "form"),
62+
@ApiImplicitParam(name = "endTime", value = "截止时间", required = true, dataType = "String", paramType = "form"),
63+
})
64+
@PostMapping("/exportExcel")
65+
@SysUserLog(description = "导出系统用户日志excel")
66+
public void exportExcel(
67+
@RequestParam(defaultValue = "1990-01-01 00:00:00") String startTime,
68+
@RequestParam(defaultValue = "2200-12-31 23:59:59") String endTime) {
69+
service.exportExcel(startTime, endTime);
70+
}
71+
5972
}

springboot/demo-pc/src/main/java/com/weiziplus/pc/core/system/service/SysUserLogService.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
package com.weiziplus.pc.core.system.service;
22

3+
import com.alibaba.excel.ExcelWriter;
4+
import com.alibaba.excel.metadata.Sheet;
5+
import com.alibaba.excel.metadata.Table;
6+
import com.alibaba.excel.support.ExcelTypeEnum;
37
import com.github.pagehelper.PageHelper;
48
import com.weiziplus.common.base.BaseService;
9+
import com.weiziplus.common.base.BaseWhere;
10+
import com.weiziplus.common.models.SysUserLog;
11+
import com.weiziplus.common.util.HttpRequestUtils;
512
import com.weiziplus.common.util.PageUtils;
613
import com.weiziplus.common.util.ResultUtils;
14+
import com.weiziplus.common.util.ToolUtils;
15+
import com.weiziplus.common.util.token.AdminTokenUtils;
16+
import com.weiziplus.pc.common.config.MyGlobalConfig;
717
import com.weiziplus.pc.core.system.mapper.SysUserLogMapper;
818
import com.weiziplus.pc.core.system.vo.SysUserLogVo;
919
import lombok.extern.slf4j.Slf4j;
1020
import org.springframework.beans.factory.annotation.Autowired;
1121
import org.springframework.stereotype.Service;
22+
import org.springframework.web.context.request.RequestContextHolder;
23+
import org.springframework.web.context.request.ServletRequestAttributes;
1224

25+
import javax.servlet.ServletOutputStream;
26+
import javax.servlet.http.HttpServletRequest;
27+
import javax.servlet.http.HttpServletResponse;
28+
import java.io.IOException;
29+
import java.nio.charset.StandardCharsets;
30+
import java.util.ArrayList;
31+
import java.util.Arrays;
1332
import java.util.List;
1433

1534
/**
@@ -54,4 +73,77 @@ public ResultUtils<PageUtils<List<SysUserLogVo>>> getPageList(Integer pageNum, I
5473
return ResultUtils.success(pageUtil);
5574
}
5675

76+
/**
77+
* 导出excel
78+
*
79+
* @param startTime
80+
* @param endTime
81+
*/
82+
public void exportExcel(String startTime, String endTime) {
83+
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
84+
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
85+
Integer userIdByHttpServletRequest = AdminTokenUtils.getUserIdByHttpServletRequest(request);
86+
if (!MyGlobalConfig.SYS_USER_SUPER_ADMIN_ID.equals(userIdByHttpServletRequest)) {
87+
HttpRequestUtils.handleErrorResponse(
88+
response, ResultUtils.errorRole("只有超级管理员才可以导出系统日志"), "非超级管理员导出系统用户日志");
89+
return;
90+
}
91+
List<SysUserLog> sysUserLogList = baseFindListByClassAndBaseWhereList(SysUserLog.class, new ArrayList<BaseWhere>(ToolUtils.initialCapacity(2)) {{
92+
add(new BaseWhere(SysUserLog.COLUMN_CREATE_TIME, BaseWhere.Where.MORE_THAN.getValue(), startTime));
93+
add(new BaseWhere(SysUserLog.COLUMN_CREATE_TIME, BaseWhere.Where.LESS_THAN_EQUAL.getValue(), endTime));
94+
}});
95+
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
96+
ServletOutputStream outputStream = null;
97+
try {
98+
outputStream = response.getOutputStream();
99+
ExcelWriter writer = new ExcelWriter(outputStream, ExcelTypeEnum.XLSX);
100+
Sheet sheet = new Sheet(1, 0);
101+
sheet.setSheetName("系统用户日志");
102+
Table table = new Table(1);
103+
//表头
104+
List<List<String>> titleList = new ArrayList<>(ToolUtils.initialCapacity(7));
105+
titleList.add(Arrays.asList("请求路径"));
106+
titleList.add(Arrays.asList("请求参数"));
107+
titleList.add(Arrays.asList("请求类型"));
108+
titleList.add(Arrays.asList("操作描述"));
109+
titleList.add(Arrays.asList("浏览器"));
110+
titleList.add(Arrays.asList("操作系统"));
111+
titleList.add(Arrays.asList("创建时间"));
112+
table.setHead(titleList);
113+
//内容
114+
List<List<String>> dataList = new ArrayList<>();
115+
String[] typeArr = {null, "查询", "新增", "修改", "删除"};
116+
for (SysUserLog sysUserLog : sysUserLogList) {
117+
dataList.add(Arrays.asList(
118+
sysUserLog.getUrl(),
119+
sysUserLog.getParam(),
120+
typeArr[sysUserLog.getType()],
121+
sysUserLog.getDescription(),
122+
sysUserLog.getBorderName(),
123+
sysUserLog.getOsName(),
124+
sysUserLog.getCreateTime()
125+
));
126+
}
127+
writer.write0(dataList, sheet, table);
128+
// 下载EXCEL
129+
response.setHeader("Content-Disposition", "attachment");
130+
response.setContentType("multipart/form-data");
131+
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
132+
writer.finish();
133+
outputStream.flush();
134+
} catch (IOException e) {
135+
log.warn("系统用户日志导出excel出错,详情:" + e);
136+
HttpRequestUtils.handleErrorResponse(
137+
response, ResultUtils.error("系统用户日志导出excel出错,详情:" + e), "系统用户日志导出excel出错");
138+
} finally {
139+
if (null != outputStream) {
140+
try {
141+
outputStream.close();
142+
} catch (Exception e) {
143+
log.warn("系统用户日志导出excel关闭流出错,详情:" + e);
144+
}
145+
}
146+
}
147+
}
148+
57149
}

springboot/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@
169169
<artifactId>mysql-connector-java</artifactId>
170170
<version>8.0.17</version>
171171
</dependency>
172+
<!-- excel导出 -->
173+
<dependency>
174+
<groupId>com.alibaba</groupId>
175+
<artifactId>easyexcel</artifactId>
176+
<version>2.1.4</version>
177+
</dependency>
172178
</dependencies>
173179
</dependencyManagement>
174180

vue/src/utils/global_variable.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ const URL = {
9595
/*********************系统用户日志*****************/
9696
sysUserLog: {
9797
/*获取分页数据*/
98-
getPageList: '/sysUserLog/getPageList'
98+
getPageList: '/sysUserLog/getPageList',
99+
/*导出excel*/
100+
exportExcel: '/sysUserLog/exportExcel'
99101
},
100102
/*********************系统异常*****************/
101103
sysError: {

vue/src/views/main/system/sysUserLog/Index.vue

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<wei-table ref="table"
55
:tableDataRequest="tableDataRequest"
66
:tableColumns="tableColumns"
7+
:tableHeaderButtons="tableHeaderButtons"
78
:tableOperates="tableOperates"
89
:tableSearch="tableSearch"></wei-table>
910
</template>
@@ -69,6 +70,15 @@
6970
}
7071
}
7172
],
73+
//表格上面按钮
74+
tableHeaderButtons: [
75+
{
76+
name: '导出excel', icon: 'el-icon-notebook-2', type: 'success', show: true,
77+
handleClick() {
78+
that.exportExcel();
79+
}
80+
}
81+
],
7282
//表格对应每一行按钮
7383
tableOperates: {
7484
//按钮数组
@@ -108,6 +118,15 @@
108118
//详情弹窗
109119
dialogDetail: false,
110120
}
121+
},
122+
methods: {
123+
exportExcel() {
124+
let that = this;
125+
this.$axiosDown({
126+
url: that.$global.URL.system.sysUserLog.exportExcel,
127+
filename: '系统用户日志.xlsx'
128+
})
129+
}
111130
}
112131
}
113132
</script>

0 commit comments

Comments
 (0)
0