如何在C#中将类转换为Csv文件 [英] How can I convert class to Csv file in C#

查看:106
本文介绍了如何在C#中将类转换为Csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为DataSet的课程

  public   class  DataSet 
{
private string _title;
private 列表< ImageAssociation> _associated_images = new 列表< ImageAssociation>();


public string time_start = 已开始;

public string duration {获得; set ; }

public string title
{
< span class =code-keyword> get
{ return _title; }
set
{
int length = .Length > 150 150 value .Length;
_title = value .Substring( 0 ,length);
}
}


public string description { get ; set ; }


public string integration_id {获得; set ; }


public string auction_code {获得; set ; }


public string viewing_information {获得; set ; }


public AuctionPublicationStatus publication_status { get ; set ; }

public string currency_code {获得; set ; }


public string location_name {获得; set ; }


public string location_description {获得; set ; }


public 列表< ImageAssociation> associated_images { get { return _associated_images; }
}
}





ImageAssociation类为

  public   partial   class  ImageAssociation 
{

public string image_id {获得; set ; }

public string caption {获得; set ; }
}





现在我想将此类文件中的值转换为csv文件我的csv文件代码写入如下



  protected   void  Button1_Click( object  sender,EventArgs e)
{
if (!Directory.Exists( E:\\baiju\\Test))
{
System.IO.Directory.CreateDirectory( E:\\\ \\baiju\\Test);
}
使用 var sw = new StreamWriter( E:\\baiju\\Test \\ Test.csv))
{
var writer = new CsvWriter(sw);
List< AuctionUpsert> list = new List< AuctionUpsert>();
IEnumerable records = list;
writer.WriteRecords(records);
writer.NextRecord();
}
}



我这样做时只有DataSet类中的字段名称显示在CSV文件中。我想要CSV文件中每个字段的值。



注意: - 而且associated_images和start_time(默认值为已启动)字段不可用还有。

提前谢谢。

解决方案

Baiju christadima写道:



列表< AuctionUpsert> list = new List< AuctionUpsert>(); //<  - 空! 
IEnumerable records = list; //< - 太空了!
writer.WriteRecords(records);



我这样做时只有DataSet类中的字段名称显示在CSV文件中。我想要CSV文件中每个字段的值。



您已将空列表传递给 WriteRecords 方法。没有值可写。



如果你想将一些数据写入CSV文件,那么你需要将这些数据传递给 CsvWriter


Hi, I have a class named DataSet

public class DataSet
    {
         private string _title;
        private List<ImageAssociation> _associated_images = new List<ImageAssociation>();
 
        
        public string time_start = "Started";
        
        public string duration { get; set; }
        
        public string title
        { 
            get { return _title; }
            set
            { 
                int length = value.Length > 150 ? 150 : value.Length;
                _title = value.Substring(0, length);
            }
        }
 
        
        public string description { get; set; }
 
        
        public string integration_id { get; set; }
 
        
        public string auction_code { get; set; }
 
        
        public string viewing_information { get; set; }
 
        
        public AuctionPublicationStatus publication_status { get; set; }
 
        public string currency_code { get; set; }
 
        
        public string location_name { get; set; }
 
       
        public string location_description { get; set; }
 
        
        public List<ImageAssociation> associated_images { get { return _associated_images; } }
    }
    }



ImageAssociation class as

public partial class ImageAssociation
    {
        
        public string image_id { get; set; }
        
        public string caption { get; set; }
    }



Now I want to convert values in this class file to a csv file My code for the csv file write is as follows

 protected void Button1_Click(object sender, EventArgs e)
        {
if (!Directory.Exists("E:\\baiju\\Test"))
                {
                    System.IO.Directory.CreateDirectory("E:\\baiju\\Test");
                }
                    using (var sw = new StreamWriter("E:\\baiju\\Test\\Test.csv"))
                    {
                         var writer = new CsvWriter(sw);
                        List<AuctionUpsert> list = new List<AuctionUpsert>();
                        IEnumerable records = list;
                        writer.WriteRecords(records);
                        writer.NextRecord();
                     }
}


While I am doing this Only the field names in the DataSet class are shown in the CSV file. I want the value of each fields in the CSV file.

NB:- And the associated_images and start_time(That has value "started" by default) fields are not available there also.
Thanks in advance.

解决方案

Baiju christadima wrote:


List<AuctionUpsert> list = new List<AuctionUpsert>(); // <- EMPTY!
IEnumerable records = list; // <- ALSO EMPTY!
writer.WriteRecords(records);


While I am doing this Only the field names in the DataSet class are shown in the CSV file. I want the value of each fields in the CSV file.


You've passed an empty list to the WriteRecords method. There are no values to write.

If you want to write some data to the CSV file, then you'll need to pass that data to the CsvWriter.


这篇关于如何在C#中将类转换为Csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆