csvhelper

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

此生再无相见时 提交于 2019-12-09 02:07:11
问题 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

Exporting data to CSV from Controller

梦想的初衷 提交于 2019-12-08 07:58:02
问题 I'm using the CsvHelper library in an ASP.NET MVC project to export data to CSV, and am finding that the exported data is either cut off, or in the case of smaller lists, no data is being written at all, and I'm receiving a blank CSV file. My base controller has a method like this (which is called by controllers inheriting from this class to export lists of entities): protected FileContentResult GetExportFileContentResult(IList data, string filename) { using (var memoryStream = new

Handling bad CSV records in CsvHelper

与世无争的帅哥 提交于 2019-12-07 01:34:14
问题 I would like to be able to iterate through all records in a CSV file and add all the good records to one collection and handle all the "bad" ones separately. I don't seem to be able to do this and I think I must be missing something. If I attempt to catch the BadDataException then subsequent reads will fail meaning I cannot carry on and read the rest of the file - while (true) { try { if (!reader.Read()) break; var record = reader.GetRecord<Record>(); goodList.Add(record); } catch

Manually Add Header in CsvHelper.CsvWriter

ぃ、小莉子 提交于 2019-12-06 21:20:01
问题 I'm using CsvHelper class to write rows in DataTable to a csv file. The code works but I can't get it to write the headers. How can I add the headers manually without creating a Class Map? http://joshclose.github.io/CsvHelper/ DataTable dt = GetDataTableFromDB(); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter( memoryStream ); CsvHelper.CsvWriter writer = new CsvHelper.CsvWriter( streamWriter ); foreach (

CsvHelper Parsing boolean from String

雨燕双飞 提交于 2019-12-06 18:53:36
I'm currently trying to parse a value from a csv file that is a boolean. We've noticed that the value will successfully parse Yes and Y (in any case) but will not parse No and N I'm mapping the value in the classmap like this: Map(m => m.Enabled).Name("Enabled").TypeConverterOption(false, string.Empty); Is there a reason why this would read Yes but not No and is there a way to add the ability to parse no ? In version 4.0.3, this works. void Main() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var csv

CSVHelper mandatory fields

荒凉一梦 提交于 2019-12-05 11:03:20
When parsing a csv file, how do i define that a specific field is mandatory. Essentially, I want to make sure that a given field is never empty, and if it is then I would like an exception thrown. Here is the mapping class: public sealed class DataMapper : CsvClassMap<DataType> { public DataMapper() { Map(m => m.Field1).Name("FirstField"); Map(m => m.Field2).Name("SecondField"); Map(m => m.Field3).Name("ThirdField"); // this field should be mandatory } } and the usage: List<DataType> data; using (var sr = new StreamReader(localFilePath)) { var reader = new CsvReader(sr); reader.Configuration

Enforce LF line endings with CsvHelper

删除回忆录丶 提交于 2019-12-05 07:10:49
If I have some LF converted (using N++) CSV files, everytime I write data to them using JoshClose's CsvHelper the line endings are back to CRLF. Since I'm having problems with CLRF ROWTERMINATORS in SQL Server, I whish to keep my line endings like the initital status of the file. Couldn't find it in the culture settings, I compile my own version of the library. How to proceed? From what I can tell, the line terminator isn't controlled by CvsHelper. I've gotten it to work by adjusting the File writer I pass to CsvWriter. TextWriter tw = File.CreateText(filepathname); tw.NewLine = "\n";

Handling bad CSV records in CsvHelper

眉间皱痕 提交于 2019-12-05 05:48:13
I would like to be able to iterate through all records in a CSV file and add all the good records to one collection and handle all the "bad" ones separately. I don't seem to be able to do this and I think I must be missing something. If I attempt to catch the BadDataException then subsequent reads will fail meaning I cannot carry on and read the rest of the file - while (true) { try { if (!reader.Read()) break; var record = reader.GetRecord<Record>(); goodList.Add(record); } catch (BadDataException ex) { // Exception is caught but I won't be able to read further rows in file // (all further

Manually Add Header in CsvHelper.CsvWriter

只愿长相守 提交于 2019-12-05 01:45:13
I'm using CsvHelper class to write rows in DataTable to a csv file. The code works but I can't get it to write the headers. How can I add the headers manually without creating a Class Map? http://joshclose.github.io/CsvHelper/ DataTable dt = GetDataTableFromDB(); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter( memoryStream ); CsvHelper.CsvWriter writer = new CsvHelper.CsvWriter( streamWriter ); foreach ( DataColumn column in properties.Columns ) writer.WriteHeader( column.ColumnName ); //<--- How do I write

Dynamic creation of columns using csvHelper

三世轮回 提交于 2019-12-05 01:26:32
I have a worker with various fields that are fetched from server. I am using CSVHelper package to convert this class to an excel sheet. Worker has Fields like : class Worker { string name; string phone; string age; Dictionary<string,object> customerField; } I can map the name, phone, number like class WorkerMap : CsvClassMap<Worker> { public WorkerMap() { Map(m => m.name); Map(m => m.phone); Map(m => m.age); } } And I generate the map by : csv.Configuration.RegisterClassMap<WorkerMap>(); Write the list of workers by : csv.WriteRecords(workerList); How can I map the customerField dictionary to