简体   繁体   中英

Write csv file using csvhelper library

I am using the following code and getting an exception ,

 Exception thrown: 'CsvHelper.WriterException' in CsvHelper.dll on this line executed:

csv.WriteRecord(item);

This is the more of the code:

using (var memoryStream = new MemoryStream())
{
    using (var writer = new StreamWriter(memoryStream))
    {
        using (var csv = new CsvHelper.CsvWriter(writer, System.Globalization.CultureInfo.CurrentCulture))
        {
            foreach (var item in csvData)
            {
                csv.WriteRecord(item); // Exception thrown here
            }
        }
    }

    var arr = memoryStream.ToArray();
    //js.SaveAs("people.csv", arr);  what type object is js? copied from the stackoverflow answer linked here
}

This is the csvData code:

IEnumerable<DateLowHigh> csvData = stocks.candles
    .Select(c => new DateLowHigh
    {
        Time = c.datetime,
        Close = c.close,
        Low = c.low,
        High = c.high
    })
    .ToList();

I do get the csvData.

This stackoverflow answer helped me get started.

I don't know why you need to use CsvHelper when you can easily do the same with the IO library.

sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.csv";
        static void Main(string[] args)
        {
            Stocks stocks = new Stocks();

            IEnumerable<DateLowHigh> csvData = stocks.candles.Select(c => new DateLowHigh
            {
                Time = c.datetime,
                Close = c.close,
                Low = c.low,
                High = c.high
            }).ToList();

            MemoryStream memoryStream = new MemoryStream();
            using (var writer = new StreamWriter(memoryStream))
            {
                string[] headers = {"Time", "Close", "Low", "High"};
                writer.WriteLine(string.Join(",", headers));
                foreach (var item in csvData)
                {
                    writer.WriteLine(string.Join(",", new string[] { item.Time.ToString(), item.Close, item.Low.ToString(), item.High.ToString() }));
                }  
            }
            memoryStream.Position = 0;
        }
    }
    public class Stocks
    {
        public List<Candle> candles { get; set; }
    }
    public class Candle
    {
        public DateTime datetime { get; set; }
        public string close { get; set; }        
        public int low { get; set; }
        public int high { get; set; }
    }
    public class DateLowHigh
    {
        public DateTime Time { get; set; }
        public string Close { get; set; }
        public int Low { get; set; }
        public int High { get; set; }
    }
}

To answer your question as to what type of object js is in js.SaveAs("people.csv", arr); it is likely Microsoft.JSInterop.IJSRuntime as seen in this Blazer HowTo . The StackOverflow question you referenced was likely using it to get access to JavaScript they could use in C# code. If you just want to save a CSV file using CsvHelper, it could be as simple as:

using (var writer = new StreamWriter("path\\to\\file.csv"))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{    
    csv.WriteRecords(csvData);
}

If you are still getting errors, you would need to post the full exception with StackTrace in order to help further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM