csvhelper

Dynamically mapping nested objects with CSVHelper

感情迁移 提交于 2019-12-12 19:06:53
问题 I'm using CSVHelper (thanks Josh Close) to read a CSV file which works great. I'm now trying to use it to map that file to some internal classes; however, the CSV's I'm mapping vary by customer but all need to map to my internal classes. I need to allow the customer to define how the CSV maps to my POCO objects. I'm storing the customer defined mappings as Dictionary<string,int> -- ["Firstname", 20],["Lastname",21],["Address.Line1",30], ["Address.Line2",31], etc. I have a dynamic map class

CsvHelper: Replace missing Csv field with another expression?

主宰稳场 提交于 2019-12-12 18:04:27
问题 When a field is missing in the CSV file, an exception is thrown. I'd rather map another value (such as empty string) when a field is missing. Map(dest => dest.PatientID).Name("Patient ID"); CsvHelper.CsvMissingFieldException: 'Fields 'Patient ID' do not exist in the CSV file.' If the configuration setting IgnoreReadingExceptions is used, no records are read into the results. var csv = new CsvReader(sr); csv.Configuration.RegisterClassMap<TMap>(); csv.Configuration.IgnoreReadingExceptions =

How to read a header from a specific line with CsvHelper?

寵の児 提交于 2019-12-12 11:27:53
问题 I'm trying to read a CSV file where header is at row 3: some crap line some empty line COL1,COL2,COl3,... val1,val2,val3 val1,val2,val3 How do I tell CSVHelper the header is not at the first row? I tried to skip 2 lines with Read() but the succeeding call to ReadHeader() throws an exception that the header has already been read. using (var csv = new CsvReader(new StreamReader(stream), csvConfiguration)) { csv.Read(); csv.Read(); csv.ReadHeader(); ..... If I set csvConfiguration

Dynamic creation of columns using csvHelper

走远了吗. 提交于 2019-12-12 09:36:15
问题 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>();

How to ignore empty rows in CSV when reading

有些话、适合烂在心里 提交于 2019-12-11 19:37:26
问题 Trying to read a CSV file that has empty rows (usually at the end) using CsvHelper.GetRecords<T>() . Without the empty rows this works a treat. However if the CSV file has an empty row (defined as , , , , , ) then it throws a TypeConverterException Text: '' MemberType: IntelligentEditing.PerfectIt.Core.DataTypes.Styles.StyleRuleType TypeConverter: 'CsvHelper.TypeConversion.EnumConverter' I have gone through the documentation (https://joshclose.github.io/CsvHelper/api/CsvHelper.Configuration

Azure Web Jobs cannot read csv properly

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 15:39:01
问题 I am using the latest version of CsvHelper library and yet Azure WebJob making troubles reading my csv file. It says not valid DateTime format. Though I am 101% sure that my csv has the right DateTime format. I believe that the CsvHelper causes the issue but I don't really know.. If more information is needed please let me know. 回答1: For such issue, it would be diffcult to us to answer if we don't have a sample that can reproduce the issue. So here I will just share you an effective way to

Writing enumerable to csv file

陌路散爱 提交于 2019-12-11 03:45:52
问题 I'm sure its very straightforward but I am struggling to figure out how to write an array to file using CSVHelper. I have a class for example public class Test { public Test() { data = new float[]{0,1,2,3,4}; } public float[] data{get;set;} } i would like the data to be written with each array value in a separate cell. I have a custom converter below which is instead providing one cell with all the values in it. What am I doing wrong? public class DataArrayConverter<T> : ITypeConverter {

How to handle empty column in parsing with CsvHelper?

我与影子孤独终老i 提交于 2019-12-10 16:58:00
问题 I have a CSV file, I'm trying to parse it, But getting the below error on int datatype column.In the file, I'm passing the blank or empty value for this column. [AutoMap(typeof(Test))] [FileEntity("Test")] public class TestFileEntity : BaseFileEntity { [CsvFileColumn(1)] public int TestId { get; set; } } public IEnumerable<BaseFileEntity> Parse(Stream masterData, Type entityType, out IEnumerable<MasterDataFileParserError> errors) { var list = new List<BaseFileEntity>(); var errorList = new

CsvHelper : How to detect the Delimiter from the given csv file

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 13:58:44
问题 I am using CsvHelper to read/writer the data into Csv file. Now I want to parse the delimiter of the csv file. How can I get this please? My code: var parser = new CsvParser(txtReader); delimiter = parser.Configuration.Delimiter; I always got delimiter is "," but actually in the csv file the delimiter is "\t". 回答1: CSV is Comma Separated Values. I don't think you can reliably detect if there is a different character used a separator. If there is a header row, then you might be able to count

What is the best way to get the list of column names using CsvHelper?

烂漫一生 提交于 2019-12-09 07:53:36
问题 I am trying to use CsvHelper for a project. I went through the documentation but I couldn't find a way to read all the column names with a single method. How can I get a list of all column header names easily with CsvHelper? I am doing it like this currently but I assume there is a better way. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using CsvHelper; using System.IO; namespace Kg { class Program { static void Main