drupal stats

Export GridView to CSV

June 11th, 2011 Web Forms 1 Comment


Step1: Add HttpContext

HttpContext context = HttpContext.Current;

context.Response.Clear();

Step 2: Add CSV Header

for (int i = 0; i < GridView1.Columns.Count; i++)

{

    context.Response.Write(GridView1.Columns[i].HeaderText + "," );

}

context.Response.Write(Environment.NewLine);

Step 3: Add row and column value

for (int m = 0; m < GridView1.Rows.Count; m++)

{

    for (int n = 0; n < GridView1.Columns.Count; n++)

    {

        context.Response.Write(GridView1.Rows[m].Cells[n].Text + ",");

    }

    context.Response.Write(Environment.NewLine);

}

Step 4: Save CSV file

context.Response.ContentType = "text/csv";

context.Response.AppendHeader("Content-Disposition", "attachment; filename=GridView1.csv");

context.Response.End();

 

Complete Source Code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.IO;

using System.Data;

 

public partial class _Default : System.Web.UI.Page 

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            DataSet ds = new DataSet();

            ds.ReadXml(Server.MapPath("Products.xml"));

            GridView1.DataSource = ds;

            GridView1.DataBind();

        }

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        HttpContext context = HttpContext.Current;

        context.Response.Clear();

 

        for (int i = 0; i < GridView1.Columns.Count; i++)

        {

            context.Response.Write(GridView1.Columns[i].HeaderText + "," );

        }

 

        context.Response.Write(Environment.NewLine);

 

        for (int m = 0; m < GridView1.Rows.Count; m++)

        {

            for (int n = 0; n < GridView1.Columns.Count; n++)

            {

                context.Response.Write(GridView1.Rows[m].Cells[n].Text + ",");

            }

            context.Response.Write(Environment.NewLine);

        }

 

        context.Response.ContentType = "text/csv";

        context.Response.AppendHeader("Content-Disposition", "attachment; filename=GridView1.csv");

        context.Response.End();  

    }

}

 

Example

GridView Data Sheet

ExportGridViewtoCSV1 thumb Export GridView to CSV

Exported Data

ExportGridViewtoCSV2 thumb Export GridView to CSV 

Download Source Code

Tags: gridview to csv, export gridview to csv, c# datagridview export to csv, export gridview to csv in asp net, gridview csv, GridView export to CSV, how to export datagridview to csv in c#, export gridview to csv c#, c# export gridview to csv, asp net export gridview to csv

Related posts:

  1. Export GridView to Excel in ASP.NET

1 Comment

  1. RN024 says:

    Can show how the code would look like if the ProductID column was a hyperlink control. I don’t need the hyperlink to be passed, just the text. in your example, if a column is a hyperlink, the data for that column is blank, only the header is shown. Thanks for the help.