From c46d5015d2ccbac81e9c3ac21f5bba51e240996f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com>
Date: Sat, 4 Dec 2021 13:15:57 +0800
Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E5=88=86=E5=8A=9F=E8=83=BD=E5=AF=BC?=
=?UTF-8?q?=E5=87=BAExcel=E6=96=B0=E5=A2=9E=E5=BF=BD=E7=95=A5=E5=88=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
ZR.Admin.WebApi/Controllers/BaseController.cs | 6 ++-
.../Controllers/System/SysUserController.cs | 40 +------------------
ZR.Model/System/SysBase.cs | 8 ++++
ZR.Model/System/SysDictType.cs | 6 +--
ZR.Model/System/SysLogininfor.cs | 2 +
ZR.Model/System/SysOperLog.cs | 22 +++++-----
ZR.Model/System/SysPost.cs | 9 +++--
ZR.Model/System/SysTasksLog.cs | 7 +++-
ZR.Model/System/SysUser.cs | 9 ++++-
ZR.Model/ZR.Model.csproj | 1 +
ZR.Service/System/GenTableService.cs | 8 ++--
.../System/IService/IGenTableService.cs | 4 +-
12 files changed, 55 insertions(+), 67 deletions(-)
diff --git a/ZR.Admin.WebApi/Controllers/BaseController.cs b/ZR.Admin.WebApi/Controllers/BaseController.cs
index f19cc41..b34e50e 100644
--- a/ZR.Admin.WebApi/Controllers/BaseController.cs
+++ b/ZR.Admin.WebApi/Controllers/BaseController.cs
@@ -9,6 +9,8 @@ using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
+using System.Linq;
+using System.Reflection;
using ZR.Admin.WebApi.Filters;
namespace ZR.Admin.WebApi.Controllers
@@ -120,13 +122,13 @@ namespace ZR.Admin.WebApi.Controllers
//调试模式需要加上
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
Directory.CreateDirectory(Path.GetDirectoryName(newFileName));
- using (ExcelPackage package = new ExcelPackage(new FileInfo(newFileName)))
+ using (ExcelPackage package = new(new FileInfo(newFileName)))
{
// 添加worksheet
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(sheetName);
//全部字段导出
- worksheet.Cells.LoadFromCollection(list, true);
+ worksheet.Cells.LoadFromCollection(list, true, OfficeOpenXml.Table.TableStyles.Light13);
package.Save();
}
diff --git a/ZR.Admin.WebApi/Controllers/System/SysUserController.cs b/ZR.Admin.WebApi/Controllers/System/SysUserController.cs
index 5ad149f..fd2242e 100644
--- a/ZR.Admin.WebApi/Controllers/System/SysUserController.cs
+++ b/ZR.Admin.WebApi/Controllers/System/SysUserController.cs
@@ -218,48 +218,10 @@ namespace ZR.Admin.WebApi.Controllers.System
[ActionPermissionFilter(Permission = "system:user: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");
- #region 自定义导出
- //添加头
- //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[i + 2, 1].Value = item.UserId;
- // //worksheet.Cells[i + 2, 2].Value = item.UserName;
- // //worksheet.Cells[i + 2, 3].Value = item.NickName;
- // //worksheet.Cells[i + 2, 4].Value = item.DeptName;
- // //worksheet.Cells[i + 2, 5].Value = item.Phonenumber;
- // //worksheet.Cells[i + 2, 6].Value = item.Sex;
- // //worksheet.Cells[i + 2, 7].Value = item.Status;
- // //worksheet.Cells[i + 2, 8].Value = item.Create_time.ToString();
- // //worksheet.Cells[i + 2, 9].Value = item.LoginIP;
- // //worksheet.Cells[i + 2, 10].Value = item.LoginDate.ToString();
- //}
- #endregion
- //全部字段导出
- worksheet.Cells.LoadFromCollection(list, true);
- package.Save();
- }
+ string sFileName = ExportExcel(list, "user", "用户列表");
return SUCCESS(new { path = "/export/" + sFileName, fileName = sFileName });
}
}
diff --git a/ZR.Model/System/SysBase.cs b/ZR.Model/System/SysBase.cs
index d7ea4fe..7cb7a95 100644
--- a/ZR.Model/System/SysBase.cs
+++ b/ZR.Model/System/SysBase.cs
@@ -4,27 +4,33 @@ using System;
using System.Collections.Generic;
using System.Text;
using SqlSugar;
+using OfficeOpenXml.Attributes;
namespace ZR.Model.System
{
+ [EpplusTable(PrintHeaders = true, AutofitColumns = true, AutoCalculate = true, ShowTotal = true)]
public class SysBase
{
[SugarColumn(IsOnlyIgnoreUpdate = true)]//设置后修改不会有此字段
[JsonProperty(propertyName: "CreateBy")]
+ [EpplusIgnore]
public string Create_by { get; set; }
[SugarColumn(IsOnlyIgnoreUpdate = true)]//设置后修改不会有此字段
[JsonProperty(propertyName: "CreateTime")]
+ [EpplusTableColumn(NumberFormat = "yyyy-MM-dd HH:mm:ss")]
public DateTime Create_time { get; set; } = DateTime.Now;
[JsonIgnore]
[JsonProperty(propertyName: "UpdateBy")]
[SugarColumn(IsOnlyIgnoreInsert = true)]
+ [EpplusIgnore]
public string Update_by { get; set; }
//[JsonIgnore]
[SugarColumn(IsOnlyIgnoreInsert = true)]//设置后插入数据不会有此字段
[JsonProperty(propertyName: "UpdateTime")]
+ [EpplusIgnore]
public DateTime? Update_time { get; set; }
public string Remark { get; set; }
@@ -40,6 +46,7 @@ namespace ZR.Model.System
///
[SugarColumn(IsIgnore = true)]
[JsonIgnore]
+ [EpplusIgnore]
public DateTime? BeginTime { get; set; }
///
@@ -47,6 +54,7 @@ namespace ZR.Model.System
///
[SugarColumn(IsIgnore = true)]
[JsonIgnore]
+ [EpplusIgnore]
public DateTime? EndTime { get; set; }
}
}
diff --git a/ZR.Model/System/SysDictType.cs b/ZR.Model/System/SysDictType.cs
index 92c067a..4b240b5 100644
--- a/ZR.Model/System/SysDictType.cs
+++ b/ZR.Model/System/SysDictType.cs
@@ -1,8 +1,5 @@
-//using Dapper.Contrib.Extensions;
+using OfficeOpenXml.Attributes;
using SqlSugar;
-using System;
-using System.Collections.Generic;
-using System.Text;
namespace ZR.Model.System
{
@@ -30,6 +27,7 @@ namespace ZR.Model.System
///
/// 状态 0、正常 1、停用
///
+ [EpplusIgnore]
public string Status { get; set; }
}
}
diff --git a/ZR.Model/System/SysLogininfor.cs b/ZR.Model/System/SysLogininfor.cs
index 1c16801..958e2f4 100644
--- a/ZR.Model/System/SysLogininfor.cs
+++ b/ZR.Model/System/SysLogininfor.cs
@@ -1,4 +1,5 @@
//using Dapper.Contrib.Extensions;
+using OfficeOpenXml.Attributes;
using SqlSugar;
using System;
@@ -52,6 +53,7 @@ namespace ZR.Model.System
///
/// 访问时间
///
+ [EpplusTableColumn(NumberFormat = "yyyy-MM-dd HH:mm:ss")]
public DateTime loginTime { get; set; } = DateTime.Now;
}
}
diff --git a/ZR.Model/System/SysOperLog.cs b/ZR.Model/System/SysOperLog.cs
index 8c1147d..7e5a690 100644
--- a/ZR.Model/System/SysOperLog.cs
+++ b/ZR.Model/System/SysOperLog.cs
@@ -3,6 +3,7 @@ using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
+using OfficeOpenXml.Attributes;
namespace ZR.Model.System
{
@@ -26,11 +27,11 @@ namespace ZR.Model.System
/** 请求方法 */
//@Excel(name = "请求方法")
- public String method { get; set; }
+ public string method { get; set; }
/** 请求方式 */
//@Excel(name = "请求方式")
- public String requestMethod { get; set; }
+ public string requestMethod { get; set; }
/** 操作类别(0其它 1后台用户 2手机端用户) */
//@Excel(name = "操作类别", readConverterExp = "0=其它,1=后台用户,2=手机端用户")
@@ -38,31 +39,31 @@ namespace ZR.Model.System
/** 操作人员 */
//@Excel(name = "操作人员")
- public String operName { get; set; }
+ public string operName { get; set; }
/** 部门名称 */
//@Excel(name = "部门名称")
- public String deptName { get; set; }
+ public string deptName { get; set; }
/** 请求url */
//@Excel(name = "请求地址")
- public String operUrl { get; set; }
+ public string operUrl { get; set; }
/** 操作地址 */
//@Excel(name = "操作地址")
- public String operIp { get; set; }
+ public string operIp { get; set; }
/** 操作地点 */
//@Excel(name = "操作地点")
- public String operLocation { get; set; }
+ public string operLocation { get; set; }
/** 请求参数 */
//@Excel(name = "请求参数")
- public String operParam { get; set; }
+ public string operParam { get; set; }
/** 返回参数 */
//@Excel(name = "返回参数")
- public String jsonResult { get; set; }
+ public string jsonResult { get; set; }
/** 操作状态(0正常 1异常) */
//@Excel(name = "状态", readConverterExp = "0=正常,1=异常")
@@ -70,13 +71,14 @@ namespace ZR.Model.System
/** 错误消息 */
//@Excel(name = "错误消息")
- public String errorMsg { get; set; }
+ public string errorMsg { get; set; }
/** 操作时间 */
//@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
//@Excel(name = "操作时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
//[DataType]
[JsonConverter(typeof(JsonDateConverter))]
+ [EpplusTableColumn(NumberFormat = "yyyy-MM-dd HH:mm:ss")]
public DateTime? operTime { get; set; }
///
/// 操作用时
diff --git a/ZR.Model/System/SysPost.cs b/ZR.Model/System/SysPost.cs
index a441522..766cbc3 100644
--- a/ZR.Model/System/SysPost.cs
+++ b/ZR.Model/System/SysPost.cs
@@ -1,19 +1,22 @@
-using SqlSugar;
+using OfficeOpenXml.Attributes;
+using SqlSugar;
namespace ZR.Model.System
{
[SugarTable("sys_post")]
[Tenant("0")]
- public class SysPost: SysBase
+ public class SysPost : SysBase
{
///
/// 岗位Id
///
- [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
+ [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public long PostId { get; set; }
public string PostCode { get; set; }
public string PostName { get; set; }
+ [EpplusIgnore]
public int PostSort { get; set; }
+ [EpplusIgnore]
public string Status { get; set; }
}
}
diff --git a/ZR.Model/System/SysTasksLog.cs b/ZR.Model/System/SysTasksLog.cs
index 17e22d0..32ab87e 100644
--- a/ZR.Model/System/SysTasksLog.cs
+++ b/ZR.Model/System/SysTasksLog.cs
@@ -1,4 +1,5 @@
-using SqlSugar;
+using OfficeOpenXml.Attributes;
+using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
@@ -15,7 +16,7 @@ namespace ZR.Model.System
///
/// 日志Id
///
- [SqlSugar.SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
+ [SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
public long JobLogId { get; set; }
///
/// 任务Id
@@ -39,6 +40,8 @@ namespace ZR.Model.System
/// 调用目标字符串
///
public string InvokeTarget { get; set; }
+
+ [EpplusTableColumn(NumberFormat = "yyyy-MM-dd HH:mm:ss")]
public DateTime CreateTime { get; set; }
///
/// 执行用时,毫秒
diff --git a/ZR.Model/System/SysUser.cs b/ZR.Model/System/SysUser.cs
index c01ed61..7a19ae6 100644
--- a/ZR.Model/System/SysUser.cs
+++ b/ZR.Model/System/SysUser.cs
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
+using OfficeOpenXml.Attributes;
using SqlSugar;
using System;
using System.Collections.Generic;
@@ -29,12 +30,13 @@ namespace ZR.Model.System
//[JsonProperty(propertyName: "userType")]
//public string User_type { get; set; } = "";
[SugarColumn(IsOnlyIgnoreInsert = true)]
+ [EpplusIgnore]
public string Avatar { get; set; }
public string Email { get; set; }
[JsonIgnore]
- //[ColName("用户密码")]
+ [EpplusIgnore]
public string Password { get; set; }
//[ColName("手机号")]
@@ -48,6 +50,7 @@ namespace ZR.Model.System
///
/// 帐号状态(0正常 1停用)
///
+ [EpplusIgnore]
public string Status { get; set; }
///
@@ -66,6 +69,7 @@ namespace ZR.Model.System
/// 最后登录时间
///
[SugarColumn(IsOnlyIgnoreInsert = true)]
+ [EpplusTableColumn(NumberFormat = "yyyy-MM-dd HH:mm:ss")]
public DateTime LoginDate { get; set; }
///
@@ -94,14 +98,17 @@ namespace ZR.Model.System
/// 角色id集合
///
[SugarColumn(IsIgnore = true)]
+ [EpplusIgnore]
public int[] RoleIds { get; set; }
///
/// 岗位集合
///
[SugarColumn(IsIgnore = true)]
+ [EpplusIgnore]
public int[] PostIds { get; set; }
[SugarColumn(IsIgnore = true)]
+ [EpplusIgnore]
public List Roles { get; set; }
#endregion
diff --git a/ZR.Model/ZR.Model.csproj b/ZR.Model/ZR.Model.csproj
index 58886cf..038843a 100644
--- a/ZR.Model/ZR.Model.csproj
+++ b/ZR.Model/ZR.Model.csproj
@@ -5,6 +5,7 @@
+
diff --git a/ZR.Service/System/GenTableService.cs b/ZR.Service/System/GenTableService.cs
index 5d8b85d..62454fe 100644
--- a/ZR.Service/System/GenTableService.cs
+++ b/ZR.Service/System/GenTableService.cs
@@ -16,11 +16,11 @@ namespace ZR.Service.System
/// 代码生成表
///
[AppService(ServiceType = typeof(IGenTableService), ServiceLifetime = LifeTime.Transient)]
- public class GenTableService : IGenTableService
+ public class GenTableService : BaseService, IGenTableService
{
private GenTableRepository GenTableRepository;
private IGenTableColumnService GenTableColumnService;
- public GenTableService(IGenTableColumnService genTableColumnService, GenTableRepository genTableRepository)
+ public GenTableService(IGenTableColumnService genTableColumnService, GenTableRepository genTableRepository) : base(genTableRepository)
{
GenTableColumnService = genTableColumnService;
GenTableRepository = genTableRepository;
@@ -127,11 +127,11 @@ namespace ZR.Service.System
/// 代码生成表列
///
[AppService(ServiceType = typeof(IGenTableColumnService), ServiceLifetime = LifeTime.Transient)]
- public class GenTableColumnService : IGenTableColumnService
+ public class GenTableColumnService : BaseService, IGenTableColumnService
{
private GenTableColumnRepository GetTableColumnRepository;
- public GenTableColumnService(GenTableColumnRepository genTableColumnRepository)
+ public GenTableColumnService(GenTableColumnRepository genTableColumnRepository) : base(genTableColumnRepository)
{
GetTableColumnRepository = genTableColumnRepository;
}
diff --git a/ZR.Service/System/IService/IGenTableService.cs b/ZR.Service/System/IService/IGenTableService.cs
index dd401f2..9a996b7 100644
--- a/ZR.Service/System/IService/IGenTableService.cs
+++ b/ZR.Service/System/IService/IGenTableService.cs
@@ -4,7 +4,7 @@ using ZR.Model.System.Generate;
namespace ZR.Service.System.IService
{
- public interface IGenTableService
+ public interface IGenTableService: IBaseService
{
List SelectDbTableListByNamess(string[] tableNames);
@@ -17,7 +17,7 @@ namespace ZR.Service.System.IService
int UpdateGenTable(GenTable genTable);
}
- public interface IGenTableColumnService
+ public interface IGenTableColumnService: IBaseService
{
int InsertGenTableColumn(List tableColumn);