Skip to content

Commit

Permalink
Merge pull request #228 from 263613093/master
Browse files Browse the repository at this point in the history
修复Excel导出列头索引与内容排序不一致问题及单测
  • Loading branch information
xin-lai authored Jan 8, 2021
2 parents 86bbd0d + a99d831 commit e75f8f6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
24 changes: 19 additions & 5 deletions src/Magicodes.ExporterAndImporter.Excel/Utility/ExportHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ public virtual void AddExporterHeaderInfoList(List<ExporterHeaderInfo> exporterH
{
_exporterHeaderList = exporterHeaderInfos;
}
/// <summary>
/// 获得经过排序的属性
/// </summary>
protected virtual List<PropertyInfo> SortedProperties
{
get
{
var type = _type ?? typeof(T);
var objProperties = type.GetProperties().OrderBy(p => p.GetAttribute<ExporterHeaderAttribute>()?.ColumnIndex ?? 10000).ToList();
return objProperties;
}
}

/// <summary>
/// 获取头部定义
Expand Down Expand Up @@ -278,12 +290,13 @@ protected virtual void GetExporterHeaderInfoList(DataTable dt = null, ICollectio
}
else if (!IsDynamicDatableExport)
{
var type = _type ?? typeof(T);
//var type = _type ?? typeof(T);
//#179 GetProperties方法不按特定顺序(如字母顺序或声明顺序)返回属性,因此此处支持按ColumnIndex排序返回
var objProperties = type.GetProperties().OrderBy(p => p.GetAttribute<ExporterHeaderAttribute>()?.ColumnIndex ?? 10000).ToArray();
if (objProperties.Length == 0)
//var objProperties = type.GetProperties().OrderBy(p => p.GetAttribute<ExporterHeaderAttribute>()?.ColumnIndex ?? 10000).ToArray();
var objProperties = SortedProperties;
if (objProperties.Count == 0)
return;
for (var i = 0; i < objProperties.Length; i++)
for (var i = 0; i < objProperties.Count; i++)
{

var item = new ExporterHeaderInfo
Expand Down Expand Up @@ -553,8 +566,9 @@ protected void AddDataItems(dynamic dataItems, ExcelRangeBase excelRange = null)
/// <param name="dataItems"></param>
protected virtual DataTable ParseData(ICollection<T> dataItems)
{
var x = ExporterHeaderList.Count;
var type = typeof(T);
var properties = type.GetProperties();
var properties = SortedProperties;
DataTable dt = new DataTable();
foreach (var propertyInfo in properties)
{
Expand Down
10 changes: 7 additions & 3 deletions src/Magicodes.ExporterAndImporter.Tests/ExcelExporter_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static DataTable EntityToDataTable<T>(DataTable dt, IEnumerable<T> entit
}


[Fact(DisplayName = "DTO特性导出(测试格式化)")]
[Fact(DisplayName = "DTO特性导出(测试格式化以及列头索引)")]
public async Task AttrsExport_Test()
{
IExporter exporter = new ExcelExporter();
Expand All @@ -84,7 +84,7 @@ public async Task AttrsExport_Test()
pck.Workbook.Worksheets.Count.ShouldBe(1);
var sheet = pck.Workbook.Worksheets.First();
sheet.Cells[sheet.Dimension.Address].Rows.ShouldBe(101);
sheet.Cells["A2"].Text.ShouldBe(data[0].Text);
sheet.Cells["A2"].Text.ShouldBe(data[0].Text2);

//[ExporterHeader(DisplayName = "日期1", Format = "yyyy-MM-dd")]
sheet.Cells["E2"].Text.Equals(DateTime.Parse(sheet.Cells["E2"].Text).ToString("yyyy-MM-dd"));
Expand All @@ -102,7 +102,11 @@ public async Task AttrsExport_Test()

var tb = sheet.Tables.First();
tb.Columns.Count.ShouldBe(9);
tb.Columns.First().Name.ShouldBe("加粗文本");
tb.Columns.First().Name.ShouldBe("普通文本");

sheet.Tables.First();
tb.Columns.Count.ShouldBe(9);
tb.Columns[2].Name.ShouldBe("加粗文本");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,27 @@ public ExporterHeaderInfo Filter(ExporterHeaderInfo exporterHeaderInfo)
[ExcelExporter(Name = "测试", TableStyle =TableStyles.Dark10, AutoFitAllColumn = true, AutoFitMaxRows = 5000)]
public class ExportTestDataWithAttrs
{
[ExporterHeader(DisplayName = "加粗文本", IsBold = true)]
/// <summary>
/// Text:索引10
/// </summary>
[ExporterHeader(DisplayName = "加粗文本", IsBold = true,ColumnIndex =10)]
public string Text { get; set; }
[ExporterHeader(DisplayName = "普通文本")] public string Text2 { get; set; }
[ExporterHeader(DisplayName = "忽略", IsIgnore = true)]
/// <summary>
/// Text2:索引1
/// </summary>
[ExporterHeader(DisplayName = "普通文本",ColumnIndex =1)]
public string Text2 { get; set; }
/// <summary>
/// Text3:索引2
/// </summary>
[ExporterHeader(DisplayName = "忽略", IsIgnore = true,ColumnIndex =2)]
public string Text3 { get; set; }
[ExporterHeader(DisplayName = "数值", Format = "#,##0")]
/// <summary>
/// Number:索引3
/// </summary>
[ExporterHeader(DisplayName = "数值", Format = "#,##0",ColumnIndex =3)]
public int Number { get; set; }

[ExporterHeader(DisplayName = "名称", IsAutoFit = true)]
public string Name { get; set; }

Expand Down

0 comments on commit e75f8f6

Please sign in to comment.