csvhelper

Using CsvHelper can I translate white space to a nullable?

廉价感情. 提交于 2019-12-04 03:09:12
问题 I have some really lame Csv files I need to parse. I am using CsvHelper and it is working awesome. Except I have some lines that have whitespace where normaly I have a double. File: Text,SomeDouble,MoreText "Good",1.23,"Good" "Bad", ,"Bad" if I try and map this into public class Test { [CsvField(Name = "Text")] public string Text { get; set; } [CsvField(Name = "SomeDouble")] public double? SomeDouble{ get; set; } [CsvField(Name = "MoreText")] public string MoreText{ get; set; } } then I get

using csvhelper (nuGET) with C# MVC to import CSV files

帅比萌擦擦* 提交于 2019-12-03 20:07:08
问题 http://csvhelper.com available via NuGet is used to read and write CSV files. CsvHelper allows you to read your CSV file directly into your custom class. As following was shown in a previous question var streamReader = // Create a reader to your CSV file. var csvReader = new CsvReader( streamReader ); List<MyCustomType> myData = csvReader.GetRecords<MyCustomType>(); CsvReader will automatically figure out how to match the property names based on the header row (this is configurable). It uses

How to add CsvHelper records to DataTable to use for SqlBulkCopy to the database

99封情书 提交于 2019-12-03 12:04:50
I am trying to read a CSV file with CsvHelper, load each record into a DataTable, and then use SqlBulkCopy to insert the data into a database table. With the current code, I get an exception when adding a row to the DataTable. The exception is: "Unable to cast object of type 'MvcStockAnalysis.Models.StockPrice' to type 'System.IConvertible'.Couldn't store in Date Column. Expected type is DateTime." The example CSV file is from yahoo finance. For example: http://ichart.yahoo.com/table.csv?s=MMM&a=0&b=1&c=2010&d=0&e=17&f=2014&g=d&ignore=.csv The CSV file contains the following header: Date Open

Using CsvHelper can I translate white space to a nullable?

纵饮孤独 提交于 2019-12-01 17:03:35
I have some really lame Csv files I need to parse. I am using CsvHelper and it is working awesome. Except I have some lines that have whitespace where normaly I have a double. File: Text,SomeDouble,MoreText "Good",1.23,"Good" "Bad", ,"Bad" if I try and map this into public class Test { [CsvField(Name = "Text")] public string Text { get; set; } [CsvField(Name = "SomeDouble")] public double? SomeDouble{ get; set; } [CsvField(Name = "MoreText")] public string MoreText{ get; set; } } then I get an error like this: CsvHelper.CsvReaderException: An error occurred trying to read a record of type Row:

In CsvHelper how to catch a conversion error and know what field and what row it happened in?

跟風遠走 提交于 2019-12-01 02:06:14
I am using the class CsvReader successfully and am happy with it, however, the file that I consume is being produced by a group which changes column formats without letting me know. So, one moment everything is working, then the next morning things break and the try catch block around csv.GetRecord<MyType>() catches the error and logs the error, however I can't gather any valuable info from the Exception instance. It just says: "The conversion cannot be performed." and the InnerException has nothing. Not very useful. I don't even know which one of my 150 columns are causing the problem. Can

How to use EnumConverter with CsvHelper

一世执手 提交于 2019-11-30 21:53:40
I'm using CsvHelper to serialize a class to csv file - until here everything works well. Now I'm trying to find a way to convert the class's enum properties to their int value in the csv, so I could use the CSV for bulk insert later. I found out the EnumConverter class in CsvHelper but I can't figure out how to properly use it, as all my tries are failing. Here is my mapping class code public sealed class MyMapping : CsvClassMap<TradingCalendarException> { public MyMapping() { EnumConverter enumConverter = new EnumConverter(typeof(CalendarExceptionEntityType)); Map(m => m.ExceptionEntityType)

Failed to write large amount of data to stream

对着背影说爱祢 提交于 2019-11-30 21:10:51
问题 When I'm trying to write very large amount of data (list with 300 000 rows and more) to memory stream using CsvHelper, it throws the exception "System.IO.IOException: Stream was too long." . Data class is rather big and has ~30 properties, consequently each record in the file would have ~30 columns. This is the actual writing code where exception throws (by the way this code is based on that answer of CsvHelper lib's author): using (var memoryStream = new MemoryStream()) { using (var

Using CSVHelper on file upload

北城以北 提交于 2019-11-30 18:57:24
I am trying to use the CSVhelper plugin to read an uploaded CSV file. Here is my modelBinder class: public class SurveyEmailListModelsModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var csv = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); var file = ((csv.RawValue as HttpPostedFileBase[]) ?? Enumerable.Empty<HttpPostedFileBase>()).FirstOrDefault(); if (file == null || file.ContentLength < 1) { bindingContext.ModelState.AddModelError( "", "Please select a valid CSV file" ); return

How to use EnumConverter with CsvHelper

99封情书 提交于 2019-11-30 18:06:21
问题 I'm using CsvHelper to serialize a class to csv file - until here everything works well. Now I'm trying to find a way to convert the class's enum properties to their int value in the csv, so I could use the CSV for bulk insert later. I found out the EnumConverter class in CsvHelper but I can't figure out how to properly use it, as all my tries are failing. Here is my mapping class code public sealed class MyMapping : CsvClassMap<TradingCalendarException> { public MyMapping() { EnumConverter

CsvHelper set default custom TypeConverter

孤街浪徒 提交于 2019-11-30 09:41:50
问题 With CsvHelper, when I want a custom parser (for example, I want a MyBooleanConverter with the input string is "f" will be false, "t" will be "true"). But with every class I have to write mapper: public sealed class MyClassMap : CsvClassMap<MyClass> { public MyClassMap() { Map( m => m.Id ).Index( 0 ).TypeConverter<MyBooleanConverter>(); } } Or [CsvHelper.TypeConversion.TypeConverter( typeof( MyBooleanConverter) )] public Boolean MyObjectProperty { get; set; } How can I set MyBooleanConverter