简单的List集合导出为Excel

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace UpLoadExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //标题
            var titles = new Dictionary<string, string>();
            titles.Add("name", "姓名");
            titles.Add("age", "年龄");
            titles.Add("sex", "性别");

            //数据源
            var list = new List<Student>();
            list.Add(new  Student  {
            name="张三",age=18,sex="男"
            });
            list.Add(new Student
             {
                 name = "李四",
                 age = 18,
                 sex = "女"
             });
            //表格名称
            var fileName = "学生信息";
            //保存路径
            var fileurl = "D:/";


            //生成csv文件并保存
            bool bol = SaveCsv(titles, list, fileName, fileurl);
        }

        /// <summary>
        ///导出csv文件
        /// </summary>
        /// <param name="titles">列名</param>
        /// <param name="list">导出数据</param>
        /// <param name="fileName">文件名字</param>
        /// <param name="fileurl">文件地址</param>
        /// <returns></returns>
        public static bool SaveCsv<T>(Dictionary<string, string> titles, List<T> list, string fileName, string fileurl)
        {
            bool flag = true;
            try
            {
                StringWriter swCSV = new StringWriter();
                if (list.Count > 0)
                {
                    string titleStr = string.Empty;
                    List<System.Reflection.PropertyInfo> myPro = new List<System.Reflection.PropertyInfo>();
                    Type myType = list[0].GetType();
                    foreach (string ckey in titles.Keys)
                    {
                        titleStr += titles[ckey] + ",";
                        //c++;
                        System.Reflection.PropertyInfo p = myType.GetProperty(ckey);
                        if (p != null)
                        {
                            myPro.Add(p);
                        }
                    }
                    //列名
                    swCSV.WriteLine(titleStr.Substring(0, titleStr.Length - 1));

                    //遍历datatable导出数据
                    foreach (T obj in list)
                    {
                        StringBuilder sbText = new StringBuilder();
                        foreach (System.Reflection.PropertyInfo p in myPro)
                        {
                            if (p.GetValue(obj, null) != null)
                            {
                                sbText.Append(p.GetValue(obj, null).ToString().Replace(",", ",") + ",");
                            }
                            else
                            {
                                sbText.Append(",");
                            }

                        }
                        sbText.Remove(sbText.Length - 1, 1);
                        swCSV.WriteLine(sbText.ToString());
                    }
                }
                //生成文件并保存文件
                string url = fileurl + "fileupload\\" + fileName + ".csv";//要存储的路径 
                if (!Directory.Exists(fileurl + "fileupload\\"))
                {
                    Directory.CreateDirectory(fileurl + "fileupload\\");
                }
                StreamWriter sw = new StreamWriter(url, false, System.Text.Encoding.UTF8);//把文件作为文件流保存在固定的路径
                sw.Write(swCSV.ToString());// 文件流写出
                sw.Flush();//判断是否有文件,如果有则删除
                sw.Dispose();//释放流的资源
                swCSV.Close();
                swCSV.Dispose();
            }
            catch (Exception ex)
            {
                flag = false;
            }
            return flag;
        }
    }
    /// <summary>
    /// 学生类
    /// </summary>
    public class Student
    { 
     public string name { get; set; }
     public int age { get; set; }
     public string sex { get; set; }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一叶知秋~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值