3

我在网上找到了这个 CSV 导出类,并想从另一个类传递我自己的列表,我的列表已经准备好并包含 3 列

 using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Data.SqlTypes;
        using System.IO;
        using System.Reflection;


          public class CsvExport<T> where T : class
        {
            public List<T> Objects;

            public CsvExport(List<T> objects)
            {
                Objects = objects;
            }

            public string Export()
            {
                return Export(true);
            }

            public string Export(bool includeHeaderLine)
            {

                StringBuilder sb = new StringBuilder();
                //Get properties using reflection.
                IList<PropertyInfo> propertyInfos = typeof(T).GetProperties();

                if (includeHeaderLine)
                {
                    //add header line.
                    foreach (PropertyInfo propertyInfo in propertyInfos)
                    {
                        sb.Append(propertyInfo.Name).Append(",");
                    }
                    sb.Remove(sb.Length - 1, 1).AppendLine();
                }

                //add value for each property.
                foreach (T obj in Objects)
                {
                    foreach (PropertyInfo propertyInfo in propertyInfos)
                    {
                        sb.Append(MakeValueCsvFriendly(propertyInfo.GetValue(obj, null))).Append(",");
                    }
                    sb.Remove(sb.Length - 1, 1).AppendLine();
                }

                return sb.ToString();
            }

            //export to a file.
            public void ExportToFile(string path)
            {
                File.WriteAllText(path, Export());
            }

            //export as binary data.
            public byte[] ExportToBytes()
            {
                return Encoding.UTF8.GetBytes(Export());
            }

            //get the csv value for field.
            private string MakeValueCsvFriendly(object value)
            {
                if (value == null) return "";
                if (value is Nullable && ((INullable)value).IsNull) return "";

                if (value is DateTime)
                {
                    if (((DateTime)value).TimeOfDay.TotalSeconds == 0)
                        return ((DateTime)value).ToString("yyyy-MM-dd");
                    return ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss");
                }
                string output = value.ToString();

                if (output.Contains(",") || output.Contains("\""))
                    output = '"' + output.Replace("\"", "\"\"") + '"';

                return output;

            }
        }
4

2 回答 2

3

这是使用带有示例类的 Exporter 的示例

class Sample
{
   public string Field1 {get;set;}
   public int Field2 {get;set;}
}

List<Sample> source = new List<Sample>()

// fill list 

CsvExport<Sample> export = new CsvExport<Sample>(source);
export.ExportToFile("yourFile.csv");

所以基本上创建CsvExport<YourClass>现有对象的路径列表

于 2011-10-20T13:27:05.553 回答
3

如果您在使用课程时需要帮助,那么我会说您需要做这样的事情。

为您的数据创建一个类..

public class MyData
{
   public string Column1Data {get;set;}
   public string Column2Data {get;set;}
   public string Column3Data {get;set;}
}

像这样将 MyData 类型的 List 传递到您的 CsvExport 类中...

List<MyData> list = new List<MyData>();
//populate the list here
CsvExport<MyData> export = new CsvExport<MyData>();
export.ExportToFile(@"C:\MyExportFile.txt");

你现在应该有一个输出文件,比如......

Column1Data,Column2Data,Column3Data 
r1c1,r1c2,r1c3
r2c1,r2c2,r2c3
r3c1,r1c2,r3c3

要使用不同的输入类,只需将 MyData 引用切换到您所调用的类

于 2011-10-20T13:29:15.847 回答