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.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

能否将"列标题映射的默认命名约定"作用于Excel导出

VAllens opened this issue · comments

commented

能否在没有自定义映射规则的情况下,将"列标题映射的默认命名约定"作用于Excel导出。

举例:
我没有调用Map<TModel>(parameters...),但我希望导出后,
DisplayNameProperty属性在Excel表格中显示的列名是Display Name,而不是DisplayNameProperty

ColumnNameAttributeProperty属性在Excel表格中的列名是正确显示的——By Name
换句话说,在导出功能中,ColumnAttribute起作用了,但DisplayAttribute没起作用。

实体类SampleClass

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Npoi.Mapper.Attributes;

namespace Sample
{
    public class SampleClass
    {
        public string StringProperty { get; set; }

        [Column("By Name")]
        public string ColumnNameAttributeProperty { get; set; }

        [Column(11)]
        public string ColumnIndexAttributeProperty { get; set; }

        [Ignore]
        public string IgnoredAttributeProperty { get; set; }

        [Display(Name = "Display Name")]
        public string DisplayNameProperty { get; set; }

        [Column(CustomFormat = "0%")]
        public double CustomFormatProperty { get; set; }
    }
}

建议还是用ColumnAttribute或者用Map方法, DisplayAttribute的import支持做的比较早, 但后来感觉其意义比较容易和其他业务逻辑功能混淆, 所以后来的功能尽量不再使用它了, 导致目前export的时候没有考虑它.

commented

Hi, 我觉得,在读取属性的特性时,可以优先判断是否ColumnAttribute,如果没有ColumnAttribute再判断DisplayAttribute,其次是当用户调用Map<T>()覆盖你事先解析和缓存。 ImportExport都可以