需要帮助将列表传递给此 csv 导出类

发布于 2024-12-11 06:58:05 字数 2828 浏览 0 评论 0原文

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

            }
        }

I found this CSV export class online and would like to pass my own list from another class
my list is already prepared and contains 3 columns

 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;

            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日裸衫吸 2024-12-18 06:58:05

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

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

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

将类型为 MyData 的列表传递到您的 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 引用切换为您的类被称为

If you want help using the class then I would say you need to do something like this.

Create a class for your data..

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

the pass a List of type MyData into your CsvExport class like this...

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

you should now have an output file like...

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

To use a different input class just switch MyData references to what ever you class is called

回眸一遍 2024-12-18 06:58:05

这是使用 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 以及现有对象的路径列表

Here is sample of using Exporter with sample class

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");

So basically create CsvExport<YourClass> and path list of existing objects to it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文