diff --git a/docs/README.md b/docs/README.md index d25bf80..9634686 100644 --- a/docs/README.md +++ b/docs/README.md @@ -8,6 +8,7 @@ ### 0.13.4 - [Changed] DataTable use Caption for column name first, then use columname #217 +- [New] Type Query support Enum mapping #89 ### 0.13.3 - [New] Support open with read only mode, avoid error of The process cannot access the file because it is being used by another process [#87](https://github.com/shps951023/MiniExcel/issues/87) diff --git a/docs/README.zh-CN.md b/docs/README.zh-CN.md index b4c0283..e394374 100644 --- a/docs/README.zh-CN.md +++ b/docs/README.zh-CN.md @@ -9,6 +9,7 @@ ### 0.13.4 - [Changed] DataTable 以 Caption 优先当栏位名称 #217 +- [New] Query 支持 Enum mapping #89 ### 0.13.3 - [New] 支持 Excel 单纯读取模式,避免同时改模版又运行 MiniExcel 出现错误 "The process cannot access the file because it is being used by another process" [#87](https://github.com/shps951023/MiniExcel/issues/87) diff --git a/docs/README.zh-Hant.md b/docs/README.zh-Hant.md index 8a69d85..9988e36 100644 --- a/docs/README.zh-Hant.md +++ b/docs/README.zh-Hant.md @@ -9,6 +9,7 @@ ### 0.13.4 - [Changed] DataTable 以 Caption 優先當欄位名稱 #217 +- [New] Query 支持 Enum mapping #89 ### 0.13.3 - [New] 支持 Excel 單純讀取模式,避免同時改模版又運行 MiniExcel 出現錯誤 "The process cannot access the file because it is being used by another process" [#87](https://github.com/shps951023/MiniExcel/issues/87) diff --git a/samples/xlsx/TestIssue89.xlsx b/samples/xlsx/TestIssue89.xlsx new file mode 100644 index 0000000..c13a5f7 Binary files /dev/null and b/samples/xlsx/TestIssue89.xlsx differ diff --git a/src/MiniExcel/Csv/CsvReader.cs b/src/MiniExcel/Csv/CsvReader.cs index 1949dc9..f5d63d0 100644 --- a/src/MiniExcel/Csv/CsvReader.cs +++ b/src/MiniExcel/Csv/CsvReader.cs @@ -16,7 +16,7 @@ namespace MiniExcelLibs.Csv } public IEnumerable> Query(bool useHeaderRow, string sheetName, IConfiguration configuration) { - var cf = configuration==null ? CsvConfiguration.DefaultConfiguration : (CsvConfiguration)configuration; + var cf = configuration == null ? CsvConfiguration.DefaultConfiguration : (CsvConfiguration)configuration; using (var reader = cf.GetStreamReaderFunc(_stream)) { @@ -66,7 +66,7 @@ namespace MiniExcelLibs.Csv var cf = configuration == null ? CsvConfiguration.DefaultConfiguration : (CsvConfiguration)configuration; var type = typeof(T); - + Dictionary idxProps = new Dictionary(); using (var reader = cf.GetStreamReaderFunc(_stream)) { @@ -98,7 +98,18 @@ namespace MiniExcelLibs.Csv { var cell = new T(); foreach (var p in idxProps) - p.Value.SetValue(cell, read[p.Key]); + { + if (p.Value.PropertyType.IsEnum) + { + var newV = Enum.Parse(p.Value.PropertyType, read[p.Key], true); + p.Value.SetValue(cell, newV); + } + else + { + p.Value.SetValue(cell, read[p.Key]); + } + } + yield return cell; } } diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs index f28def1..8b3c33d 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs @@ -363,9 +363,12 @@ namespace MiniExcelLibs.OpenXml } else if (pInfo.Property.PropertyType == typeof(string)) { - //var vs = ; newV = XmlEncoder.DecodeString(itemValue?.ToString()); } + else if (pInfo.Property.PropertyType.IsEnum) + { + newV = Enum.Parse(pInfo.Property.PropertyType, itemValue?.ToString(), true); + } // solve : https://github.com/shps951023/MiniExcel/issues/138 else newV = Convert.ChangeType(itemValue, pInfo.ExcludeNullableType); diff --git a/tests/MiniExcelTests/MiniExcelIssueTests.cs b/tests/MiniExcelTests/MiniExcelIssueTests.cs index 5c325db..bfff9a9 100644 --- a/tests/MiniExcelTests/MiniExcelIssueTests.cs +++ b/tests/MiniExcelTests/MiniExcelIssueTests.cs @@ -23,6 +23,54 @@ namespace MiniExcelLibs.Tests this.output = output; } + /// + /// Support Enum Mapping + /// https://github.com/shps951023/MiniExcel/issues/89 + /// + [Fact] + public void Issue89() + { + //csv + { + var text = @"State +OnDuty +Fired +Leave"; + var stream = new MemoryStream(); + var writer = new StreamWriter(stream); + writer.Write(text); + writer.Flush(); + stream.Position = 0; + var rows = MiniExcel.Query(stream).ToList(); + Assert.Equal(Issue89VO.WorkState.OnDuty, rows[0].State); + Assert.Equal(Issue89VO.WorkState.Fired, rows[1].State); + Assert.Equal(Issue89VO.WorkState.Leave, rows[2].State); + } + + //xlsx + { + var path = @"../../../../../samples/xlsx/TestIssue89.xlsx"; + var rows = MiniExcel.Query(path).ToList(); + + Assert.Equal(Issue89VO.WorkState.OnDuty, rows[0].State); + Assert.Equal(Issue89VO.WorkState.Fired, rows[1].State); + Assert.Equal(Issue89VO.WorkState.Leave, rows[2].State); + } + + } + + public class Issue89VO + { + public WorkState State { get; set; } + + public enum WorkState + { + OnDuty, + Leave, + Fired + } + } + /// /// DataTable recommended to use Caption for column name first, then use columname /// https://github.com/shps951023/MiniExcel/issues/217