From 5d0171177bcd16661d92c9fc2cd5d57adb0e8473 Mon Sep 17 00:00:00 2001 From: lzw <605106923@qq.com> Date: Sun, 31 Oct 2021 17:18:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=A1=A8=E6=A0=BC=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/RoleController.cs | 25 +- .../Yi.Framework.Interface/IUserService.cs | 2 +- .../Yi.Framework.Service/MenuService.cs | 2 +- .../Yi.Framework.Service/RoleService.cs | 4 +- .../Yi.Framework.Service/UserService.cs | 4 +- Yi.Framework/Yi.Framework.WebCore/Excel.cs | 228 ++++++++++++++++++ .../Yi.Framework.WebCore.csproj | 1 + 7 files changed, 256 insertions(+), 10 deletions(-) create mode 100644 Yi.Framework/Yi.Framework.WebCore/Excel.cs diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/Controllers/RoleController.cs b/Yi.Framework/Yi.Framework.ApiMicroservice/Controllers/RoleController.cs index de30f72d..c7fb0744 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/Controllers/RoleController.cs +++ b/Yi.Framework/Yi.Framework.ApiMicroservice/Controllers/RoleController.cs @@ -3,12 +3,14 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading.Tasks; using Yi.Framework.Common.Models; using Yi.Framework.DTOModel; using Yi.Framework.Interface; using Yi.Framework.Model.Models; +using Yi.Framework.WebCore; namespace Yi.Framework.ApiMicroservice.Controllers { @@ -24,8 +26,22 @@ namespace Yi.Framework.ApiMicroservice.Controllers } [HttpGet] public async Task GetRole() + { + return Result.Success().SetData(await _roleService.GetAllEntitiesTrueAsync()); + } + [HttpPost] + public async Task GetRoleFlie() { - return Result.Success().SetData(await _roleService.GetAllEntitiesTrueAsync()); + var roleList = await _roleService.GetAllEntitiesTrueAsync(); + Dictionary dt = new(); + dt.Add("输出", "文件"); + var byteStream = Excel.ExportExcel(roleList.ToList(), dt).ToString(); + MemoryStream s = new MemoryStream(); + StreamWriter writer = new StreamWriter(s); + writer.Write(byteStream); + writer.Flush(); + //stream.Read(byteStream, 0, byteStream.Length); + return new FileStreamResult(s, "application/vnd.ms-excel"); } /// @@ -91,12 +107,13 @@ namespace Yi.Framework.ApiMicroservice.Controllers /// /// 用于给角色设置菜单的时候,点击一个角色,显示这个角色拥有的并列的菜单 /// - /// + /// /// [HttpGet] - public async Task GetTopMenusByRoleId(int roleId) + public async Task GetTopMenusByRoleId(role role) { - return Result.Success().SetData(await _roleService.GetTopMenusByRoleId(roleId) ); ; + + return Result.Success().SetData(await _roleService.GetTopMenusByRoleId(role.id) ); ; } } } diff --git a/Yi.Framework/Yi.Framework.Interface/IUserService.cs b/Yi.Framework/Yi.Framework.Interface/IUserService.cs index 5230e1c9..42de0081 100644 --- a/Yi.Framework/Yi.Framework.Interface/IUserService.cs +++ b/Yi.Framework/Yi.Framework.Interface/IUserService.cs @@ -52,7 +52,7 @@ namespace Yi.Framework.Interface /// /// /// - Task> GetMenuByHttpUser(int userId); + Task GetMenuByHttpUser(int userId); /// /// 根据路由获取菜单 /// diff --git a/Yi.Framework/Yi.Framework.Service/MenuService.cs b/Yi.Framework/Yi.Framework.Service/MenuService.cs index e2825cd6..8e1c5d19 100644 --- a/Yi.Framework/Yi.Framework.Service/MenuService.cs +++ b/Yi.Framework/Yi.Framework.Service/MenuService.cs @@ -43,7 +43,7 @@ namespace Yi.Framework.Service public async Task> GetTopMenusByTopMenuIds(List menuIds) { - return await _DbRead.Set().Where(u => menuIds.Contains(u.id)).ToListAsync(); + return await _DbRead.Set().AsNoTracking().Where(u => menuIds.Contains(u.id)).ToListAsync(); } public async Task SetMouldByMenu(int id1,int id2) diff --git a/Yi.Framework/Yi.Framework.Service/RoleService.cs b/Yi.Framework/Yi.Framework.Service/RoleService.cs index 3e0389ac..05f776b5 100644 --- a/Yi.Framework/Yi.Framework.Service/RoleService.cs +++ b/Yi.Framework/Yi.Framework.Service/RoleService.cs @@ -45,9 +45,9 @@ namespace Yi.Framework.Service } public async Task> GetTopMenusByRoleId(int roleId) { - var role_data = await _Db.Set().Where(u => u.id == roleId).FirstOrDefaultAsync(); + var role_data = await _Db.Set().Include(u=>u.menus).Where(u => u.id == roleId).FirstOrDefaultAsync(); var menuList = role_data.menus.Where(u => u.is_delete == Normal).ToList(); - menuList.ForEach(u => u.children = null); + return menuList; } diff --git a/Yi.Framework/Yi.Framework.Service/UserService.cs b/Yi.Framework/Yi.Framework.Service/UserService.cs index 3ef22753..7abe95c3 100644 --- a/Yi.Framework/Yi.Framework.Service/UserService.cs +++ b/Yi.Framework/Yi.Framework.Service/UserService.cs @@ -54,7 +54,7 @@ namespace Yi.Framework.Service return menuList; } - public async Task> GetMenuByHttpUser(int userId) + public async Task GetMenuByHttpUser(int userId) { var user_data = await GetUserById(userId); List menuList = new(); @@ -78,7 +78,7 @@ namespace Yi.Framework.Service } - return TopMenuBuild2(TopMenuBuild(endMenu, allMenuIds)); + return TopMenuBuild2(TopMenuBuild(endMenu, allMenuIds))[0]; } /// /// 这种就命名的话,改成MenuIconAndShowBuild diff --git a/Yi.Framework/Yi.Framework.WebCore/Excel.cs b/Yi.Framework/Yi.Framework.WebCore/Excel.cs new file mode 100644 index 00000000..a7961b87 --- /dev/null +++ b/Yi.Framework/Yi.Framework.WebCore/Excel.cs @@ -0,0 +1,228 @@ +using NPOI.SS.UserModel; +using NPOI.XSSF.UserModel; +using System; +using System.Collections.Generic; +using System.Data; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.WebCore +{ + public class Excel + { + /// + /// 读取Excel多Sheet数据 + /// + /// 文件路径 + /// Sheet名 + /// + public static DataSet ReadExcelToDataSet(string filePath, string sheetName = null) + { + if (!File.Exists(filePath)) + { + //LogUtil.Debug($"未找到文件{filePath}"); + return null; + } + //获取文件信息 + FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); + IWorkbook workbook = WorkbookFactory.Create(fs); + //获取sheet信息 + ISheet sheet = null; + DataSet ds = new DataSet(); + if (!string.IsNullOrEmpty(sheetName)) + { + sheet = workbook.GetSheet(sheetName); + if (sheet == null) + { + //LogUtil.Debug($"{filePath}未找到sheet:{sheetName}"); + return null; + } + DataTable dt = ReadExcelFunc(workbook, sheet); + ds.Tables.Add(dt); + } + else + { + //遍历获取所有数据 + int sheetCount = workbook.NumberOfSheets; + for (int i = 0; i < sheetCount; i++) + { + sheet = workbook.GetSheetAt(i); + if (sheet != null) + { + DataTable dt = ReadExcelFunc(workbook, sheet); + ds.Tables.Add(dt); + } + } + } + return ds; + } + + /// + /// 读取Excel信息 + /// + /// 工作区 + /// sheet + /// + private static DataTable ReadExcelFunc(IWorkbook workbook, ISheet sheet) + { + DataTable dt = new DataTable(); + //获取列信息 + IRow cells = sheet.GetRow(sheet.FirstRowNum); + int cellsCount = cells.PhysicalNumberOfCells; + int emptyCount = 0; + int cellIndex = sheet.FirstRowNum; + List listColumns = new List(); + bool isFindColumn = false; + while (!isFindColumn) + { + emptyCount = 0; + listColumns.Clear(); + for (int i = 0; i < cellsCount; i++) + { + if (string.IsNullOrEmpty(cells.GetCell(i).StringCellValue)) + { + emptyCount++; + } + listColumns.Add(cells.GetCell(i).StringCellValue); + } + //这里根据逻辑需要,空列超过多少判断 + if (emptyCount == 0) + { + isFindColumn = true; + } + cellIndex++; + cells = sheet.GetRow(cellIndex); + } + + foreach (string columnName in listColumns) + { + if (dt.Columns.Contains(columnName)) + { + //如果允许有重复列名,自己做处理 + continue; + } + dt.Columns.Add(columnName, typeof(string)); + } + //开始获取数据 + int rowsCount = sheet.PhysicalNumberOfRows; + cellIndex += 1; + DataRow dr = null; + for (int i = cellIndex; i < rowsCount; i++) + { + cells = sheet.GetRow(i); + dr = dt.NewRow(); + for (int j = 0; j < dt.Columns.Count; j++) + { + //这里可以判断数据类型 + switch (cells.GetCell(j).CellType) + { + case CellType.String: + dr[j] = cells.GetCell(j).StringCellValue; + break; + case CellType.Numeric: + dr[j] = cells.GetCell(j).NumericCellValue.ToString(); + break; + case CellType.Unknown: + dr[j] = cells.GetCell(j).StringCellValue; + break; + } + } + dt.Rows.Add(dr); + } + return dt; + } + + /// + /// 导出Excel文件 + /// + /// 数据类型 + /// 数据实体 + /// 列对应关系,如Name->姓名 + /// 标题 + /// + public static byte[] ExportExcel(List entities, Dictionary dicColumns, string title = null) + { + if (entities.Count <= 0) + { + return null; + } + //HSSFWorkbook => xls + //XSSFWorkbook => xlsx + IWorkbook workbook = new XSSFWorkbook(); + ISheet sheet = workbook.CreateSheet("test");//名称自定义 + IRow cellsColumn = null; + IRow cellsData = null; + //获取实体属性名 + PropertyInfo[] properties = entities[0].GetType().GetProperties(); + int cellsIndex = 0; + //标题 + if (!string.IsNullOrEmpty(title)) + { + ICellStyle style = workbook.CreateCellStyle(); + //边框 + style.BorderBottom = BorderStyle.Dotted; + style.BorderLeft = BorderStyle.Hair; + style.BorderRight = BorderStyle.Hair; + style.BorderTop = BorderStyle.Dotted; + //水平对齐 + style.Alignment = HorizontalAlignment.Left; + + //垂直对齐 + style.VerticalAlignment = VerticalAlignment.Center; + + //设置字体 + IFont font = workbook.CreateFont(); + font.FontHeightInPoints = 10; + font.FontName = "微软雅黑"; + style.SetFont(font); + + IRow cellsTitle = sheet.CreateRow(0); + cellsTitle.CreateCell(0).SetCellValue(title); + cellsTitle.RowStyle = style; + //合并单元格 + sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 1, 0, dicColumns.Count - 1)); + cellsIndex = 2; + } + //列名 + cellsColumn = sheet.CreateRow(cellsIndex); + int index = 0; + Dictionary columns = new Dictionary(); + foreach (var item in dicColumns) + { + cellsColumn.CreateCell(index).SetCellValue(item.Value); + columns.Add(item.Value, index); + index++; + } + cellsIndex += 1; + //数据 + foreach (var item in entities) + { + cellsData = sheet.CreateRow(cellsIndex); + for (int i = 0; i < properties.Length; i++) + { + if (!dicColumns.ContainsKey(properties[i].Name)) continue; + //这里可以也根据数据类型做不同的赋值,也可以根据不同的格式参考上面的ICellStyle设置不同的样式 + object[] entityValues = new object[properties.Length]; + entityValues[i] = properties[i].GetValue(item); + //获取对应列下标 + index = columns[dicColumns[properties[i].Name]]; + cellsData.CreateCell(index).SetCellValue(entityValues[i].ToString()); + } + cellsIndex++; + } + + byte[] buffer = null; + using (MemoryStream ms = new MemoryStream()) + { + workbook.Write(ms); + buffer = ms.GetBuffer(); + ms.Close(); + } + + return buffer; + } + } +} diff --git a/Yi.Framework/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj b/Yi.Framework/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj index a8cf0338..6af582f3 100644 --- a/Yi.Framework/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj +++ b/Yi.Framework/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj @@ -15,6 +15,7 @@ +