我遇到的问题尤其是CSVHelper库。
我的csv文件如下所示:
Number,Date,Account,Amount,Subcategory,Memo
,09/05/2017,XXX XXXXXX,-29.00,FT , [Sample string]
,09/05/2017,XXX XXXXXX,-20.00,FT ,[Sample string]
,08/05/2017,XXX XXXXXX,-6.30,PAYMENT,[Sample string]我对CSVHelper所做的是:
List<Transaction> result = new List<Transaction>();
using (TextReader fileReader = File.OpenText("data.csv"))
{
var csv = new CsvReader(fileReader);
result = csv.GetRecords<Transaction>().ToList();
}问题是,当它试图在最后一行执行GetRecord时,我得到了以下异常:
Unhandled Exception: System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
at System.DateTime.Parse(String s, IFormatProvider provider, DateTimeStyles styles)
at CsvHelper.TypeConversion.DateTimeConverter.ConvertFromString(TypeConverterOptions options, String text)
at lambda_method(Closure )
at CsvHelper.CsvReader.CreateRecord[T]()
at CsvHelper.CsvReader.<GetRecords>d__65`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)如您所见,数据存在一些问题--第一列要么为空,要么字符串为空。即使如此,异常消息仍然指向第二列日期的问题。
任何帮助都将不胜感激。
发布于 2017-05-09 22:41:05
我的猜测是,您的默认区域性指定了与CSV文件中不同的日期格式。尝试将区域性设置为与数据匹配的区域性。
var csv = new CsvReader(fileReader);
csv.Configuration.CultureInfo = CultureInfo.GetCultureInfo("en-GB");
result = csv.GetRecords<Transaction>().ToList();https://stackoverflow.com/questions/43880658
复制相似问题