【问题标题】:need help passing list to this csv export class需要帮助将列表传递给这个 csv 导出类
【发布时间】:2011-12-11 19:14:49
【问题描述】:

我在网上找到了这个 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;

            }
        }

【问题讨论】:

  • 你有什么问题???
  • 那么你的问题是什么?如何使用类?或者如何设置你的班级来使用它?导出 CSV 真的很简单,我更喜欢编写自己的类来处理它
  • 我的列表不叫列表,我应该把它改成我的实际列表

标签: c# export export-to-csv


【解决方案1】:

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

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

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 引用切换到您所调用的类

【讨论】:

  • 如果我想从另一个类导出结果怎么办
  • @bugz:您使用的 CsvExport 类使用反射根据属性创建列值。因此,您传递的类只需要在导出中具有您想要的属性。在我看来,这无论如何都不是一个好方法。你的输入类有什么数据?您如何存储列标题和数据?
  • 该类返回我需要的确切数据 return result.Where(i => IForEmployee(facade, i.Employee)).ToList();它返回的是什么,它包括 Employee = employee, Manager = manager, Weeks = new List { new Week(info.Week).ToString("end") } 结果是我需要的列表 var result = new List();
  • @bugz: 你能把你的类的结构贴出来吗(在一个单独的代码块中——所以它可以滚动)
  • 啊我认为它的工作,问题是程序需要永远运行
【解决方案2】:

这里是使用带有示例类的 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&lt;YourClass&gt; 和现有对象的路径列表

【讨论】:

    猜你喜欢
    • 2018-11-15
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    相关资源
    最近更新 更多