By 27/02/2014 25 Comments

How to Export CSV File From Gridview in ASP.NET using C#

What is a CSV file ?

  • It stands for comma separated values
  • These are different multiple values with comma separation (e.g php, javascript, asp, facebook etc) in a file
  • This file’s extension is .csv and it contains comma separated values or data inside it

What is a Grid view in asp.net ?

  • It is a table which contains data or information in a tabular form
  • It contains static as well as dynamic data (from database)
  • It has paging, sorting and more basic functions etc
  • In asp.net, it is a server side GridView control which is a part of .net framework

In the following, we discuss how to export csv file from Gridview in ASP.NET using C#.NET

Code Snippet

Response.ClearContent();
                Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "NewsLetterSubscribers.csv")); // AddHeader method pass 3 parameters, first string, second attachement string, and 3rd one the .csv file to be exported. AddHeader() method sends an attachment to the client or browser directly here that attachment is .CSV file

                Response.ContentType = "application/text"; // this line tells the browser that the contents of this file is a simple text

                // gvdetails is name of Girdview
                gvdetails.AllowPaging = false; // paging is false

                gvdetails.DataBind();

                var strbldr = new StringBuilder(); // crate an object of stringBuilder() class

                for (var i = 0; i < gvdetails.Columns.Count; i++) // loop through each data items in grid view control
                {
                    //separting header columns text with comma operator
                    strbldr.Append(gvdetails.Columns[i].HeaderText + ',');
                }

                //appending new line for gridview header row
                strbldr.Append("\n");

                for (var j = 0; j < gvdetails.Rows.Count; j++)
                {
                    for (var k = 0; k < gvdetails.Columns.Count; k++)
                    {
                        //separating gridview columns with comma
                        strbldr.Append(gvdetails.Rows[j].Cells[k].Text + ',');
                    }
                    //appending new line for gridview rows
                    strbldr.Append("\n");
                }

                Response.Write(strbldr.ToString());

                Response.End();





Next Tutorial →



Posted in: ASP.NET, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .