Create a gist now

Instantly share code, notes, and snippets.

ActionResult for ASP.NET MVC that returns a CSV file from any list of objects
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace webnetconf.website.Helpers
{
public class CsvActionResult<T> : FileResult
{
private readonly IList<T> _list;
private readonly char _separator;
public CsvActionResult(IList<T> list,
string fileDownloadName,
char separator=',')
: base("text/csv")
{
_list = list;
FileDownloadName = fileDownloadName;
_separator = separator;
}
protected override void WriteFile(HttpResponseBase response)
{
var outputStream = response.OutputStream;
using (var memoryStream = new MemoryStream())
{
WriteList(memoryStream);
outputStream.Write(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
}
private void WriteList(Stream stream)
{
var streamWriter = new StreamWriter(stream, Encoding.Default);
WriteHeaderLine(streamWriter);
streamWriter.WriteLine();
WriteDataLines(streamWriter);
streamWriter.Flush();
}
private void WriteHeaderLine(StreamWriter streamWriter)
{
foreach (MemberInfo member in typeof(T).GetProperties())
{
WriteValue(streamWriter, member.Name);
}
}
private void WriteDataLines(StreamWriter streamWriter)
{
foreach (T line in _list)
{
foreach (MemberInfo member in typeof(T).GetProperties())
{
WriteValue(streamWriter, GetPropertyValue(line, member.Name));
}
streamWriter.WriteLine();
}
}
private void WriteValue(StreamWriter writer, String value)
{
writer.Write("\"");
writer.Write(value.Replace("\"", "\"\""));
writer.Write("\"" + _separator);
}
public static string GetPropertyValue(object src, string propName)
{
return src.GetType().GetProperty(propName).GetValue(src, null).ToString()??"";
}
}
}
@mcbridedm

Please see revision https://gist.github.com/mcbrided/5306389/revisions to fix null object issue

@MrHarry01

How to call this can anybody help.
I m using this but it's not working

public CSVActionResult ExportData(ProjectViewModel model)
{
DataTable dt = new DataTable();
dt = AODataProvider.ExportSalesData(model.ID);
return new CSVActionResult(dt) { FileDownloadName = "ExportedFile.csv" };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment