Skip to content

Commit a34c5ea

Browse files
authored
Merge pull request #544 from netty2019/master
增加ValueMappingsBase值映射特性基类,方便从外部(如:Abp框架)继承实现枚举、bool类型的多语言显示
2 parents 79e8e09 + d506daf commit a34c5ea

File tree

11 files changed

+211
-9
lines changed

11 files changed

+211
-9
lines changed

README.zh-CN.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,31 @@
228228
public Genders Gender { get; set; }
229229
```
230230

231+
- **也可以继承“ValueMappingsBaseAttribute”特性基类实现值映射关系,目前仅可用于枚举和Bool类型,支持导入导出。**
232+
```csharp
233+
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
234+
public class GenderLocalAttribute : ValueMappingsBaseAttribute
235+
{
236+
public override Dictionary<string, object> GetMappings(PropertyInfo propertyInfo)
237+
{
238+
var res= new Dictionary<string, object>();
239+
res.Add("",0);
240+
res.Add("",1);
241+
return res;
242+
}
243+
}
244+
245+
246+
/// <summary>
247+
/// 性别
248+
/// </summary>
249+
[ImporterHeader(Name = "性别")]
250+
[Required(ErrorMessage = "性别不能为空")]
251+
[GenderLocal]
252+
public Genders Gender { get; set; }
253+
```
254+
255+
231256
- **支持枚举和Bool类型的导入数据验证项的生成,以及相关数据转换**
232257
- **枚举默认情况下会自动获取枚举的描述、显示名、名称和值生成数据项**
233258

src/Magicodes.ExporterAndImporter.Core/Extension/Extension.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,18 @@ public static int GetLargestContinuous(this List<int> numList)
322322
public static void ValueMapping(this PropertyInfo propertyInfo, ref Dictionary<string, dynamic> directory)
323323
{
324324
#region 处理值映射
325+
//ValueMappingsBaseAttribute
326+
var valueMappings = propertyInfo.GetAttributes<ValueMappingsBaseAttribute>(true).FirstOrDefault()?.GetMappings(propertyInfo);
327+
if(valueMappings != null )
328+
{
329+
foreach (var valueMapping in valueMappings)
330+
{
331+
if (!directory.ContainsKey(valueMapping.Key)) directory.Add(valueMapping.Key, valueMapping.Value);
332+
}
333+
if (valueMappings.Count > 0) return;
334+
}
325335

336+
//ValueMappingAttribute
326337
var mappings = propertyInfo.GetAttributes<ValueMappingAttribute>().ToList();
327338
var objects = directory;
328339
foreach (var mappingAttribute in mappings.Where(mappingAttribute =>
@@ -363,4 +374,4 @@ public static void ValueMapping(this PropertyInfo propertyInfo, ref Dictionary<s
363374
#endregion 处理值映射
364375
}
365376
}
366-
}
377+
}

src/Magicodes.ExporterAndImporter.Core/IImporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ public interface IImporter
7474
/// <param name="stream"></param>
7575
/// <param name="labelingFileStream"></param>
7676
/// <returns></returns>
77-
Task<ImportResult<T>> Import<T>(Stream stream,Stream labelingFileStream) where T : class, new();
77+
Task<ImportResult<T>> Import<T>(Stream stream, Stream labelingFileStream, Func<ImportResult<T>, ImportResult<T>> importResultCallback = null) where T : class, new();
7878
}
7979
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Text;
5+
6+
namespace Magicodes.IE.Core
7+
{
8+
/// <summary>
9+
/// 值映射
10+
/// </summary>
11+
public abstract class ValueMappingsBaseAttribute : Attribute
12+
{
13+
/// <summary>
14+
/// 根据字段信息获取映射
15+
/// </summary>
16+
/// <param name="propertyInfo"></param>
17+
/// <returns></returns>
18+
public abstract Dictionary<string, object> GetMappings(PropertyInfo propertyInfo);
19+
}
20+
}

src/Magicodes.ExporterAndImporter.Csv/CsvImporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public class CsvImporter : ICsvImporter
8989
/// <param name="stream"></param>
9090
/// <param name="labelingFileStream"></param>
9191
/// <returns></returns>
92-
public Task<ImportResult<T>> Import<T>(Stream stream, Stream labelingFileStream) where T : class, new()
92+
public Task<ImportResult<T>> Import<T>(Stream stream, Stream labelingFileStream, Func<ImportResult<T>, ImportResult<T>> importResultCallback = null) where T : class, new()
9393
{
9494
using (var importer = new ImportHelper<T>(stream))
9595
{

src/Magicodes.ExporterAndImporter.Excel/ExcelImporter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ public class ExcelImporter : IExcelImporter
169169
/// <param name="stream"></param>
170170
/// <param name="labelingFileStream"></param>
171171
/// <returns></returns>
172-
public Task<ImportResult<T>> Import<T>(Stream stream, Stream labelingFileStream) where T : class, new()
172+
public Task<ImportResult<T>> Import<T>(Stream stream, Stream labelingFileStream, Func<ImportResult<T>, ImportResult<T>> importResultCallback = null) where T : class, new()
173173
{
174174
using (var importer = new ImportHelper<T>(stream, labelingFileStream))
175175
{
176-
return importer.Import();
176+
return importer.Import(importResultCallback: importResultCallback);
177177
}
178178
}
179179

src/Magicodes.ExporterAndImporter.Tests/ExcelExporterWithXSSFWorkbook_Tests.cs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,8 +958,55 @@ public async Task ValueMapping_Test()
958958
}
959959
}
960960

961-
962-
961+
[Fact(DisplayName = "ValueMappingsBase测试#544")]
962+
public async Task ValueMappingsBase_Test()
963+
{
964+
IExporter exporter = new ExcelExporter();
965+
var filePath = GetTestFilePath($"{nameof(ValueMappingsBase_Test)}.xlsx");
966+
DeleteFile(filePath);
967+
var list = new List<Issue544>()
968+
{
969+
new Issue544()
970+
{
971+
Gender ="男",
972+
IsAlumni = true,
973+
Name ="张三",
974+
IsAlumni2 = true,
975+
},
976+
new Issue544()
977+
{
978+
Gender ="男",
979+
IsAlumni = false,
980+
Name ="张三",
981+
IsAlumni2 = true,
982+
},
983+
new Issue544()
984+
{
985+
Gender ="男",
986+
IsAlumni = null,
987+
Name ="张三",
988+
IsAlumni2 = false,
989+
},
990+
};
991+
var result = await exporter.ExportWithXSSFWorkbook(filePath, list);
992+
result.ShouldNotBeNull();
993+
File.Exists(filePath).ShouldBeTrue();
994+
using (var pck = new ExcelPackage(new FileInfo(filePath)))
995+
{
996+
pck.Workbook.Worksheets.Count.ShouldBe(1);
997+
var sheet = pck.Workbook.Worksheets.First();
998+
sheet.Cells["D2"].Text.ShouldBe("是");
999+
sheet.Cells["D3"].Text.ShouldBe("是");
1000+
sheet.Cells["D4"].Text.ShouldBe("否");
1001+
1002+
sheet.Cells["C2"].Text.ShouldBe("是");
1003+
sheet.Cells["C3"].Text.ShouldBe("否");
1004+
sheet.Cells["C4"].Text.ShouldBe("");
1005+
}
1006+
}
1007+
1008+
1009+
9631010
[Fact(DisplayName = "导出日期格式化#331")]
9641011
public async Task DateTimeExport_Test()
9651012
{

src/Magicodes.ExporterAndImporter.Tests/ExcelExporter_Tests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,55 @@ public async Task ValueMapping_Test()
964964
}
965965

966966

967+
[Fact(DisplayName = "ValueMappingsBase测试#544")]
968+
public async Task ValueMappingsBase_Test()
969+
{
970+
IExporter exporter = new ExcelExporter();
971+
var filePath = GetTestFilePath($"{nameof(ValueMappingsBase_Test)}.xlsx");
972+
DeleteFile(filePath);
973+
var list = new List<Issue544>()
974+
{
975+
new Issue544()
976+
{
977+
Gender ="男",
978+
IsAlumni = true,
979+
Name ="张三",
980+
IsAlumni2 = true,
981+
},
982+
new Issue544()
983+
{
984+
Gender ="男",
985+
IsAlumni = false,
986+
Name ="张三",
987+
IsAlumni2 = true,
988+
},
989+
new Issue544()
990+
{
991+
Gender ="男",
992+
IsAlumni = null,
993+
Name ="张三",
994+
IsAlumni2 = false,
995+
},
996+
};
997+
var result = await exporter.Export(filePath, list);
998+
result.ShouldNotBeNull();
999+
File.Exists(filePath).ShouldBeTrue();
1000+
using (var pck = new ExcelPackage(new FileInfo(filePath)))
1001+
{
1002+
pck.Workbook.Worksheets.Count.ShouldBe(1);
1003+
var sheet = pck.Workbook.Worksheets.First();
1004+
sheet.Cells["D2"].Text.ShouldBe("是");
1005+
sheet.Cells["D3"].Text.ShouldBe("是");
1006+
sheet.Cells["D4"].Text.ShouldBe("否");
1007+
1008+
sheet.Cells["C2"].Text.ShouldBe("是");
1009+
sheet.Cells["C3"].Text.ShouldBe("否");
1010+
sheet.Cells["C4"].Text.ShouldBe("");
1011+
}
1012+
}
1013+
1014+
1015+
9671016

9681017
[Fact(DisplayName = "导出日期格式化#331")]
9691018
public async Task DateTimeExport_Test()

src/Magicodes.ExporterAndImporter.Tests/Models/Export/ExporterHeaderFilterTestData.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public class ExporterHeaderFilterTestData1
5959
[ExporterHeader(DisplayName = "加粗文本", IsBold = true)]
6060
public string Text { get; set; }
6161

62-
[ExporterHeader(DisplayName = "普通文本")] public string Text2 { get; set; }
62+
[ExporterHeader(DisplayName = "普通文本")]
63+
public string Text2 { get; set; }
6364

6465
[ExporterHeader(DisplayName = "忽略", IsIgnore = true)]
6566
public string Text3 { get; set; }
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Magicodes.ExporterAndImporter.Core;
2+
using Magicodes.ExporterAndImporter.Excel;
3+
using Magicodes.IE.Core;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Reflection;
7+
8+
namespace Magicodes.ExporterAndImporter.Tests.Models.Export
9+
{
10+
[ExcelExporter(Name = "导出结果", TableStyle = OfficeOpenXml.Table.TableStyles.None)]
11+
public class Issue544
12+
{
13+
/// <summary>
14+
/// 名称
15+
/// </summary>
16+
[ExporterHeader(DisplayName = "姓名")]
17+
public string Name { get; set; }
18+
19+
/// <summary>
20+
/// 性别
21+
/// </summary>
22+
[ExporterHeader(DisplayName = "性别")]
23+
public string Gender { get; set; }
24+
25+
/// <summary>
26+
/// 是否校友
27+
/// </summary>
28+
[ExporterHeader(DisplayName = "是否校友")]
29+
[BoolLocal337]
30+
public bool? IsAlumni { get; set; }
31+
32+
[ExporterHeader(DisplayName = "是否校友2")]
33+
[BoolLocal337]
34+
public bool IsAlumni2 { get; set; }
35+
}
36+
37+
38+
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
39+
public class BoolLocal337Attribute : ValueMappingsBaseAttribute
40+
{
41+
public override Dictionary<string, object> GetMappings(PropertyInfo propertyInfo)
42+
{
43+
var res= new Dictionary<string, object>();
44+
res.Add("是",true);
45+
res.Add("否",false);
46+
return res;
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)