zoukankan      html  css  js  c++  java
  • List<T>集合导出csv方法参考,通过增加自定义的属性控制输出的字段。

    public string CreateAdvExcel(List<GridScoreManager> lt)
            {
                StringBuilder builder = new StringBuilder();
                if (lt == null || (0 == lt.Count))
                {
                    return "";
                }
                System.Reflection.PropertyInfo[] myPropertyInfo = lt.First().GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
                int i = 0, j;
                for (i = 0, j = myPropertyInfo.Length; i < j; i++)
                {
                    System.Reflection.PropertyInfo pi = myPropertyInfo[i];
                    var ex = pi.GetCustomAttribute<ExAttribute>();
                    if (ex != null)
                    {
                        string headname = ex.HeaderName;//单元格头部
                        builder.Append(string.Format(""{0}"", headname.Replace(""", """")));
                        builder.Append(",");
                    }               
                }
                builder.AppendLine();
                foreach (GridScoreManager t in lt)
                {
                    if (lt == null)
                    {
                        continue;
                    }
                    for (i = 0, j = myPropertyInfo.Length; i < j; i++)
                    {
                        System.Reflection.PropertyInfo pi = myPropertyInfo[i];
                        var ex = pi.GetCustomAttribute<ExAttribute>();
                        if (ex != null)
                        {
                            string str = string.Format(""{0}"", pi.GetValue(t, null).ToString().Replace(""", """"));
                            builder.Append(str).Append(",");
                        }                        
                        
                    }
                    builder.AppendLine();
                }
    
    
                return builder.ToString();
    
            }
        }
        public class GridScoreManager
        {
    
            [ExAttribute(HeaderName = "aaaa  aa Manager Nameaaaa  , "aa Manager Nameaaaa  aa Manager Nameaaaa  aa Manager Nameaaaa  aa Manager Name")]
            public string Name { get; set; }
            [ExAttribute(HeaderName ="Identity")]
            public int Id { get; set; }
    
            public int Age { get; set; }
    
        }
    
        public class ExAttribute : Attribute
        {
            public string HeaderName { get; set; }
        }
    

      

  • 相关阅读:
    hdu 1312 Red and Black
    hdu 1573 X问题
    广工校赛决赛之简单的数论题
    最大的LeftMax与rightMax之差绝对值
    POJ 2385 Apple Catching
    hdu 1171 Big Event in HDU
    ACM比赛经验
    BestCoder Valentine's Day Round
    使用ffmpeg进行视频封面截取
    使用ODP.NET连接Oracle数据库
  • 原文地址:https://www.cnblogs.com/sgciviolence/p/5631425.html
Copyright © 2011-2022 走看看