Created
November 27, 2019 12:46
-
-
Save rpbeukes/e4721ca171d015075676a6c13a87a6c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class CSVHelperExtentions | |
{ | |
public static string ToCSVString<T>(this IEnumerable<T> objectToConvert) where T : class | |
{ | |
using (var memoryStream = new MemoryStream()) | |
{ | |
var config = new Configuration(); | |
using (var streamWriter = new StreamWriter(memoryStream)) | |
using (var csvWriter = new CsvWriter(streamWriter)) | |
{ | |
var options = new TypeConverterOptions | |
{ | |
Formats = new List<string>() { "yyyy-MM-dd HH:mm:ss"}.ToArray() | |
}; | |
csvWriter.Configuration.TypeConverterOptionsCache.AddOptions<DateTime>(options); | |
csvWriter.Configuration.ShouldQuote = (field, context) => true; | |
csvWriter.WriteRecords(objectToConvert); | |
} | |
return Encoding.ASCII.GetString(memoryStream.ToArray()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment