csvhelper

In C#, how can I create a TextReader object from a string (without writing to disk)

你离开我真会死。 提交于 2019-11-30 05:33:10
I'm using A Fast CSV Reader to parse some pasted text into a webpage. The Fast CSV reader requires a TextReader object, and all I have is a string. What's the best way to convert a string into a TextReader object on the fly? Thanks! Update- Sample code- In the original sample, a new StreamReader is looking for a file called "data.csv". I'm hoping to supply it via TextBox_StartData.Text. Using this code below doesn't compile. TextReader sr = new StringReader(TextBox_StartData.Text); using (CsvReader csv = new CsvReader(new StreamReader(sr), true)) { DetailsView1.DataSource = csv; DetailsView1

CsvHelper - read in multiple columns to a single list

徘徊边缘 提交于 2019-11-30 05:06:06
I'm using CSVHelper to read in lots of data I'm wondering if it's possible to read the last n columns in and transpose them to a list "Name","LastName","Attribute1","Attribute2","Attribute3" And mould the data into something like this public class Person { public string FirstName { get; set; } public string LastName { get; set; } public IList<string> Attributes { get; set; } } I'm looking to do this in one step, I'm sure I could have an intermediate step where I put into an object with matching attribute properties but it would be nice to do it on a one-er This does the trick as a mapper.

CsvHelper ConvertUsing not changing output

落爺英雄遲暮 提交于 2019-11-29 18:17:23
问题 I'm trying to use the ConvertUsing method of the CsvHelper library (v 2.4.0). I've read the documentation about ConvertUsing but can't get it to work. I'm using a simple class: public class Test { public long Id { get; set; } public string Title { get; set; } } With this ClassMap : public class TestClassMap : CsvClassMap<Test> { public override void CreateMap() { Map(m => m.Id).Name("id").ConvertUsing(row => 11111); Map(m => m.Title).Name("title").ConvertUsing(row => row.GetField("title") + "

Writing a Header using CsvHelper? C#

我只是一个虾纸丫 提交于 2019-11-29 15:15:07
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; } public float Wage { get; set; } } Here is the code for the reading and writing: private void ReadCSV

Custom conversions when writing CSV files using CsvHelper

浪子不回头ぞ 提交于 2019-11-29 11:03:48
I've been doing some CSV reading and writing lately, and ran across CsvHelper which is fantastic so far. I've ran into one small problem; I use a custom converter when reading the files, but when I write them back out, that format is lost. My CSV format looks like this: 67,1234-1,20150115,750,20150115,1340,549,549,406,0,FRG The fields 20150115,750 map to a single DateTime field called Start (So, 01/15/2015 7:50 AM). My class map looks like this: public sealed class DutyMap : CsvClassMap<Duty> { static readonly CultureInfo enUS = new CultureInfo("en-US"); public DutyMap() { Map(m => m.PersonId)

In C#, how can I create a TextReader object from a string (without writing to disk)

孤者浪人 提交于 2019-11-29 04:18:18
问题 I'm using A Fast CSV Reader to parse some pasted text into a webpage. The Fast CSV reader requires a TextReader object, and all I have is a string. What's the best way to convert a string into a TextReader object on the fly? Thanks! Update- Sample code- In the original sample, a new StreamReader is looking for a file called "data.csv". I'm hoping to supply it via TextBox_StartData.Text. Using this code below doesn't compile. TextReader sr = new StringReader(TextBox_StartData.Text); using

CsvHelper - read in multiple columns to a single list

纵饮孤独 提交于 2019-11-29 02:53:53
问题 I'm using CSVHelper to read in lots of data I'm wondering if it's possible to read the last n columns in and transpose them to a list "Name","LastName","Attribute1","Attribute2","Attribute3" And mould the data into something like this public class Person { public string FirstName { get; set; } public string LastName { get; set; } public IList<string> Attributes { get; set; } } I'm looking to do this in one step, I'm sure I could have an intermediate step where I put into an object with

Using CSVHelper to output stream to browser

懵懂的女人 提交于 2019-11-28 20:15:40
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(); StreamWriter sw = new StreamWriter(ms); CsvWriter writer = new CsvWriter(sw); List<Payment_dto> pd =

CsvHelper not writing anything to memory stream

佐手、 提交于 2019-11-28 08:03:24
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. The objects being passed are as follows: using CsvHelper.Configuration; namespace Application.Models

Custom conversions when writing CSV files using CsvHelper

断了今生、忘了曾经 提交于 2019-11-28 04:35:19
问题 I've been doing some CSV reading and writing lately, and ran across CsvHelper which is fantastic so far. I've ran into one small problem; I use a custom converter when reading the files, but when I write them back out, that format is lost. My CSV format looks like this: 67,1234-1,20150115,750,20150115,1340,549,549,406,0,FRG The fields 20150115,750 map to a single DateTime field called Start (So, 01/15/2015 7:50 AM). My class map looks like this: public sealed class DutyMap : CsvClassMap<Duty>