Skip to content

Instantly share code, notes, and snippets.

@greggnakamura
Created November 19, 2012 18:01
Show Gist options
  • Save greggnakamura/4112355 to your computer and use it in GitHub Desktop.
Save greggnakamura/4112355 to your computer and use it in GitHub Desktop.
MVC: Simple Export to CSV file
var db = new PetaPoco.Database(ConnectionHelper.ConnectionStringName);
var internalUsers = db.Fetch<InternalUser>("Order by InternalUserID");
StringWriter sw = new StringWriter();
sw.WriteLine("\"UserID\",\"FirstName\",\"LastName\",\"EmailAddress\"");
foreach (var user in internalUsers)
{
sw.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\"",
user.UserID,
user.FirstName,
user.LastName,
user.EmailAddress));
}
Response.AddHeader("Content-Disposition", "attachment; filename=internal-users.csv");
Response.ContentType = "text/csv";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.Write(sw);
Response.End();
return View();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment