新增加用户数据导出

This commit is contained in:
izory 2021-11-20 20:01:41 +08:00
parent ec5ea3ef9b
commit 156b4b5222
4 changed files with 87 additions and 18 deletions

View File

@ -1,8 +1,11 @@
using Infrastructure.Attribute; using Infrastructure.Attribute;
using Infrastructure.Enums; using Infrastructure.Enums;
using Infrastructure.Model; using Infrastructure.Model;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using OfficeOpenXml;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using ZR.Admin.WebApi.Filters; using ZR.Admin.WebApi.Filters;
@ -13,7 +16,7 @@ using ZR.Service.System.IService;
namespace ZR.Admin.WebApi.Controllers.System namespace ZR.Admin.WebApi.Controllers.System
{ {
[Verify] //[Verify]
[Route("system/user")] [Route("system/user")]
public class SysUserController : BaseController public class SysUserController : BaseController
{ {
@ -23,17 +26,20 @@ namespace ZR.Admin.WebApi.Controllers.System
private readonly ISysRoleService RoleService; private readonly ISysRoleService RoleService;
private readonly ISysPostService PostService; private readonly ISysPostService PostService;
private readonly ISysUserPostService UserPostService; private readonly ISysUserPostService UserPostService;
private IWebHostEnvironment WebHostEnvironment;
public SysUserController( public SysUserController(
ISysUserService userService, ISysUserService userService,
ISysRoleService roleService, ISysRoleService roleService,
ISysPostService postService, ISysPostService postService,
ISysUserPostService userPostService) ISysUserPostService userPostService,
IWebHostEnvironment HostEnvironment)
{ {
UserService = userService; UserService = userService;
RoleService = roleService; RoleService = roleService;
PostService = postService; PostService = postService;
UserPostService = userPostService; UserPostService = userPostService;
WebHostEnvironment = HostEnvironment;
} }
/// <summary> /// <summary>
@ -203,5 +209,56 @@ namespace ZR.Admin.WebApi.Controllers.System
// //Response.Headers.Append("content-disposition", "attachment;filename=sysUser.xlsx"); // //Response.Headers.Append("content-disposition", "attachment;filename=sysUser.xlsx");
// return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "sysUser.xlsx"); // return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "sysUser.xlsx");
//} //}
/// <summary>
/// 用户导出
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
[HttpGet("export")]
[Log(Title = "用户导出", BusinessType = BusinessType.EXPORT)]
public IActionResult UserExport([FromQuery] SysUser user)
{
string sFileName = $"用户列表{DateTime.Now:yyyyMMddHHmmss}.xlsx";
string newFileName = Path.Combine(WebHostEnvironment.WebRootPath, "export", sFileName);
var list = UserService.SelectUserList(user, new PagerInfo(1, 10000));
//调试模式需要加上
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
Directory.CreateDirectory(Path.GetDirectoryName(newFileName));
using (ExcelPackage package = new ExcelPackage(new FileInfo(newFileName)))
{
// 添加worksheet
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("sysuser");
//添加头
worksheet.Cells[1, 1].Value = "用户id";
worksheet.Cells[1, 2].Value = "用户名称";
worksheet.Cells[1, 3].Value = "用户昵称";
worksheet.Cells[1, 4].Value = "部门";
worksheet.Cells[1, 5].Value = "手机号码";
worksheet.Cells[1, 6].Value = "性别";
worksheet.Cells[1, 7].Value = "状态";
worksheet.Cells[1, 8].Value = "添加时间";
worksheet.Cells[1, 9].Value = "登录IP";
worksheet.Cells[1, 10].Value = "最后登录时间";
for (int i = 0; i < list.Count; i++)
{
var item = list[i];
worksheet.Cells[$"A{i + 2}"].Value = item.UserId;
worksheet.Cells[$"B{i + 2}"].Value = item.UserName;
worksheet.Cells[$"C{i + 2}"].Value = item.NickName;
worksheet.Cells[$"D{i + 2}"].Value = item.DeptName;
worksheet.Cells[$"E{i + 2}"].Value = item.Phonenumber;
worksheet.Cells[$"F{i + 2}"].Value = item.Sex;
worksheet.Cells[$"G{i + 2}"].Value = item.Status;
worksheet.Cells[$"H{i + 2}"].Value = item.Create_time.ToString();
worksheet.Cells[$"I{i + 2}"].Value = item.LoginIP;
worksheet.Cells[$"J{i + 2}"].Value = item.LoginDate.ToString();
}
package.Save();
}
return SUCCESS(new { zipPath = "/export/" + sFileName, fileName = sFileName });
}
} }
} }

View File

@ -23,6 +23,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="EPPlus" Version="5.8.3" />
<PackageReference Include="Hei.Captcha" Version="0.3.0" /> <PackageReference Include="Hei.Captcha" Version="0.3.0" />
<PackageReference Include="IPTools.China" Version="1.6.0" /> <PackageReference Include="IPTools.China" Version="1.6.0" />
<PackageReference Include="CSRedisCore" Version="3.6.5" /> <PackageReference Include="CSRedisCore" Version="3.6.5" />
@ -90,6 +91,7 @@
<ItemGroup> <ItemGroup>
<Folder Include="Controllers\business\" /> <Folder Include="Controllers\business\" />
<Folder Include="wwwroot\export\" />
<Folder Include="wwwroot\Generatecode\" /> <Folder Include="wwwroot\Generatecode\" />
</ItemGroup> </ItemGroup>

View File

@ -84,6 +84,7 @@ export function resolveExcel(res, fileName) {
link.style.display = 'none' link.style.display = 'none'
link.href = url link.href = url
link.setAttribute('download', fileName)// 文件名 link.setAttribute('download', fileName)// 文件名
link.setAttribute('target', "_black")
document.body.appendChild(link) document.body.appendChild(link)
link.click() link.click()
document.body.removeChild(link) // 下载完成移除元素 document.body.removeChild(link) // 下载完成移除元素

View File

@ -45,17 +45,17 @@
</el-col> </el-col>
<!-- <el-col :span="1.5"> <!-- <el-col :span="1.5">
<el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport" v-hasPermi="['system:user:import']">导入</el-button> <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport" v-hasPermi="['system:user:import']">导入</el-button>
</el-col> </el-col> -->
<el-col :span="1.5"> <el-col :span="1.5">
<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:user:export']">导出</el-button> <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:user:export']">导出</el-button>
</el-col> --> </el-col>
<right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar> <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
</el-row> </el-row>
<el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange"> <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
<el-table-column type="selection" width="50" align="center" /> <el-table-column type="selection" width="50" align="center" />
<el-table-column label="用户编号" align="center" prop="userId" width="80" /> <el-table-column label="用户编号" align="center" prop="userId" width="80" />
<el-table-column label="用户名称" align="center" prop="userName" :show-overflow-tooltip="true"/> <el-table-column label="用户名称" align="center" prop="userName" :show-overflow-tooltip="true" />
<el-table-column label="用户昵称" align="center" prop="nickName" :show-overflow-tooltip="true"/> <el-table-column label="用户昵称" align="center" prop="nickName" :show-overflow-tooltip="true" />
<el-table-column label="头像" prop="avatar"> <el-table-column label="头像" prop="avatar">
<template slot-scope="scope"> <template slot-scope="scope">
<el-image :src="scope.row.avatar" :preview-src-list="[scope.row.avatar]"></el-image> <el-image :src="scope.row.avatar" :preview-src-list="[scope.row.avatar]"></el-image>
@ -68,8 +68,8 @@
<el-switch v-model="scope.row.status" active-value="0" inactive-value="1" @change="handleStatusChange(scope.row)"></el-switch> <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" @change="handleStatusChange(scope.row)"></el-switch>
</template> </template>
</el-table-column> </el-table-column>
<el-table-column label="创建时间" align="center" prop="createTime" width="160" ></el-table-column> <el-table-column label="创建时间" align="center" prop="createTime" width="160"></el-table-column>
<el-table-column label="登录IP" align="center" prop="loginIP" v-if="columns[0].checked"/> <el-table-column label="登录IP" align="center" prop="loginIP" v-if="columns[0].checked" />
<el-table-column label="最后登录时间" align="center" prop="loginDate" v-if="columns[1].checked"> <el-table-column label="最后登录时间" align="center" prop="loginDate" v-if="columns[1].checked">
<template slot-scope="scope"> <template slot-scope="scope">
<span>{{ parseTime(scope.row.createTime) }}</span> <span>{{ parseTime(scope.row.createTime) }}</span>
@ -176,7 +176,8 @@
<!-- 用户导入对话框 --> <!-- 用户导入对话框 -->
<el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body> <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
<el-upload name="file" ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag> <el-upload name="file" ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
:on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
<i class="el-icon-upload"></i> <i class="el-icon-upload"></i>
<div class="el-upload__text"> <div class="el-upload__text">
将文件拖到此处 将文件拖到此处
@ -208,13 +209,13 @@ import {
exportUser, exportUser,
resetUserPwd, resetUserPwd,
changeUserStatus, changeUserStatus,
importTemplate, // importTemplate,
} from "@/api/system/user"; } from "@/api/system/user";
import { getToken } from "@/utils/auth"; import { getToken } from "@/utils/auth";
import { treeselect } from "@/api/system/dept"; import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect"; import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css"; import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { downLoadExcel } from "@/utils/zipdownload.js"; import { downLoadExcel, downloadFile } from "@/utils/zipdownload.js";
export default { export default {
name: "User", name: "User",
@ -346,7 +347,7 @@ export default {
this.getDicts("sys_user_sex").then((response) => { this.getDicts("sys_user_sex").then((response) => {
this.sexOptions = response.data; this.sexOptions = response.data;
}); });
this.getConfigKey("sys.user.initPassword").then(response => { this.getConfigKey("sys.user.initPassword").then((response) => {
this.initPassword = response.data; this.initPassword = response.data;
}); });
}, },
@ -545,13 +546,21 @@ export default {
confirmButtonText: "确定", confirmButtonText: "确定",
cancelButtonText: "取消", cancelButtonText: "取消",
type: "warning", type: "warning",
}) }).then(() => {
.then(function () { exportUser(queryParams).then((response) => {
return exportUser(queryParams); console.log(JSON.stringify(response));
}) const { code, data } = response;
.then((response) => { if (code == 200) {
this.download(response.msg); this.msgSuccess("导出成功");
downloadFile(
process.env.VUE_APP_BASE_API + data.zipPath,
data.fileName
);
} else {
this.msgError("导出失败");
}
}); });
});
}, },
/** 导入按钮操作 */ /** 导入按钮操作 */
handleImport() { handleImport() {