Skip to content

Instantly share code, notes, and snippets.

@simonech
Created November 18, 2012 10:27
  • Star 9 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save simonech/4104490 to your computer and use it in GitHub Desktop.
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
Copy link

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

@MrHarry01
Copy link

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" };
}

@jeffersantoss
Copy link

jeffersantoss commented Oct 10, 2018

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

I made this change on my code like bellow:

public static string GetPropertyValue(object src, string propName)
{
            var type = src.GetType();
            var property = type.GetProperty(propName);
            var value = property.GetValue(src, null);
            return value == null ? "" : value.ToString();
}

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