完善表格模块
This commit is contained in:
@@ -3,12 +3,14 @@ using Microsoft.AspNetCore.Http;
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Yi.Framework.Common.Models;
|
using Yi.Framework.Common.Models;
|
||||||
using Yi.Framework.DTOModel;
|
using Yi.Framework.DTOModel;
|
||||||
using Yi.Framework.Interface;
|
using Yi.Framework.Interface;
|
||||||
using Yi.Framework.Model.Models;
|
using Yi.Framework.Model.Models;
|
||||||
|
using Yi.Framework.WebCore;
|
||||||
|
|
||||||
namespace Yi.Framework.ApiMicroservice.Controllers
|
namespace Yi.Framework.ApiMicroservice.Controllers
|
||||||
{
|
{
|
||||||
@@ -24,8 +26,22 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
|||||||
}
|
}
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<Result> GetRole()
|
public async Task<Result> GetRole()
|
||||||
|
{
|
||||||
|
return Result.Success().SetData(await _roleService.GetAllEntitiesTrueAsync());
|
||||||
|
}
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<IActionResult> GetRoleFlie()
|
||||||
{
|
{
|
||||||
return Result.Success().SetData(await _roleService.GetAllEntitiesTrueAsync());
|
var roleList = await _roleService.GetAllEntitiesTrueAsync();
|
||||||
|
Dictionary<string, string> 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");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -91,12 +107,13 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用于给角色设置菜单的时候,点击一个角色,显示这个角色拥有的并列的菜单
|
/// 用于给角色设置菜单的时候,点击一个角色,显示这个角色拥有的并列的菜单
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="roleId"></param>
|
/// <param name="role"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<Result> GetTopMenusByRoleId(int roleId)
|
public async Task<Result> GetTopMenusByRoleId(role role)
|
||||||
{
|
{
|
||||||
return Result.Success().SetData(await _roleService.GetTopMenusByRoleId(roleId) ); ;
|
|
||||||
|
return Result.Success().SetData(await _roleService.GetTopMenusByRoleId(role.id) ); ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ namespace Yi.Framework.Interface
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId"></param>
|
/// <param name="userId"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<List<menu>> GetMenuByHttpUser(int userId);
|
Task<menu> GetMenuByHttpUser(int userId);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 根据路由获取菜单
|
/// 根据路由获取菜单
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ namespace Yi.Framework.Service
|
|||||||
|
|
||||||
public async Task<List<menu>> GetTopMenusByTopMenuIds(List<int> menuIds)
|
public async Task<List<menu>> GetTopMenusByTopMenuIds(List<int> menuIds)
|
||||||
{
|
{
|
||||||
return await _DbRead.Set<menu>().Where(u => menuIds.Contains(u.id)).ToListAsync();
|
return await _DbRead.Set<menu>().AsNoTracking().Where(u => menuIds.Contains(u.id)).ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<menu> SetMouldByMenu(int id1,int id2)
|
public async Task<menu> SetMouldByMenu(int id1,int id2)
|
||||||
|
|||||||
@@ -45,9 +45,9 @@ namespace Yi.Framework.Service
|
|||||||
}
|
}
|
||||||
public async Task<List<menu>> GetTopMenusByRoleId(int roleId)
|
public async Task<List<menu>> GetTopMenusByRoleId(int roleId)
|
||||||
{
|
{
|
||||||
var role_data = await _Db.Set<role>().Where(u => u.id == roleId).FirstOrDefaultAsync();
|
var role_data = await _Db.Set<role>().Include(u=>u.menus).Where(u => u.id == roleId).FirstOrDefaultAsync();
|
||||||
var menuList = role_data.menus.Where(u => u.is_delete == Normal).ToList();
|
var menuList = role_data.menus.Where(u => u.is_delete == Normal).ToList();
|
||||||
menuList.ForEach(u => u.children = null);
|
|
||||||
return menuList;
|
return menuList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ namespace Yi.Framework.Service
|
|||||||
return menuList;
|
return menuList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<menu>> GetMenuByHttpUser(int userId)
|
public async Task<menu> GetMenuByHttpUser(int userId)
|
||||||
{
|
{
|
||||||
var user_data = await GetUserById(userId);
|
var user_data = await GetUserById(userId);
|
||||||
List<menu> menuList = new();
|
List<menu> menuList = new();
|
||||||
@@ -78,7 +78,7 @@ namespace Yi.Framework.Service
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return TopMenuBuild2(TopMenuBuild(endMenu, allMenuIds));
|
return TopMenuBuild2(TopMenuBuild(endMenu, allMenuIds))[0];
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 这种就命名的话,改成MenuIconAndShowBuild
|
/// 这种就命名的话,改成MenuIconAndShowBuild
|
||||||
|
|||||||
228
Yi.Framework/Yi.Framework.WebCore/Excel.cs
Normal file
228
Yi.Framework/Yi.Framework.WebCore/Excel.cs
Normal file
@@ -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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 读取Excel多Sheet数据
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filePath">文件路径</param>
|
||||||
|
/// <param name="sheetName">Sheet名</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 读取Excel信息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="workbook">工作区</param>
|
||||||
|
/// <param name="sheet">sheet</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
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<string> listColumns = new List<string>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 导出Excel文件
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">数据类型</typeparam>
|
||||||
|
/// <param name="entities">数据实体</param>
|
||||||
|
/// <param name="dicColumns">列对应关系,如Name->姓名</param>
|
||||||
|
/// <param name="title">标题</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static byte[] ExportExcel<T>(List<T> entities, Dictionary<string, string> 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<string, int> columns = new Dictionary<string, int>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.2.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.2.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="5.0.4" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="5.0.4" />
|
||||||
|
<PackageReference Include="NPOI" Version="2.5.5" />
|
||||||
<PackageReference Include="Quartz.AspNetCore" Version="3.3.3" />
|
<PackageReference Include="Quartz.AspNetCore" Version="3.3.3" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user