feat: 完成excel导出操作

This commit is contained in:
橙子
2024-01-02 23:26:05 +08:00
parent 0aac42e19b
commit e3108f7382
6 changed files with 66 additions and 23 deletions

View File

@@ -3,6 +3,8 @@ using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Json;
using Yi.Framework.Core.Extensions;
using static System.Net.WebRequestMethods;
namespace Yi.Framework.AspNetCore.Microsoft.AspNetCore.Middlewares
{
@@ -10,7 +12,20 @@ namespace Yi.Framework.AspNetCore.Microsoft.AspNetCore.Middlewares
{
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
context.Response.OnStarting(() =>
{
if (context.Response.StatusCode == StatusCodes.Status200OK
&& context.Response.Headers["Content-Type"].ToString() == "application/vnd.ms-excel")
{
context.FileAttachmentHandle($"{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")}.xlsx");
}
return Task.CompletedTask;
});
await next(context);
}
}
}

View File

@@ -1,16 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Validation;
namespace Yi.Framework.Ddd.Application
{
@@ -65,6 +59,11 @@ namespace Yi.Framework.Ddd.Application
{
}
/// <summary>
/// 多查
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public override async Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input)
{
List<TEntity>? entites = null;
@@ -84,7 +83,7 @@ namespace Yi.Framework.Ddd.Application
}
/// <summary>
/// 偷梁换柱
/// 多删
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
@@ -93,6 +92,12 @@ namespace Yi.Framework.Ddd.Application
{
await Repository.DeleteManyAsync(id);
}
/// <summary>
/// 偷梁换柱
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[RemoteService(isEnabled: false)]
public override Task DeleteAsync(TKey id)
{
@@ -100,8 +105,11 @@ namespace Yi.Framework.Ddd.Application
}
/// <summary>
/// 导出excel
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task<IActionResult> GetExportExcelAsync(TGetListInput input)
{
if (input is IPagedResultRequest paged)
@@ -112,20 +120,28 @@ namespace Yi.Framework.Ddd.Application
var output = await this.GetListAsync(input);
var dirPath = $"/wwwroot/temp";
var filePath = $"{dirPath}/{Guid.NewGuid()}.xlsx";
var fileName = $"{typeof(TEntity).Name}_{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")}_{Guid.NewGuid()}";
var filePath = $"{dirPath}/{fileName}.xlsx";
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
MiniExcel.SaveAs(filePath, output.Items);
return new FileStreamResult(File.OpenRead(filePath), "application/vnd.ms-excel");
return new PhysicalFileResult(filePath, "application/vnd.ms-excel");
}
public virtual async Task PostImportExcelAsync()
/// <summary>
/// 导入excle
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task PostImportExcelAsync(List<TCreateInput> input)
{
var entities = input.Select(x => MapToEntity(x)).ToList();
await Repository.DeleteManyAsync(entities.Select(x => x.Id));
await Repository.InsertManyAsync(entities);
}
}
}

View File

@@ -34,7 +34,7 @@ namespace Yi.Framework.Rbac.Application.Services
private ISqlSugarRepository<RoleDeptEntity> _roleDeptRepository;
private ISqlSugarRepository<UserRoleEntity> _userRoleRepository;
[UnitOfWork]
public async Task UpdateDataScpoceAsync(UpdateDataScpoceInput input)
{
//只有自定义的需要特殊处理
@@ -66,7 +66,6 @@ namespace Yi.Framework.Rbac.Application.Services
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[UnitOfWork]
public override async Task<RoleGetOutputDto> CreateAsync(RoleCreateInputVo input)
{
RoleGetOutputDto outputDto;
@@ -88,7 +87,6 @@ namespace Yi.Framework.Rbac.Application.Services
/// <param name="id"></param>
/// <param name="input"></param>
/// <returns></returns>
[UnitOfWork]
public override async Task<RoleGetOutputDto> UpdateAsync(Guid id, RoleUpdateInputVo input)
{
var dto = new RoleGetOutputDto();

View File

@@ -3,7 +3,6 @@ using SqlSugar;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.EventBus.Local;
using Volo.Abp.Uow;
using Volo.Abp.Users;
using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.User;
@@ -82,7 +81,7 @@ namespace Yi.Framework.Rbac.Application.Services
/// <param name="input"></param>
/// <returns></returns>
[OperLog("添加用户", OperEnum.Insert)]
[UnitOfWork]
[Permission("system:user:add")]
public async override Task<UserGetOutputDto> CreateAsync(UserCreateInputVo input)
{
if (string.IsNullOrEmpty(input.Password))
@@ -131,7 +130,7 @@ namespace Yi.Framework.Rbac.Application.Services
/// <param name="input"></param>
/// <returns></returns>
[OperLog("更新用户", OperEnum.Update)]
[UnitOfWork]
[Permission("system:user:update")]
public async override Task<UserGetOutputDto> UpdateAsync(Guid id, UserUpdateInputVo input)
{
if (await _repository.IsAnyAsync(u => input.UserName!.Equals(u.UserName) && !id.Equals(u.Id)))
@@ -180,6 +179,7 @@ namespace Yi.Framework.Rbac.Application.Services
/// <returns></returns>
[Route("user/{id}/{state}")]
[OperLog("更新用户状态", OperEnum.Update)]
[Permission("system:user:update")]
public async Task<UserGetOutputDto> UpdateStateAsync([FromRoute] Guid id, [FromRoute] bool state)
{
var entity = await _repository.GetByIdAsync(id);
@@ -197,5 +197,17 @@ namespace Yi.Framework.Rbac.Application.Services
{
await base.DeleteAsync(id);
}
[Permission("system:user:export")]
public override Task<IActionResult> GetExportExcelAsync(UserGetListInputVo input)
{
return base.GetExportExcelAsync(input);
}
[Permission("system:user:import")]
public override Task PostImportExcelAsync(List<UserCreateInputVo> input)
{
return base.PostImportExcelAsync(input);
}
}
}

View File

@@ -39,7 +39,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="logs\" />
<Folder Include="wwwroot\icon\" />
</ItemGroup>

View File

@@ -153,7 +153,10 @@ namespace Yi.Abp.Web
//swagger
app.UseYiSwagger();
//请求处理
app.UseYiApiHandlinge();
//静态资源
app.UseStaticFiles("/api/app/wwwroot");
app.UseDefaultFiles();