zoukankan      html  css  js  c++  java
  • List集合 导出 Excel

    public string CreateAdvExcel(IList<DocAdvInfo> lt)
            {
                StringBuilder builder = new StringBuilder();
                Random rn = new Random();
                string name = rn.Next(9999) + ".xls";
                string path = Server.MapPath("\Document\" + name);
                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];
                    string headname = pi.Name;//单元格头部
                    builder.Append(headname);
                    builder.Append("	");
                }
                builder.Append("
    ");
                foreach (DocAdvInfo t in lt)
                {
                    if (lt == null)
                    {
                        continue;
                    }
                    for (i = 0, j = myPropertyInfo.Length; i < j; i++)
                    {
                        System.Reflection.PropertyInfo pi = myPropertyInfo[i];
                        string str = string.Format("{0}", pi.GetValue(t, null)).Replace("
    ", "");
                        if (str == "")
                        {
                            builder.Append("	");
                        }
                        else
                        {
                            builder.Append(str + "	");//横向跳到另一个单元格
                        }
                    }
                    builder.Append("
    ");//换行
                }
                StreamWriter sw = new StreamWriter(path, false, System.Text.Encoding.GetEncoding("GB2312"));
                sw.Write(builder.ToString());//输出
                sw.Flush();
                sw.Close();
    
  • 相关阅读:
    济南学习 Day5 T3 晚
    Codevs 1043 ==洛谷 P1004 方格取数
    济南学习 Day 5 T2 晚
    济南学习 Day 5 T1 晚
    济南学习 Day 5 T3 am
    济南学习 Day 5 T2 am
    LeetCode Weekly Contest 8
    poj-1410 Intersection
    leetcode-Warm Up Contest-Aug.21
    poj-1384 Piggy-Bank
  • 原文地址:https://www.cnblogs.com/huhaihua/p/3745371.html
Copyright © 2011-2022 走看看