csvhelper

CsvHelper Parsing boolean from String

让人想犯罪 __ 提交于 2019-12-23 02:18:13
问题 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 ? 回答1: In version 4.0.3, this works. void Main() { using (var stream = new MemoryStream()

CsvHelper Parsing boolean from String

被刻印的时光 ゝ 提交于 2019-12-23 02:18:06
问题 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 ? 回答1: In version 4.0.3, this works. void Main() { using (var stream = new MemoryStream()

CSVHelper mandatory fields

老子叫甜甜 提交于 2019-12-22 06:59:10
问题 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;

Enforce LF line endings with CsvHelper

依然范特西╮ 提交于 2019-12-22 05:16:08
问题 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? 回答1: From what I can tell, the line terminator isn't controlled by CvsHelper. I've gotten it to work by adjusting the

Write to a File using CsvHelper in C#

廉价感情. 提交于 2019-12-18 12:52:17
问题 I tried to write to CSV file using CsvHelper in C#. This is the link to the library http://joshclose.github.io/CsvHelper/ I used the code in web site. Here is my code: var csv = new CsvWriter(writer); csv.Configuration.Encoding = Encoding.UTF8; foreach (var value in valuess) { csv.WriteRecord(value); } It writes only a part of data to csv file. Last rows were missing. Could you please help with this. 回答1: You need to flush the stream. The Using statement will flush when out of scope. using

Using CSVHelper to output stream to browser

不问归期 提交于 2019-12-17 23:09:27
问题 I'm trying to use CSVHelper to generate a CSV file and send it back to a browser, so the user can select a save location and filename and save the data. The website is MVC based. Here' the jQuery button code I'm using to make the call (data is some serialised Json representation of a DTO list): $.ajax({ type: "POST", url: unity.baseUrl + "common/ExportPayments", data: data }); Here's the controller code: [HttpPost] public FileStreamResult ExportPayments() { MemoryStream ms = new MemoryStream(

CsvHelper not writing anything to memory stream

会有一股神秘感。 提交于 2019-12-17 18:27:34
问题 I have the following method: public byte[] WriteCsvWithHeaderToMemory<T>(IEnumerable<T> records) where T : class { using (var memoryStream = new MemoryStream()) using (var streamWriter = new StreamWriter(memoryStream)) using (var csvWriter = new CsvWriter(streamWriter)) { csvWriter.WriteRecords<T>(records); return memoryStream.ToArray(); } } Which is being called with a list of objects - eventually from a database, but since something is not working I'm just populating a static collection.

Writing a Header using CsvHelper? C#

十年热恋 提交于 2019-12-17 16:58:11
问题 I'm using CsvHelper. To write to a .csv file, I need a header based off of a class. I write the header manually and it works, but I need to be done automatically when it is read. All the documentation I can find says to use this expression, writer.WriteHeader<CSVDataFormat>(); but that isn't working because it needs more work done to it. Here is the class that the header should be based off: public class CSVDataFormat { public string FirstName { get; set; } public string LastName { get; set;

CSVHelper 7.1.1 - Header matching [id] names at index 0 was not found

眉间皱痕 提交于 2019-12-13 05:11:12
问题 I have an exception thrown when loading this CSV file :/ which is quite simple : public class BreederTemp { public BreederTemp () { } public int Id { get; set; } public string Affix { get; set; } public bool Affix_Prefix { get; set; } } public sealed class BreederClassMap : ClassMap<BreederTemp> { public BreederClassMap() { AutoMap(); Map(m => m.Id); Map(m => m.Affix); Map(m => m.Affix_Prefix); } } class Program { static void Main(string[] args) { // [...] System.IO.TextReader textReader =

CsvHelper Map Colletion

雨燕双飞 提交于 2019-12-12 22:33:19
问题 I'm mapping csv file with School records using CsvHelper The end result should be list of Schools. Similar issue can be found here public class School { public IList<Student> Students{ get; set; } } public class Student { public StudentRef Reference{ get; set; } } public class StudentRef { public string RefNumber{ get; set; } } One of the columns found in the CSV file is SRef which should be linked to StudentRef.RefNumber public sealed class StudentRefMap : CsvClassMap<StudentRef> { public