From ddcda67d02749c01b35143b54872e3a72eeba2a1 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Wed, 5 Jul 2023 16:14:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AF=BC=E5=85=A5=E5=AF=BC?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/GenericController.cs | 29 ++++-- .../demo/src/WTA.Shared/Data/BaseDbContext.cs | 2 +- .../ClosedXmlExportImportService.cs | 52 +++++++++-- .../Resources/{calibril.ttf => font.ttf} | Bin docs/demo/src/WTA.Shared/WTA.Shared.csproj | 8 ++ .../src/WTA/wwwroot/components/list/index.js | 86 +++++++++++++----- docs/demo/src/WTA/wwwroot/request/index.js | 9 +- docs/demo/src/WTA/wwwroot/utils/index.js | 6 +- 8 files changed, 155 insertions(+), 37 deletions(-) rename docs/demo/src/WTA.Shared/Resources/{calibril.ttf => font.ttf} (100%) diff --git a/docs/demo/src/WTA.Shared/Controllers/GenericController.cs b/docs/demo/src/WTA.Shared/Controllers/GenericController.cs index 5b50a031..44f90945 100644 --- a/docs/demo/src/WTA.Shared/Controllers/GenericController.cs +++ b/docs/demo/src/WTA.Shared/Controllers/GenericController.cs @@ -1,12 +1,15 @@ using System.Globalization; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using WTA.Shared.Application; using WTA.Shared.Attributes; using WTA.Shared.Data; using WTA.Shared.Domain; +using WTA.Shared.ExportImport; using WTA.Shared.Extensions; using WTA.Shared.Mappers; @@ -41,10 +44,6 @@ public class GenericController)); var query = BuildQuery(model, isTree); - foreach (var item in model.Filters) - { - query = query.Where(string.Format(CultureInfo.InvariantCulture, item.Operator, item.Property.ToPascalCase()), item.Value); - } model.TotalCount = query.Count(); if (!string.IsNullOrEmpty(model.OrderBy)) { @@ -73,11 +72,14 @@ public class GenericController.ParentId)},{nameof(BaseEntity.Order)},{nameof(BaseEntity.CreatedOn)}"; } - return query; } @@ -178,6 +180,20 @@ public class GenericController(); + return exportImportService.GetImportTemplate(); + } + catch (Exception ex) + { + return Problem(ex.Message); + } + } + [HttpPost, Multiple, Order(-2), HtmlClass("el-button--primary")] public virtual IActionResult Import(IFormFile importexcelfile) { @@ -206,7 +222,8 @@ public class GenericController(); + return exportImportService.Export(query.ToList().Select(o => o.ToObject()).ToList()); } catch (Exception ex) { diff --git a/docs/demo/src/WTA.Shared/Data/BaseDbContext.cs b/docs/demo/src/WTA.Shared/Data/BaseDbContext.cs index 291495a5..581b6d13 100644 --- a/docs/demo/src/WTA.Shared/Data/BaseDbContext.cs +++ b/docs/demo/src/WTA.Shared/Data/BaseDbContext.cs @@ -112,7 +112,7 @@ public abstract class BaseDbContext : DbContext where T : DbContext if (entityType.IsAssignableTo(typeof(BaseEntity))) { //软删除、租户过滤 - this.GetType().GetMethod(nameof(this.CreateQueryFilter))?.MakeGenericMethod(entityType).Invoke(this, new object[] { modelBuilder }); + //this.GetType().GetMethod(nameof(this.CreateQueryFilter))?.MakeGenericMethod(entityType).Invoke(this, new object[] { modelBuilder }); // //基类 entityTypeBuilder.HasKey(nameof(BaseEntity.Id)); diff --git a/docs/demo/src/WTA.Shared/ExportImport/ClosedXmlExportImportService.cs b/docs/demo/src/WTA.Shared/ExportImport/ClosedXmlExportImportService.cs index 02dfcfc7..c8924169 100644 --- a/docs/demo/src/WTA.Shared/ExportImport/ClosedXmlExportImportService.cs +++ b/docs/demo/src/WTA.Shared/ExportImport/ClosedXmlExportImportService.cs @@ -5,8 +5,8 @@ using ClosedXML.Excel; using ClosedXML.Graphics; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; -using Microsoft.OpenApi.Extensions; using WTA.Shared.Attributes; +using WTA.Shared.Extensions; namespace WTA.Shared.ExportImport; @@ -18,7 +18,7 @@ public class ClosedXmlExportImportService : IExportImportService static ClosedXmlExportImportService() { - using var fallbackFontStream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"{WebApp.Current.Prefix}.Infrastructure.Resources.calibril.ttf"); + using var fallbackFontStream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"{typeof(WTA.Shared.Resources.Resource).Namespace}.font.ttf"); LoadOptions.DefaultGraphicEngine = DefaultGraphicEngine.CreateWithFontsAndSystemFonts(fallbackFontStream); } @@ -32,12 +32,31 @@ public class ClosedXmlExportImportService : IExportImportService try { using var workbook = new XLWorkbook(); - var name = typeof(TExportModel).GetCustomAttribute()?.Name ?? typeof(TExportModel).Name; + var name = typeof(TExportModel).GetDisplayName(); var fileName = $"{name}_导出.xlsx"; var ws = workbook.Worksheets.Add(name); - ws.Style.Font.FontName = "宋体"; // - //Internal + var type = typeof(TExportModel); + var propertyList = GetPropertiesForImportModel(type); + var rowIndex = 1; + for (var i = 0; i < propertyList.Length; i++) + { + var property = propertyList[i]; + var columnIndex = i + 1; + var cell = ws.Cell(1, columnIndex); + cell.Value = property.GetDisplayName(); + } + list.ForEach(model => + { + rowIndex++; + for (var i = 0; i < propertyList.Length; i++) + { + var property = propertyList[i]; + var columnIndex = i + 1; + var cell = ws.Cell(rowIndex, columnIndex); + cell.Value = property.GetValue(model)?.ToString(); + } + }); // var stream = new MemoryStream(); workbook.SaveAs(stream); @@ -57,8 +76,27 @@ public class ClosedXmlExportImportService : IExportImportService public FileContentResult GetImportTemplate() { - //TModelType=>File - throw new NotImplementedException(); + using var workbook = new XLWorkbook(); + var type = typeof(TImportModel); + var name = type.GetDisplayName(); + var fileName = $"{name}_导入模板.xlsx"; + var ws = workbook.Worksheets.Add(name); + var properties = GetPropertiesForImportModel(type); + for (var i = 0; i < properties.Length; i++) + { + var property = properties[i]; + var headerName = property.GetDisplayName(); + var cell = ws.Cell(1, i + 1); + cell.Value = headerName; + } + var stream = new MemoryStream(); + workbook.SaveAs(stream); + stream.Seek(0, SeekOrigin.Begin); + var result = new FileContentResult(stream.ToArray(), ContentType) + { + FileDownloadName = fileName + }; + return result; } public IList Import(byte[] bytes) diff --git a/docs/demo/src/WTA.Shared/Resources/calibril.ttf b/docs/demo/src/WTA.Shared/Resources/font.ttf similarity index 100% rename from docs/demo/src/WTA.Shared/Resources/calibril.ttf rename to docs/demo/src/WTA.Shared/Resources/font.ttf diff --git a/docs/demo/src/WTA.Shared/WTA.Shared.csproj b/docs/demo/src/WTA.Shared/WTA.Shared.csproj index 35afd276..d85b278d 100644 --- a/docs/demo/src/WTA.Shared/WTA.Shared.csproj +++ b/docs/demo/src/WTA.Shared/WTA.Shared.csproj @@ -1,5 +1,13 @@ + + + + + + + + diff --git a/docs/demo/src/WTA/wwwroot/components/list/index.js b/docs/demo/src/WTA/wwwroot/components/list/index.js index 0e7672e9..958a933e 100644 --- a/docs/demo/src/WTA/wwwroot/components/list/index.js +++ b/docs/demo/src/WTA/wwwroot/components/list/index.js @@ -190,7 +190,12 @@ export default { - + @@ -219,18 +224,27 @@ export default {