/// <summary> /// Transform an IEnumerable to CSV and write it to a file using custom settings. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <param name="outputFilename"></param> /// <param name="csvConfig"></param> /// <returns></returns> public static async Task ToCsv <T>(this IEnumerable <T> data, string outputFilename, CsvConfig csvConfig) { using var writer = new StreamWriter(outputFilename); await data.ToCsv(writer, csvConfig); }
/// <summary> /// Transform an IEnumerable to a CSV and write it to a Stream using custom settings. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <param name="stream"></param> /// <param name="csvConfig"></param> /// <returns></returns> public static async Task ToCsv <T>(this IEnumerable <T> data, Stream stream, CsvConfig csvConfig) { using var writer = new StreamWriter(stream); await data.ToCsv(writer, csvConfig); }
internal static IFormatProvider GetFormatProvider <T>(this CsvConfig config, T obj) { config.FormatProviders.TryGetValue(obj.GetType(), out IFormatProvider formatProvider); return(formatProvider); }
/// <summary> /// Add a case-sensitive property name to exclude from the resulting CSV /// </summary> /// <param name="config"></param> /// <param name="propertyName"></param> /// <returns></returns> public static CsvConfig IgnoreProperty(this CsvConfig config, string propertyName) { config.IgnoredProperties.Add(propertyName); return(config); }
/// <summary> /// Set a format provider for a type that implements IFormattable /// </summary> /// <typeparam name="T">A type that implements IFormattable</typeparam> /// <param name="config"></param> /// <param name="formatProvider"></param> /// <returns></returns> public static CsvConfig UseFormatProvider <T>(this CsvConfig config, IFormatProvider formatProvider) where T : IFormattable { config.FormatProviders.Add(typeof(T), formatProvider); return(config); }
/// <summary> /// Set a filter for a type that implements IFormattable /// </summary> /// <typeparam name="T">A type that implements IFormattable</typeparam> /// <param name="config"></param> /// <param name="filter"></param> /// <returns></returns> public static CsvConfig UseFilter <T>(this CsvConfig config, string filter) where T : IConvertible { config.Filters.Add(typeof(T), filter); return(config); }
/// <summary> /// Determine whether or not to quote qualify columns, and also define the quote qualification character/string /// </summary> /// <param name="config"></param> /// <param name="quoteQualify">default = true</param> /// <param name="quoteCharacter">default = "</param> /// <returns></returns> public static CsvConfig UseQuoteQualification(this CsvConfig config, bool quoteQualify = true, string quoteCharacter = "\"") { config.QuoteQualified = quoteQualify; config.QuoteCharacter = quoteCharacter; return(config); }