| | | 1 | | using System.Globalization; |
| | | 2 | | using CsvHelper; |
| | | 3 | | using CsvHelper.Configuration; |
| | | 4 | | |
| | | 5 | | namespace ContextProviders.Kicktipp.Csv; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Extension methods for writing objects to CSV using CsvHelper. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class CsvWriterExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Writes a collection of records to CSV format using the specified ClassMap. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <typeparam name="T">The type of records to write.</typeparam> |
| | | 16 | | /// <typeparam name="TMap">The ClassMap defining the CSV schema.</typeparam> |
| | | 17 | | /// <param name="records">The collection of records to write.</param> |
| | | 18 | | /// <returns>The CSV content as a string.</returns> |
| | | 19 | | public static string WriteToCsv<T, TMap>(this IEnumerable<T> records) |
| | | 20 | | where T : class |
| | | 21 | | where TMap : ClassMap<T>, new() |
| | | 22 | | { |
| | 1 | 23 | | using var stringWriter = new StringWriter(); |
| | 1 | 24 | | using var csvWriter = new CsvWriter(stringWriter, CultureInfo.InvariantCulture); |
| | | 25 | | |
| | 1 | 26 | | csvWriter.Context.RegisterClassMap<TMap>(); |
| | 1 | 27 | | csvWriter.WriteRecords(records); |
| | | 28 | | |
| | 1 | 29 | | return stringWriter.ToString(); |
| | 1 | 30 | | } |
| | | 31 | | } |