From adc121adaf80420da3f79e31184e2fb1bdd342e7 Mon Sep 17 00:00:00 2001 From: Max Zheng <34936931+maxzh1999tw@users.noreply.github.com> Date: Sun, 17 Dec 2023 09:09:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E6=AA=94=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Excely.Example.Console.csproj | 15 --- .../Exemples/SimpleClassListToXlsxExemple.cs | 80 --------------- .../XlsxImportErrorHandlingExemple.cs | 99 ------------------- Excely.Example.Console/Models/SimpleClass.cs | 19 ---- Excely.Example.Console/Program.cs | 3 - .../Utilities/PropertyExtension.cs | 14 --- 6 files changed, 230 deletions(-) delete mode 100644 Excely.Example.Console/Excely.Example.Console.csproj delete mode 100644 Excely.Example.Console/Exemples/SimpleClassListToXlsxExemple.cs delete mode 100644 Excely.Example.Console/Exemples/XlsxImportErrorHandlingExemple.cs delete mode 100644 Excely.Example.Console/Models/SimpleClass.cs delete mode 100644 Excely.Example.Console/Program.cs delete mode 100644 Excely.Example.Console/Utilities/PropertyExtension.cs diff --git a/Excely.Example.Console/Excely.Example.Console.csproj b/Excely.Example.Console/Excely.Example.Console.csproj deleted file mode 100644 index 0d2c2be..0000000 --- a/Excely.Example.Console/Excely.Example.Console.csproj +++ /dev/null @@ -1,15 +0,0 @@ - - - - Exe - net6.0 - enable - enable - - - - - - - - diff --git a/Excely.Example.Console/Exemples/SimpleClassListToXlsxExemple.cs b/Excely.Example.Console/Exemples/SimpleClassListToXlsxExemple.cs deleted file mode 100644 index 2252224..0000000 --- a/Excely.Example.Console/Exemples/SimpleClassListToXlsxExemple.cs +++ /dev/null @@ -1,80 +0,0 @@ -using Excely.ClosedXML.Shaders; -using Excely.ClosedXML.Workflows; -using Excely.Example.Console.Models; -using Excely.Example.Console.Utilities; -using Excely.Shaders; -using Excely.TableConverters; -using Excely.TableFactories; -using Excely.Workflows; - -namespace Excely.Example.Console.Exemples -{ - internal class SimpleClassListToXlsxExemple - { - public static void Demo() - { - #region === 匯出 === - var list = GetList(); - var exportOption = new ClassListTableFactoryOptions - { - PropertyNamePolicy = p => p.GetDisplayName(), - CustomValuePolicy = (p, obj) => p.Name switch - { - nameof(SimpleClass.DateTimeField) => obj.DateTimeField?.ToString("yyyy/MM/dd"), - nameof(SimpleClass.BoolField) => obj.BoolField == null ? null : (obj.BoolField.Value ? "是" : "否"), - _ => p.GetValue(obj) - }, - }; - - var shaders = new IShader[] - { - new SchemaFilterShader(), - new CellFittingShader(), - }; - - var exporter = ExcelyExporter.FromClassList(exportOption, shaders); - using var excel = exporter.ToExcel(list); - excel.SaveAs("SimpleClassListToXlsxExemple.xlsx"); - #endregion - - #region === 匯入 === - var worksheet = excel.Worksheets.First(); - var importer = new XlsxImporter(); - - var importOption = new ClassListTableConverterOptions - { - PropertyNamePolicy = p => p.GetDisplayName(), - PropertyValueSettingPolicy = (p, value) => p.Name switch - { - nameof(SimpleClass.DateTimeField) => value != null ? DateTime.Parse(value.ToString()) : null, - nameof(SimpleClass.BoolField) => value != null ? value?.ToString() == "是" : null, - _ => value - }, - }; - - var importedList = importer.ToClassList(worksheet, importOption); - #endregion - } - - private static IEnumerable GetList() - { - return new List() - { - new SimpleClass() - { - Id = 1, - StringField = "Text", - DateTimeField = DateTime.Now, - BoolField = true, - }, - new SimpleClass() - { - Id = 2, - StringField = null, - DateTimeField = null, - BoolField = null, - }, - }; - } - } -} diff --git a/Excely.Example.Console/Exemples/XlsxImportErrorHandlingExemple.cs b/Excely.Example.Console/Exemples/XlsxImportErrorHandlingExemple.cs deleted file mode 100644 index b0564d4..0000000 --- a/Excely.Example.Console/Exemples/XlsxImportErrorHandlingExemple.cs +++ /dev/null @@ -1,99 +0,0 @@ -using ClosedXML.Excel; -using Excely.ClosedXML.Shaders; -using Excely.ClosedXML.Workflows; -using Excely.Example.Console.Models; -using Excely.Example.Console.Utilities; -using Excely.Shaders; -using Excely.TableConverters; -using Excely.TableFactories; -using Excely.Workflows; - -namespace Excely.Example.Console.Exemples -{ - internal class XlsxImportErrorHandlingExemple - { - private static IEnumerable GetList() - { - return new List() - { - new SimpleClass() - { - Id = 1, - StringField = "比較長的文字欄位111111111111111111", - DateTimeField = DateTime.Now, - BoolField = true, - }, - new SimpleClass() - { - Id = 2, - StringField = "比較長的文字欄位222222222222222222", - DateTimeField = DateTime.Now, - BoolField = false, - }, - }; - } - - private static XLWorkbook GetExampleWorkbook() - { - var list = GetList(); - var exportOption = new ClassListTableFactoryOptions - { - PropertyNamePolicy = p => p.GetDisplayName(), - CustomValuePolicy = (p, obj) => p.Name switch - { - // 故意設定錯誤的日期格式 - nameof(SimpleClass.DateTimeField) => obj.DateTimeField?.ToString("yyyy/yyyy/dd"), - nameof(SimpleClass.BoolField) => obj.BoolField == null ? null : (obj.BoolField.Value ? "是" : "否"), - _ => p.GetValue(obj) - }, - }; - - var shaders = new IShader[] - { - new SchemaFilterShader(), - new CellFittingShader(), - }; - - var exporter = ExcelyExporter.FromClassList(exportOption, shaders); - return exporter.ToExcel(list); - } - - public static void Demo() - { - using var excel = GetExampleWorkbook(); - var worksheet = excel.Worksheets.First(); - var importer = new XlsxImporter(); - var errorDict = new Dictionary(); - - var importOption = new ClassListTableConverterOptions - { - PropertyNamePolicy = p => p.GetDisplayName(), - PropertyValueSettingPolicy = (p, value) => p.Name switch - { - nameof(SimpleClass.BoolField) => value != null ? value?.ToString() == "是" : null, - _ => value - }, - ErrorHandlingPolicy = (cellLocation, obj, p, value, ex) => - { - switch (p.Name) - { - case nameof(SimpleClass.DateTimeField): - errorDict.Add(cellLocation, "錯誤的日期格式"); - break; - default: - errorDict.Add(cellLocation, ex.Message); - break; - }; - return true; - } - }; - - var importedList = importer.ToClassList(worksheet, importOption); - new ErrorMarkShader(errorDict).Excute(worksheet); - new CellFittingShader().Excute(worksheet); - new SchemaFilterShader().Excute(worksheet); - new TableThemeShader().Excute(worksheet); - excel.SaveAs("XlsxImportErrorHandlingExemple.xlsx"); - } - } -} diff --git a/Excely.Example.Console/Models/SimpleClass.cs b/Excely.Example.Console/Models/SimpleClass.cs deleted file mode 100644 index 11455a8..0000000 --- a/Excely.Example.Console/Models/SimpleClass.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace Excely.Example.Console.Models -{ - internal class SimpleClass - { - [Display(Name = "序號")] - public int Id { get; set; } - - [Display(Name = "文字欄位")] - public string? StringField { get; set; } - - [Display(Name = "是/否")] - public bool? BoolField { get; set; } - - [Display(Name = "日期欄位")] - public DateTime? DateTimeField { get; set; } - } -} diff --git a/Excely.Example.Console/Program.cs b/Excely.Example.Console/Program.cs deleted file mode 100644 index 120f034..0000000 --- a/Excely.Example.Console/Program.cs +++ /dev/null @@ -1,3 +0,0 @@ -using Excely.Example.Console.Exemples; - -XlsxImportErrorHandlingExemple.Demo(); \ No newline at end of file diff --git a/Excely.Example.Console/Utilities/PropertyExtension.cs b/Excely.Example.Console/Utilities/PropertyExtension.cs deleted file mode 100644 index cf97f8f..0000000 --- a/Excely.Example.Console/Utilities/PropertyExtension.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System.ComponentModel.DataAnnotations; -using System.Reflection; - -namespace Excely.Example.Console.Utilities -{ - internal static class PropertyExtension - { - public static string GetDisplayName(this PropertyInfo propertyInfo) - { - var displayAttribute = propertyInfo.GetCustomAttribute(); - return displayAttribute?.Name ?? propertyInfo.Name; - } - } -}