mirror of
https://gitee.com/dotnetchina/MiniExcel.git
synced 2024-11-29 18:38:08 +08:00
[New] Type Query support Enum mapping #89
This commit is contained in:
parent
750231e0fa
commit
cd1a71a6f6
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
BIN
samples/xlsx/TestIssue89.xlsx
Normal file
BIN
samples/xlsx/TestIssue89.xlsx
Normal file
Binary file not shown.
@ -16,7 +16,7 @@ namespace MiniExcelLibs.Csv
|
||||
}
|
||||
public IEnumerable<IDictionary<string, object>> 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<int, PropertyInfo> idxProps = new Dictionary<int, PropertyInfo>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -23,6 +23,54 @@ namespace MiniExcelLibs.Tests
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Support Enum Mapping
|
||||
/// https://github.com/shps951023/MiniExcel/issues/89
|
||||
/// </summary>
|
||||
[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<Issue89VO>(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<Issue89VO>(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
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DataTable recommended to use Caption for column name first, then use columname
|
||||
/// https://github.com/shps951023/MiniExcel/issues/217
|
||||
|
Loading…
Reference in New Issue
Block a user