Created
August 17, 2016 20:34
-
-
Save KerryRitter/243ba96655c4f1fb239e1823d5a7c964 to your computer and use it in GitHub Desktop.
CsvBuilder.cs
This file contains 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
/// <summary> | |
/// Class for building CSV outputs | |
/// </summary> | |
/// <seealso cref="System.IDisposable" /> | |
public class CsvBuilder : IDisposable | |
{ | |
private readonly StringBuilder _stringBuilder; | |
private readonly string _delimiter; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="CsvBuilder"/> class. | |
/// </summary> | |
/// <param name="delimiter">The delimiter.</param> | |
public CsvBuilder(string delimiter) | |
{ | |
_stringBuilder = new StringBuilder(); | |
_delimiter = delimiter; | |
} | |
public void ParseObjectsToCsv<T>(IEnumerable<T> objects) | |
{ | |
if (objects == null || !objects.Any()) | |
{ | |
return; | |
} | |
_stringBuilder.Clear(); | |
var properties = typeof(T).GetProperties(); | |
var headers = properties.Select(p => p.Name); | |
AddLine(headers); | |
var lines = objects.Select(o => properties.Select(p => p.GetValue(o))); | |
AddLines(lines); | |
} | |
/// <summary> | |
/// Adds the lines. Each item is a row, and each item in the item is a column. | |
/// </summary> | |
/// <param name="lines">The lines.</param> | |
public void AddLines(IEnumerable<IEnumerable<object>> lines) | |
{ | |
foreach (var line in lines) | |
{ | |
AddLine(line); | |
} | |
} | |
/// <summary> | |
/// Adds the line one-column. | |
/// </summary> | |
/// <param name="line">The line.</param> | |
public void AddLine(object line) | |
{ | |
AddLine(new List<object> { line }); | |
} | |
/// <summary> | |
/// Adds the line. Each item is a column. | |
/// </summary> | |
/// <param name="line">The line.</param> | |
public void AddLine(IEnumerable<object> line) | |
{ | |
var lineAsStrings = line.Select(col => col?.ToString().Replace(_delimiter, string.Empty)).ToArray(); | |
_stringBuilder.AppendLine(string.Join(_delimiter, lineAsStrings)); | |
} | |
/// <summary> | |
/// Gets the output as a string. | |
/// </summary> | |
/// <returns></returns> | |
public string GetOutput() | |
{ | |
return _stringBuilder.ToString(); | |
} | |
/// <summary> | |
/// Returns a FileContentResult with the CSV data. | |
/// </summary> | |
/// <param name="fileName">Name of the file.</param> | |
/// <returns></returns> | |
public FileContentResult ToFileContentResult(string fileName) | |
{ | |
var fileContents = Encoding.ASCII.GetBytes(GetOutput()); | |
var result = new FileContentResult(fileContents, "text/csv") | |
{ | |
FileDownloadName = fileName | |
}; | |
return result; | |
} | |
public ResponseMessageResult ToResponseMessageResult(string fileName) | |
{ | |
var content = GetOutput(); | |
var result = new HttpResponseMessage(HttpStatusCode.OK) | |
{ | |
Content = new StringContent(content, Encoding.UTF8, "text/csv") | |
}; | |
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") | |
{ | |
FileName = fileName | |
}; | |
result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv"); | |
return new ResponseMessageResult(result); | |
} | |
/// <summary> | |
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | |
/// </summary> | |
public void Dispose() | |
{ | |
_stringBuilder.Clear(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment