zs
2025-06-04 5a149d626ae8bc3fa4bddbb53f8caf40f51f6da6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Threading.Tasks;
 
namespace CmsQueryExtensions
{
    /// <summary>
    /// 导出帮助类.
    /// </summary>
    public static class ExportHelper
    {
       
 
        // 批量转换方法
        public static List<Dictionary<string, object>> ConvertListToExportData<T>(IEnumerable<T> list)
        {
            return list.Select(ConvertToExportData).ToList();
        }
 
        // 通用转换方法(兼容 Func<T, Dictionary<string, object>>)
        public static Dictionary<string, object> ConvertToExportData<T>(T entity)
        {
            var dict = new Dictionary<string, object>();
            if (entity == null) return dict;
 
            foreach (var prop in typeof(T).GetProperties())
            {
                var value = prop.GetValue(entity);
                dict[prop.Name] = FormatValue(value);
            }
            return dict;
        }
 
        private static object FormatValue(object value)
        {
            if (value == null) return string.Empty;
 
            // 处理枚举
            if (value is Enum enumValue)
                return GetEnumDescriptionUtil.GetEnumDescription(enumValue);
 
            // 处理布尔
            if (value is bool boolValue)
                return boolValue ? "是" : "否";
 
            return value;
        }
 
    }
}