数据模板导入、数据导出
This commit is contained in:
@@ -465,6 +465,18 @@
|
||||
用户管理
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.UserController.Template">
|
||||
<summary>
|
||||
下载模板
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.UserController.Export">
|
||||
<summary>
|
||||
导出数据
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.UserController.PageList(Yi.Framework.Model.Models.UserEntity,Yi.Framework.Common.Models.PageParModel,System.Nullable{System.Int64})">
|
||||
<summary>
|
||||
动态条件分页查询
|
||||
|
||||
@@ -5,6 +5,9 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Const;
|
||||
using Yi.Framework.Common.Enum;
|
||||
using Yi.Framework.Common.Helper;
|
||||
using Yi.Framework.Common.Models;
|
||||
using Yi.Framework.DTOModel;
|
||||
using Yi.Framework.Interface;
|
||||
@@ -25,11 +28,48 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
public class UserController : BaseSimpleRdController<UserEntity>
|
||||
{
|
||||
private IUserService _iUserService;
|
||||
|
||||
public UserController(ILogger<UserEntity> logger, IUserService iUserService) : base(logger, iUserService)
|
||||
{
|
||||
_iUserService = iUserService;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载模板
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
public IActionResult Template()
|
||||
{
|
||||
List<UserEntity> users = new();
|
||||
var fileName = nameof(UserEntity) + PathConst.DataTemplate;
|
||||
var path = ExcelHelper.DownloadImportTemplate(users, fileName, Path.Combine(PathConst.wwwroot, PathEnum.Excel.ToString()));
|
||||
var file = System.IO.File.OpenRead(path);
|
||||
return File(file, "text/plain", $"{DateTime.Now.ToString("yyyyMMddHHmmssffff") + fileName }.xlsx");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 导出数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> Export()
|
||||
{
|
||||
var users = await _iUserService._repository.GetListAsync();
|
||||
var fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + nameof(UserEntity) + PathConst.DataExport;
|
||||
var path = ExcelHelper.ExportExcel(users, fileName, Path.Combine(PathConst.wwwroot, PathEnum.Temp.ToString()));
|
||||
var file = System.IO.File.OpenRead(path);
|
||||
return File(file, "text/plain", $"{ fileName }.xlsx");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 动态条件分页查询
|
||||
/// </summary>
|
||||
@@ -39,7 +79,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[Permission("system:user:query")]
|
||||
public async Task<Result> PageList([FromQuery] UserEntity user, [FromQuery] PageParModel page,[FromQuery] long? deptId)
|
||||
public async Task<Result> PageList([FromQuery] UserEntity user, [FromQuery] PageParModel page, [FromQuery] long? deptId)
|
||||
{
|
||||
return Result.Success().SetData(await _iUserService.SelctPageList(user, page, deptId));
|
||||
}
|
||||
@@ -93,7 +133,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
[Permission("system:user:edit")]
|
||||
public async Task<Result> Update(UserInfoDto userDto)
|
||||
{
|
||||
if (await _iUserService._repository.IsAnyAsync(u => userDto.User.UserName.Equals(u.UserName)&&!userDto.User.Id.Equals(u.Id)))
|
||||
if (await _iUserService._repository.IsAnyAsync(u => userDto.User.UserName.Equals(u.UserName) && !userDto.User.Id.Equals(u.Id)))
|
||||
{
|
||||
return Result.Error("用户名已存在,修改失败!");
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="wwwroot\excel\" />
|
||||
<Folder Include="wwwroot\file\" />
|
||||
<Folder Include="wwwroot\image\" />
|
||||
</ItemGroup>
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,14 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Common.Const
|
||||
{
|
||||
public class FileConst
|
||||
{
|
||||
public const string Image = "Image";
|
||||
public const string File = "File";
|
||||
}
|
||||
}
|
||||
15
Yi.Framework.Net6/Yi.Framework.Common/Const/PathConst.cs
Normal file
15
Yi.Framework.Net6/Yi.Framework.Common/Const/PathConst.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Common.Const
|
||||
{
|
||||
public class PathConst
|
||||
{
|
||||
public const string wwwroot = "wwwroot";
|
||||
public const string DataTemplate = "_DataTemplate";
|
||||
public const string DataExport = "_DataExport";
|
||||
}
|
||||
}
|
||||
16
Yi.Framework.Net6/Yi.Framework.Common/Enum/PathEnum.cs
Normal file
16
Yi.Framework.Net6/Yi.Framework.Common/Enum/PathEnum.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Common.Enum
|
||||
{
|
||||
public enum PathEnum
|
||||
{
|
||||
Excel,
|
||||
File,
|
||||
Image,
|
||||
Temp
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,11 @@ using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 此处来源于ZrAdmin
|
||||
/// </summary>
|
||||
namespace Yi.Framework.Common.Helper
|
||||
{
|
||||
public class ExcelHelper
|
||||
@@ -18,7 +23,7 @@ namespace Yi.Framework.Common.Helper
|
||||
/// <param name="list"></param>
|
||||
/// <param name="sheetName"></param>
|
||||
/// <param name="fileName"></param>
|
||||
public static string ExportExcel<T>(List<T> list, string sheetName, string fileName,string path)
|
||||
public static string ExportExcel<T>(List<T> list, string fileName,string path,string sheetName= "default")
|
||||
{
|
||||
string sFileName = $"{fileName}.xlsx";
|
||||
string newFileName = Path.Combine(path, sFileName);
|
||||
@@ -47,16 +52,25 @@ namespace Yi.Framework.Common.Helper
|
||||
/// <param name="stream"></param>
|
||||
/// <param name="fileName">下载文件名</param>
|
||||
/// <returns></returns>
|
||||
protected string DownloadImportTemplate<T>(List<T> list, Stream stream, string fileName,string path)
|
||||
public static string DownloadImportTemplate<T>(List<T> list, string fileName,string path)
|
||||
{
|
||||
string sFileName = $"{fileName}.xlsx";
|
||||
string newFileName = Path.Combine(path, sFileName);
|
||||
|
||||
//如果文件存在,直接返回即可
|
||||
if (File.Exists(newFileName))
|
||||
{
|
||||
return newFileName;
|
||||
}
|
||||
//调试模式需要加上
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
if (!Directory.Exists(newFileName))
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(newFileName)!);
|
||||
}
|
||||
|
||||
|
||||
Stream stream = new FileStream(newFileName, FileMode.OpenOrCreate);
|
||||
using (ExcelPackage package = new(new FileInfo(newFileName)))
|
||||
{
|
||||
// 添加worksheet
|
||||
@@ -67,8 +81,9 @@ namespace Yi.Framework.Common.Helper
|
||||
worksheet.Cells.LoadFromCollection(list, true, OfficeOpenXml.Table.TableStyles.Light13);
|
||||
package.SaveAs(stream);
|
||||
}
|
||||
|
||||
return sFileName;
|
||||
stream.Dispose();
|
||||
|
||||
return newFileName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user