Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CsvHelper创建空白文件_C#_.net_Csvhelper - Fatal编程技术网

C# CsvHelper创建空白文件

C# CsvHelper创建空白文件,c#,.net,csvhelper,C#,.net,Csvhelper,我需要从集合中创建一个.csv或.txt文件。我正在使用库CsvHelper和我的开发人员使用以下扩展方法: public static class CsvWriter { public static void Write<TMapping, TModel>(this IEnumerable<TModel> items, string fullna

我需要从集合中创建一个.csv或.txt文件。我正在使用库CsvHelper和我的开发人员使用以下扩展方法:

public static class CsvWriter
    {
        public static void Write<TMapping, TModel>(this IEnumerable<TModel> items,
                                                   string fullname,
                                                   string separator,
                                                   Encoding encoding,
                                                   string culture)
            where TMapping : ClassMap, new()
        {
            var inputFileDescription = new CsvConfiguration(new CultureInfo(culture))
            {
                Delimiter = separator,
                Encoding = encoding,
                IgnoreBlankLines = true
            };


            using (var sr = new StreamWriter(fullname))
            using (var csv = new CsvHelper.CsvWriter(sr, inputFileDescription))
            {
                sr.AutoFlush = true;

                csv.Configuration.RegisterClassMap<TMapping>();
                csv.WriteRecords(items);

                csv.NextRecord();
            }
        }
    }

但是,无论我做什么或做什么更改,要生成的文件总是为空。我已经看到了构建.csv编写器的其他方法,但是,我所看到的一切都与我正在尝试的非常相似。有人能告诉我我做错了什么吗?

这里有太多不相关的代码,而代码又缺失了。将代码简化为a,您可能会发现问题。我可以使用您的代码并使用
FuturesFilesRepository.WriteCsvFile()
创建CSV文件,但没有任何问题。是否创建了CSV文件,但完全为空?
 public class FuturesFiles 
    {
        public DateTime TradeDate { get; set; }
        public string BmfAccount { get; set; }
        public string Side { get; set; }
        public decimal Quantity { get; set; }
        public decimal? Strike { get; set; }
        public string Type { get; set; }
        public string Payout { get; set; }
        public decimal? Price { get; set; }
        public string Ticker { get; set; }
        public string Broker { get; set; }
        public string Counterparty { get; set; }
        public string Desk { get; set; }
        public string Exchange { get; set; }
        public DateTime ArrivalDate { get; set; }
        public string Currency { get; set; }
        public int ContractId { get; set; }
}

public class FuturesFilesModel 
    {
        public DateTime TradeDate { get; set; }
        public string BmfAccount { get; set; }
        public string Side { get; set; }
        public decimal Quantity { get; set; }
        public decimal? Strike { get; set; }
        public string Type { get; set; }
        public string Payout { get; set; }
        public decimal? Price { get; set; }
        public string Ticker { get; set; }
        public string Broker { get; set; }
        public string Counterparty { get; set; }
        public string Desk { get; set; }
        public string Exchange { get; set; }
        public DateTime ArrivalDate { get; set; }
        public string Currency { get; set; }
        public int ContractId { get; set; }

    }

public class FuturesFilesMappings : ClassMap<FuturesFilesModel>
    {
        public FuturesFilesMappings()
        {
            Map(i => i.TradeDate).Name("Trade Date").Index(1);
            Map(i => i.BmfAccount).Name("A/C Ref").Index(2);
            Map(i => i.Side).Name("B/S").Index(3);
            Map(i => i.Quantity).Name("Lots").Index(4);
            Map(i => i.Type).Name("Type").Index(5);
            Map(i => i.Payout).Name("Price").Index(6);
            Map(i => i.Price).Name("Price").Index(7);
            Map(i => i.Ticker).Name("Ric").Index(8);
            Map(i => i.Broker).Name("Exec Firm Name").Index(9);
            Map(i => i.Counterparty).Name("Contraparte").Index(10);
            Map(i => i.Desk).Name("MESA").Index(11);
            Map(i => i.Exchange).Name("Exchange").Index(12);
            Map(i => i.ArrivalDate).Name("Delivery").Index(13);
            Map(i => i.Currency).Name("Curr").Index(14);
            Map(i => i.ContractId).Name("AGE").Index(15);

        }
    }
public class FuturesFilesRepository
    {      

        public void WriteCsvFile(string fileName, IEnumerable<FuturesFiles> futuresFiles)
        {
            var futuresFilesModel = futuresFiles.Select(oper =>
            {
                var model = new FuturesFilesModel();
                CreateModelFutures(oper, model);

                return model;
            }).ToList();


            futuresFilesModel.Write<FuturesFilesMappings, FuturesFilesModel>(fileName, ";", System.Text.Encoding.UTF8, "pt-BR");
        }

        void CreateModelFutures(FuturesFiles oper, FuturesFilesModel model)
        {
            model.TradeDate = oper.TradeDate;
            model.Type = oper.Type;
            model.Ticker = oper.Ticker;
            model.Strike = oper.Strike;
            model.Side = oper.Side;
            model.Quantity = oper.Quantity;
            model.Price = oper.Price;
            model.Payout = oper.Payout;
            model.Exchange = oper.Exchange;
            model.Desk = oper.Desk;
            model.Currency = oper.Currency;
            model.Counterparty = oper.Counterparty;
            model.ContractId = oper.ContractId;
            model.Broker = oper.Broker;
            model.BmfAccount = oper.BmfAccount;
            model.ArrivalDate = oper.ArrivalDate;
        }