| | 1 | | namespace Songhay.Text; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Transforms and exports the specified class to CSV format. |
| | 5 | | /// </summary> |
| | 6 | | /// <typeparam name="T">the class to export</typeparam> |
| | 7 | | /// <remarks> |
| | 8 | | /// Based on http://stackoverflow.com/questions/2422212/simple-c-sharp-csv-excel-export-class |
| | 9 | | /// </remarks> |
| | 10 | | public class CsvExporter<T> where T : class |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Initializes a new instance of the <see cref="CsvExporter{T}"/> class. |
| | 14 | | /// </summary> |
| | 15 | | /// <param name="rows">The rows.</param> |
| | 16 | | public CsvExporter(IEnumerable<T>? rows) |
| 0 | 17 | | : this(rows, null) |
| 0 | 18 | | { |
| 0 | 19 | | } |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Initializes a new instance of the <see cref="CsvExporter{T}"/> class. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="rows">The rows.</param> |
| | 25 | | /// <param name="columns">The columns.</param> |
| 0 | 26 | | public CsvExporter(IEnumerable<T>? rows, IEnumerable<string>? columns) |
| 0 | 27 | | { |
| 0 | 28 | | Rows = rows ?? Enumerable.Empty<T>(); |
| 0 | 29 | | Columns = columns ?? Enumerable.Empty<string>(); |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Gets the columns. |
| | 34 | | /// </summary> |
| 0 | 35 | | public IEnumerable<string> Columns { get; private set; } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Gets the rows. |
| | 39 | | /// </summary> |
| 0 | 40 | | public IEnumerable<T> Rows { get; private set; } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Exports this instance. |
| | 44 | | /// </summary> |
| | 45 | | public string Export() |
| 0 | 46 | | { |
| 0 | 47 | | return Export(true); |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Exports the specified include header line. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="includeHeaderLine">if set to <c>true</c> [include header line].</param> |
| | 54 | | public string Export(bool includeHeaderLine) |
| 0 | 55 | | { |
| 0 | 56 | | var sb = new StringBuilder(); |
| 0 | 57 | | IList<PropertyInfo> propertyInfoList = typeof(T).GetProperties(); |
| | 58 | |
|
| 0 | 59 | | if (includeHeaderLine) |
| 0 | 60 | | { |
| 0 | 61 | | if (Columns.Any()) |
| 0 | 62 | | { |
| 0 | 63 | | foreach (var propertyName in Columns) |
| 0 | 64 | | { |
| 0 | 65 | | sb.Append(propertyName).Append(','); |
| 0 | 66 | | } |
| 0 | 67 | | } |
| | 68 | | else |
| 0 | 69 | | { |
| 0 | 70 | | foreach (var propertyInfo in propertyInfoList) |
| 0 | 71 | | { |
| 0 | 72 | | sb.Append(propertyInfo.Name).Append(','); |
| 0 | 73 | | } |
| 0 | 74 | | } |
| | 75 | |
|
| 0 | 76 | | sb.Remove(sb.Length - 1, 1).AppendLine(); |
| 0 | 77 | | } |
| | 78 | |
|
| 0 | 79 | | foreach (T obj in Rows) |
| 0 | 80 | | { |
| 0 | 81 | | if (Columns != null) |
| 0 | 82 | | { |
| 0 | 83 | | foreach (var propertyName in Columns) |
| 0 | 84 | | { |
| 0 | 85 | | var propertyInfo = propertyInfoList.FirstOrDefault(i => i.Name == propertyName); |
| 0 | 86 | | if (propertyInfo != null) sb.Append(MakeCsvText(propertyInfo.GetValue(obj, null))).Append(','); |
| 0 | 87 | | } |
| 0 | 88 | | } |
| | 89 | | else |
| 0 | 90 | | { |
| 0 | 91 | | foreach (var propertyInfo in propertyInfoList) |
| 0 | 92 | | { |
| 0 | 93 | | sb.Append(MakeCsvText(propertyInfo.GetValue(obj, null))).Append(','); |
| 0 | 94 | | } |
| 0 | 95 | | } |
| | 96 | |
|
| 0 | 97 | | sb.Remove(sb.Length - 1, 1).AppendLine(); |
| 0 | 98 | | } |
| | 99 | |
|
| 0 | 100 | | return sb.ToString(); |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Exports to file. |
| | 105 | | /// </summary> |
| | 106 | | /// <param name="path">The path.</param> |
| 0 | 107 | | public void ExportToFile(string? path) => File.WriteAllText(path.ToReferenceTypeValueOrThrow(), Export()); |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Exports to bytes. |
| | 111 | | /// </summary> |
| 0 | 112 | | public byte[] ExportToBytes() => Encoding.UTF8.GetBytes(Export()); |
| | 113 | |
|
| | 114 | | static string MakeCsvText(object? value) |
| 0 | 115 | | { |
| 0 | 116 | | if (value == null) return string.Empty; |
| | 117 | |
|
| 0 | 118 | | if (value is DateTime time) |
| 0 | 119 | | { |
| 0 | 120 | | return time.ToString(time.TimeOfDay.TotalSeconds == 0 ? "yyyy-MM-dd" : "yyyy-MM-dd HH:mm:ss"); |
| | 121 | | } |
| | 122 | |
|
| 0 | 123 | | string output = value.ToString()!; |
| | 124 | |
|
| 0 | 125 | | if (output.Contains(',') || output.Contains('"')) |
| 0 | 126 | | output = $"\"{output.Replace("\"", "\"\"")}\""; |
| | 127 | |
|
| 0 | 128 | | return output; |
| 0 | 129 | | } |
| | 130 | | } |