donnytian / Npoi.Mapper

Use this tool to import or export data with Excel file. The tool is a convention based mapper between strong typed object and Excel data via NPOI.
MIT License
601 stars 115 forks source link

能否根据Mapper.Attributes集合的顺序来导出Excel? #24

Closed VAllens closed 6 years ago

VAllens commented 6 years ago

你好,很高兴你使用CI持续发布NuGet包。 现在有个问题: image 你现在的Excel导出是根据类属性顺序导出的,而且是先输出派生类属性列,后输出基类属性列。 能否根据Mapper.Attributes集合的顺序来导出Excel?

donnytian commented 6 years ago

If you want to control the column order during export, try to specify column index by:

mapper.Map<SampleClass>(0, o =>o.ISO);
mapper.Map<SampleClass>(1, o =>o.EN);
//...
VAllens commented 6 years ago

有更好的方式吗? 例如:

mapper.Map<SampleClass>(columnIndex, columnName, Expression<Func<T, object>> propertySelector);
mapper.Map<SampleClass>(columnIndex, columnName, Expression<Func<T, object>> propertySelector, Func<IColumnInfo, object, bool> tryTake = null, Func<IColumnInfo, object, bool> tryPut = null);

我不希望专门为Import写一个Map, Export再写另一个Map

donnytian commented 6 years ago

如果import和export的时候column顺序都一致, 只写一次map就可以, 如果不同建议写两次.

VAllens commented 6 years ago

importexportcolumn顺序是一致的,但columnNamepropertyName不一致。 我在MapExtensions没有找到一个可用的方法。

mapper.Map<SampleClass>(columnIndex, "Categories",
    t => t.Category,
    (column, target) =>
    {
        string value = column.CurrentValue.NullableToString();
        //枚举值为Key,枚举描述为Value
        string key = categories.Where(t => t.Value == value).Select(t => t.Key).FirstOrDefault();
        if (key == null)
        {
            return false;
        }

        ((SampleClass) target).Category = (CareCategory) Enum.Parse(typeof(CareCategory), key);

        return true;
    },
    (column, source) =>
    {
        column.CurrentValue = ((SampleClass) source).Category.GetDescription();

        return true;
    });
donnytian commented 6 years ago

thanks for your suggestion, will enable user specify both column index and name for export.

VAllens commented 6 years ago

Okey, thank you so much. :)

VAllens commented 6 years ago

very good