Explorar o código

😎代码清理与优化

zuohuaijun hai 1 ano
pai
achega
07acd05afc

+ 1 - 1
Admin.NET/Admin.NET.Core/Utils/BaseImportInput.cs

@@ -7,7 +7,7 @@
 namespace Admin.NET.Core;
 namespace Admin.NET.Core;
 
 
 /// <summary>
 /// <summary>
-/// 数据导入输入参数基类
+/// 数据导入输入参数
 /// </summary>
 /// </summary>
 public class BaseImportInput
 public class BaseImportInput
 {
 {

+ 1 - 1
Admin.NET/Admin.NET.Core/Utils/CommonUtil.cs

@@ -247,7 +247,7 @@ public static class CommonUtil
         }
         }
         return res.Data;
         return res.Data;
     }
     }
-    
+
     /// <summary>
     /// <summary>
     /// 导入数据Excel
     /// 导入数据Excel
     /// </summary>
     /// </summary>

+ 6 - 6
Admin.NET/Admin.NET.Core/Utils/ExcelHelper.cs

@@ -37,7 +37,7 @@ public class ExcelHelper
                     }
                     }
                 }));
                 }));
             });
             });
-            
+
             // 等待所有标记验证信息任务完成
             // 等待所有标记验证信息任务完成
             Task.WhenAll(tasks).GetAwaiter().GetResult();
             Task.WhenAll(tasks).GetAwaiter().GetResult();
 
 
@@ -82,11 +82,11 @@ public class ExcelHelper
     {
     {
         using var package = new ExcelPackage((ExportData(list, filename) as XlsxFileResult)!.Stream);
         using var package = new ExcelPackage((ExportData(list, filename) as XlsxFileResult)!.Stream);
         var worksheet = package.Workbook.Worksheets[0];
         var worksheet = package.Workbook.Worksheets[0];
-        
+
         foreach (var prop in typeof(T).GetProperties())
         foreach (var prop in typeof(T).GetProperties())
         {
         {
             var propType = prop.PropertyType;
             var propType = prop.PropertyType;
-            
+
             var headerAttr = prop.GetCustomAttribute<ExporterHeaderAttribute>();
             var headerAttr = prop.GetCustomAttribute<ExporterHeaderAttribute>();
             var isNullableEnum = propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable<>) && Nullable.GetUnderlyingType(propType).IsEnum();
             var isNullableEnum = propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable<>) && Nullable.GetUnderlyingType(propType).IsEnum();
             if (isNullableEnum) propType = Nullable.GetUnderlyingType(propType);
             if (isNullableEnum) propType = Nullable.GetUnderlyingType(propType);
@@ -97,7 +97,7 @@ public class ExcelHelper
             foreach (var item in worksheet.Cells[1, 1, 1, worksheet.Dimension.End.Column])
             foreach (var item in worksheet.Cells[1, 1, 1, worksheet.Dimension.End.Column])
                 if (++columnIndex > 0 && item.Text.Equals(headerAttr.DisplayName)) break;
                 if (++columnIndex > 0 && item.Text.Equals(headerAttr.DisplayName)) break;
             if (columnIndex <= 0) continue;
             if (columnIndex <= 0) continue;
-            
+
             // 优先从代理函数中获取下列列表,若为空且字段为枚举型,则填充枚举项为下列列表,否则不设置下列列表
             // 优先从代理函数中获取下列列表,若为空且字段为枚举型,则填充枚举项为下列列表,否则不设置下列列表
             var dataList = addListValidationFun?.Invoke(worksheet, prop)?.ToList();
             var dataList = addListValidationFun?.Invoke(worksheet, prop)?.ToList();
             if (dataList == null && propType.IsEnum()) dataList = propType.EnumToList()?.Select(it => it.Describe).ToList();
             if (dataList == null && propType.IsEnum()) dataList = propType.EnumToList()?.Select(it => it.Describe).ToList();
@@ -112,9 +112,9 @@ public class ExcelHelper
             validation.ErrorTitle = "无效输入";
             validation.ErrorTitle = "无效输入";
             validation.Error = "请从列表中选择一个有效的选项";
             validation.Error = "请从列表中选择一个有效的选项";
         }
         }
-        
+
         package.Save();
         package.Save();
         package.Stream.Position = 0;
         package.Stream.Position = 0;
         return new XlsxFileResult(stream: package.Stream, fileDownloadName: $"{filename}-{DateTime.Now:yyyy-MM-dd_HHmmss}");
         return new XlsxFileResult(stream: package.Stream, fileDownloadName: $"{filename}-{DateTime.Now:yyyy-MM-dd_HHmmss}");
     }
     }
-}
+}

+ 6 - 8
Admin.NET/Admin.NET.Core/Utils/XlsxFileResult.cs

@@ -12,6 +12,9 @@ namespace Admin.NET.Core;
 /// <typeparam name="T"></typeparam>
 /// <typeparam name="T"></typeparam>
 public class XlsxFileResult<T> : XlsxFileResultBase where T : class, new()
 public class XlsxFileResult<T> : XlsxFileResultBase where T : class, new()
 {
 {
+    public string FileDownloadName { get; }
+    public ICollection<T> Data { get; }
+
     /// <summary>
     /// <summary>
     ///
     ///
     /// </summary>
     /// </summary>
@@ -23,9 +26,6 @@ public class XlsxFileResult<T> : XlsxFileResultBase where T : class, new()
         Data = data;
         Data = data;
     }
     }
 
 
-    public string FileDownloadName { get; }
-    public ICollection<T> Data { get; }
-
     public override async Task ExecuteResultAsync(ActionContext context)
     public override async Task ExecuteResultAsync(ActionContext context)
     {
     {
         var exporter = new ExcelExporter();
         var exporter = new ExcelExporter();
@@ -63,12 +63,10 @@ public class XlsxFileResult : XlsxFileResultBase
         FileDownloadName = fileDownloadName;
         FileDownloadName = fileDownloadName;
     }
     }
 
 
-
     public Stream Stream { get; protected set; }
     public Stream Stream { get; protected set; }
     public string FileDownloadName { get; protected set; }
     public string FileDownloadName { get; protected set; }
 
 
-
-    public async override Task ExecuteResultAsync(ActionContext context)
+    public override async Task ExecuteResultAsync(ActionContext context)
     {
     {
         await DownloadExcelFileAsync(context, Stream, FileDownloadName);
         await DownloadExcelFileAsync(context, Stream, FileDownloadName);
     }
     }
@@ -101,7 +99,7 @@ public class XlsxFileResultBase : ActionResult
             downloadFileName += ".xlsx";
             downloadFileName += ".xlsx";
         }
         }
 
 
-        context.HttpContext.Response.Headers.Append("Content-Disposition", new[] { "attachment; filename=" +HttpUtility.UrlEncode(downloadFileName) });
+        context.HttpContext.Response.Headers.Append("Content-Disposition", new[] { "attachment; filename=" + HttpUtility.UrlEncode(downloadFileName) });
         await stream.CopyToAsync(context.HttpContext.Response.Body);
         await stream.CopyToAsync(context.HttpContext.Response.Body);
     }
     }
-}
+}