使用 csvhelper 库编写 csv 文件
Write csv file using csvhelper library
我正在使用以下代码并遇到异常,
Exception thrown: 'CsvHelper.WriterException' in CsvHelper.dll on this line executed:
csv.WriteRecord(item);
这是更多的代码:
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 Whosebug answer linked here
}
这是 csvData 代码:
IEnumerable<DateLowHigh> csvData = stocks.candles
.Select(c => new DateLowHigh
{
Time = c.datetime,
Close = c.close,
Low = c.low,
High = c.high
})
.ToList();
我得到了 csvData。
回答帮助我开始了。
我不知道为什么您需要使用 CsvHelper,因为您可以轻松地使用 IO 库执行相同的操作。
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; }
}
}
要回答您关于 js.SaveAs("people.csv", arr);
中的对象 js
是什么类型的问题,很可能 Microsoft.JSInterop.IJSRuntime
如此 Blazer HowTo 中所见。您引用的 Whosebug 问题很可能是使用它来访问他们可以在 C# 代码中使用的 JavaScript。如果您只想使用 CsvHelper 保存 CSV 文件,它可以像这样简单:
using (var writer = new StreamWriter("path\to\file.csv"))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(csvData);
}
如果您仍然遇到错误,您需要 post 使用 StackTrace 的完整异常,以便进一步提供帮助。
我正在使用以下代码并遇到异常,
Exception thrown: 'CsvHelper.WriterException' in CsvHelper.dll on this line executed:
csv.WriteRecord(item);
这是更多的代码:
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 Whosebug answer linked here
}
这是 csvData 代码:
IEnumerable<DateLowHigh> csvData = stocks.candles
.Select(c => new DateLowHigh
{
Time = c.datetime,
Close = c.close,
Low = c.low,
High = c.high
})
.ToList();
我得到了 csvData。
我不知道为什么您需要使用 CsvHelper,因为您可以轻松地使用 IO 库执行相同的操作。
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; }
}
}
要回答您关于 js.SaveAs("people.csv", arr);
中的对象 js
是什么类型的问题,很可能 Microsoft.JSInterop.IJSRuntime
如此 Blazer HowTo 中所见。您引用的 Whosebug 问题很可能是使用它来访问他们可以在 C# 代码中使用的 JavaScript。如果您只想使用 CsvHelper 保存 CSV 文件,它可以像这样简单:
using (var writer = new StreamWriter("path\to\file.csv"))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(csvData);
}
如果您仍然遇到错误,您需要 post 使用 StackTrace 的完整异常,以便进一步提供帮助。