ExcelUnity/Core/ColumnProperty.cs

(完整代码) 1KB

using System.Reflection;

namespace ExcelUnity.Core;

public class ColumnProperty
{
    public ColumnProperty(PropertyInfo property)
    {
        Name = property.GetCustomAttribute(typeof(ColumnNameAttribute)) is ColumnNameAttribute nameAttr ? nameAttr.Name : property.Name;
        
        PropertyInfo = property;
    }

    /// <summary>
    /// 列名
    /// </summary>
    public string Name { get; set; }
    
    /// <summary>
    /// 属性所在的列索引
    /// </summary>
    public int ColumnIndex { get; set; }
    
    /// <summary>
    /// 列对应的对象属性
    /// </summary>
    public PropertyInfo PropertyInfo { get; set; }
    
    /// <summary>
    /// 获取该属性在cell中展示的文本
    /// </summary>
    /// <param name="target"></param>
    /// <returns></returns>
    public string GetCellValue(object target)
    {
        var objectValue = PropertyInfo.GetValue(target);

        string cellValue = string.Empty;
        if (objectValue != null)
        {
            if (PropertyInfo.PropertyType == typeof(DateTime) || PropertyInfo.PropertyType == typeof(DateTime?))
            {
                cellValue = ((DateTime)objectValue).ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                cellValue = objectValue.ToString();
            }
        }
        return cellValue;
    }
}

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。