[New] Support System.ComponentModel.DisplayName's [DisplayName] as title [#I4TXGT]

This commit is contained in:
Wei 2022-02-16 16:20:42 +08:00
parent 9b6c2c3d0d
commit a222e2a4f8
8 changed files with 98 additions and 2 deletions

View File

@ -829,6 +829,24 @@ public class Dto
#### 5. System.ComponentModel.DisplayNameAttribute = ExcelColumnName.excelColumnNameAttribute
Since 1.24.0, system supports System.ComponentModel.DisplayNameAttribute = ExcelColumnName.excelColumnNameAttribute
```C#
public class TestIssueI4TXGTDto
{
public int ID { get; set; }
public string Name { get; set; }
[DisplayName("Specification")]
public string Spc { get; set; }
[DisplayName("Unit Price")]
public decimal Up { get; set; }
}
```
### Excel Type Auto Check <a name="getstart5"></a> ### Excel Type Auto Check <a name="getstart5"></a>

View File

@ -837,6 +837,26 @@ public class Dto
#### 5. System.ComponentModel.DisplayNameAttribute = ExcelColumnName.excelColumnNameAttribute
从 1.24.0 开始支持 System.ComponentModel.DisplayNameAttribute 等同于 ExcelColumnName.excelColumnNameAttribute 效果
```C#
public class TestIssueI4TXGTDto
{
public int ID { get; set; }
public string Name { get; set; }
[DisplayName("Specification")]
public string Spc { get; set; }
[DisplayName("Unit Price")]
public decimal Up { get; set; }
}
```
### Excel 类别自动判断 <a name="getstart5"></a> ### Excel 类别自动判断 <a name="getstart5"></a>

View File

@ -843,6 +843,22 @@ public class Dto
#### 5. System.ComponentModel.DisplayNameAttribute = ExcelColumnName.excelColumnNameAttribute
從 1.24.0 開始支持 System.ComponentModel.DisplayNameAttribute 等同於 ExcelColumnName.excelColumnNameAttribute 效果
```C#
public class TestIssueI4TXGTDto
{
public int ID { get; set; }
public string Name { get; set; }
[DisplayName("Specification")]
public string Spc { get; set; }
[DisplayName("Unit Price")]
public decimal Up { get; set; }
}
```

View File

@ -16,6 +16,8 @@
--- ---
### 1.24.0
- [New] Support System.ComponentModel.DisplayName's `[DisplayName]` as title [#I4TXGT](https://gitee.com/dotnetchina/MiniExcel/issues/I4TXGT)
### 1.23.0 ### 1.23.0
- [New] Support `GetReader` method #328 #290 (Thanks [杨福来 Yang](https://github.com/yfl8910) ) - [New] Support `GetReader` method #328 #290 (Thanks [杨福来 Yang](https://github.com/yfl8910) )

View File

@ -24,7 +24,13 @@
--- ---
### 1.24.0
- [New] 支持 System.ComponentModel.DisplayName 的 `[DisplayName]` 作为excel标题 [#I4TXGT](https://gitee.com/dotnetchina/MiniExcel/issues/I4TXGT)
### 1.23.0 ### 1.23.0
- [New] 新增 `GetReader` 方法 #328 #290 (感谢 [杨福来 Yang](https://github.com/yfl8910) ) - [New] 新增 `GetReader` 方法 #328 #290 (感谢 [杨福来 Yang](https://github.com/yfl8910) )
### 1.22.0 ### 1.22.0

View File

@ -18,6 +18,8 @@
--- ---
### 1.24.0
- [New] 支持 System.ComponentModel.DisplayName 的 `[DisplayName]` 作為excel標題 [#I4TXGT](https://gitee.com/dotnetchina/MiniExcel/issues/I4TXGT)
### 1.23.0 ### 1.23.0
- [New] 新增 `GetReader` 方法 #328 #290 (感謝 [楊福來 Yang](https://github.com/yfl8910) ) - [New] 新增 `GetReader` 方法 #328 #290 (感謝 [楊福來 Yang](https://github.com/yfl8910) )

View File

@ -152,7 +152,7 @@
.Select(p => .Select(p =>
{ {
var gt = Nullable.GetUnderlyingType(p.PropertyType); var gt = Nullable.GetUnderlyingType(p.PropertyType);
var excelColumnName = p.GetAttribute<ExcelColumnNameAttribute>(); var excelColumnName = p.GetAttribute<ExcelColumnNameAttribute>() ;
var excludeNullableType = gt ?? p.PropertyType; var excludeNullableType = gt ?? p.PropertyType;
var excelFormat = p.GetAttribute<ExcelFormatAttribute>()?.Format; var excelFormat = p.GetAttribute<ExcelFormatAttribute>()?.Format;
MethodInfo formatToStringMethod = null; MethodInfo formatToStringMethod = null;
@ -168,7 +168,7 @@
ExcludeNullableType = excludeNullableType, ExcludeNullableType = excludeNullableType,
Nullable = gt != null, Nullable = gt != null,
ExcelColumnAliases = excelColumnName?.Aliases, ExcelColumnAliases = excelColumnName?.Aliases,
ExcelColumnName = excelColumnName?.ExcelColumnName ?? p.Name, ExcelColumnName = excelColumnName?.ExcelColumnName ?? p.GetAttribute<System.ComponentModel.DisplayNameAttribute>()?.DisplayName ?? p.Name ,
ExcelColumnIndex = p.GetAttribute<ExcelColumnIndexAttribute>()?.ExcelColumnIndex, ExcelColumnIndex = p.GetAttribute<ExcelColumnIndexAttribute>()?.ExcelColumnIndex,
ExcelColumnWidth = p.GetAttribute<ExcelColumnWidthAttribute>()?.ExcelColumnWidth, ExcelColumnWidth = p.GetAttribute<ExcelColumnWidthAttribute>()?.ExcelColumnWidth,
ExcelFormat = excelFormat, ExcelFormat = excelFormat,

View File

@ -31,6 +31,38 @@ namespace MiniExcelLibs.Tests
this.output = output; this.output = output;
} }
[Fact]
public void TestIssueI4TXGT()
{
var path = PathHelper.GetTempFilePath();
var value = new[] { new TestIssueI4TXGTDto { ID = 1, Name = "Apple", Spc = "X", Up = 6999 } };
MiniExcel.SaveAs(path, value);
{
var rows = MiniExcel.Query(path).ToList();
Assert.Equal("ID", rows[0].A);
Assert.Equal("Name", rows[0].B);
Assert.Equal("Specification", rows[0].C);
Assert.Equal("Unit Price", rows[0].D);
}
{
var rows = MiniExcel.Query<TestIssueI4TXGTDto>(path).ToList();
Assert.Equal(1, rows[0].ID);
Assert.Equal("Apple", rows[0].Name);
Assert.Equal("X", rows[0].Spc);
Assert.Equal(6999, rows[0].Up);
}
}
public class TestIssueI4TXGTDto
{
public int ID { get; set; }
public string Name { get; set; }
[DisplayName("Specification")]
public string Spc { get; set; }
[DisplayName("Unit Price")]
public decimal Up { get; set; }
}
[Fact] [Fact]
public void TestIssue328() public void TestIssue328()
{ {