public async Task CsvHelperAsync() { TextWriter tw = TextWriter.Null; var items = TestData.GetTestObjects(); var csv = new CsvHelper.CsvWriter(tw, new CsvConfiguration(CultureInfo.InvariantCulture)); csv.WriteField("Id"); csv.WriteField("Name"); csv.WriteField("Date"); csv.WriteField("IsActive"); for (int i = 0; i < ValueCount; i++) { csv.WriteField("Value" + i); } await csv.NextRecordAsync(); foreach (var item in items) { csv.WriteField(item.Id); csv.WriteField(item.Name); csv.WriteField(item.Date); csv.WriteField(item.IsActive); for (int i = 0; i < ValueCount; i++) { csv.WriteField(item.DataSet[i]); } await csv.NextRecordAsync(); } await csv.FlushAsync(); }
public async Task ConvertCsv() { var csvPath = @"C:\Users\sweet\Downloads\Beat Buddy Midi Drum Map - Map (1).csv"; using (var reader = new StreamReader(csvPath)) using (var writer = new StreamWriter("map.csv")) using (var csvReader = new CsvHelper.CsvReader(reader, CultureInfo.InvariantCulture)) using (var csvWriter = new CsvHelper.CsvWriter(writer, new CsvConfiguration(CultureInfo.InvariantCulture))) { await csvReader.ReadAsync(); csvReader.ReadHeader(); csvWriter.WriteHeader <Record>(); await csvWriter.NextRecordAsync(); while (await csvReader.ReadAsync()) { var inRecord = csvReader.GetRecord <dynamic>() as IDictionary <string, object>; var keys = inRecord.Keys; var outRecord = new Record() { Number = Convert.ToInt32(inRecord["Number"]), MidiNote = Convert.ToString(inRecord["Note"]), OctaveMiddleC3 = Convert.ToInt32(inRecord["Octave (Middle C3)"]), OctaveMiddleC4 = Convert.ToInt32(inRecord["Octave (Middle C4)"]) }; for (var k = 4; k < keys.Count; k++) { var key = keys.ElementAt(k); var value = Convert.ToString(inRecord[key]); // if (string.IsNullOrWhiteSpace(value)) // continue; var instrimentNote = GetInstrumentNote(value); outRecord.Kit = key; outRecord.Instrument = instrimentNote.Instrument; outRecord.KitNote = instrimentNote.KitNote; csvWriter.WriteRecord(outRecord); await csvWriter.NextRecordAsync(); } } } }