Skip to content

Commit 00bb7c6

Browse files
committed
增加测试和文档
1 parent 4d010e8 commit 00bb7c6

File tree

4 files changed

+172
-2
lines changed

4 files changed

+172
-2
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("",Genders.Male);
240+
res.Add("",Genders.Female);
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.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()
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)