By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,011 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

How to Export a GridView Data to CSV

P: 17
Language: ASP.net
Platform: Visual Studio 2008 with ASP.net
Technology: Used in ASP.net
Level: Beginner

Introduction

1. Add a gridview into a aspx file
2. Add a button into a aspx file and give the name as "btnExportToCSV"
3. Write a code in aspx.cs file

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. namespace Example
  9. {
  10.     public partial class ExportFiles : System.Web.UI.Page
  11.     {
  12.         protected void Page_Load(object sender, EventArgs e)
  13.         {
  14.             DataTable dtRecords = new DataTable();
  15.             dtRecords.Columns.Add("State", typeof(string));
  16.             dtRecords.Columns.Add("City", typeof(string));
  17.             DataRow dr = dtRecords.NewRow();
  18.             dr["State"] = "Karnataka";
  19.             dr["City"] = "Bangalore";
  20.             dtRecords.Rows.Add(dr);
  21.  
  22.             grdData.DataSource = dtRecords;
  23.             grdData.DataBind();
  24.         }
  25.  
  26.         protected void btnExportToCSV_Click(object sender, EventArgs e)
  27.         {
  28.              Response.Clear();
  29.  
  30.             Response.Buffer = true;
  31.  
  32.             Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
  33.  
  34.             Response.Charset = "";
  35.  
  36.             Response.ContentType = "application/text";
  37.  
  38.             GridView1.AllowPaging = false;
  39.  
  40.             StringBuilder sb = new StringBuilder();
  41.  
  42.             for (int k = 0; k < GridView1.Columns.Count; k++)
  43.             {
  44.                 sb.Append(GridView1.Columns[k].HeaderText + ',');
  45.             }
  46.  
  47.             sb.Append("\r\n");
  48.             for (int i = 0; i < GridView1.Rows.Count; i++)
  49.             {
  50.  
  51.                 for (int k = 0; k < GridView1.Columns.Count; k++)
  52.                 {
  53.                     sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
  54.                 }
  55.  
  56.                 sb.Append("\r\n");
  57.             }
  58.  
  59.             Response.Output.Write(sb.ToString());
  60.  
  61.             Response.Flush();
  62.  
  63.             Response.End();
  64.         }
  65.         public override void VerifyRenderingInServerForm(Control control)
  66.         {
  67.  
  68.         }
  69.  
  70.  
  71.     }
  72. }
Summary:
Run the application click on button, it will ask to save or open a file, if you save it will be saved into your disk
or if you click on open it directely open the excel file.


References

License
May 10 '11 #1
Share this Article
Share on Google+
1 Comment


P: 2
Expand|Select|Wrap|Line Numbers
  1. protected void btntoCsv_Click(object sender, EventArgs e)
  2. {
  3.     Response.Clear();
  4.     Response.Buffer = true;
  5.     Response.AddHeader("content-disposition", "attachment;filename=gvtocsv.csv");
  6.     Response.Charset = "";
  7.     Response.ContentType = "application/text";
  8.     StringBuilder sBuilder = new System.Text.StringBuilder();
  9.     for (int index = 0; index < GridView1.Columns.Count; index++)
  10.     {
  11.         sBuilder.Append(GridView1.Columns[index].HeaderText + ',');
  12.     }
  13.     sBuilder.Append("\r\n");
  14.     for (int i = 0; i < GridView1.Rows.Count; i++)
  15.     {
  16.         for (int k = 0; k < GridView1.HeaderRow.Cells.Count; k++)
  17.         {
  18.             sBuilder.Append(GridView1.Rows[i].Cells[k].Text.Replace(",", "") + ",");
  19.         }
  20.         sBuilder.Append("\r\n");
  21.     }
  22.     Response.Output.Write(sBuilder.ToString());
  23.     Response.Flush();
  24.     Response.End();
  25. }
  26.  
Drag a GridView and add the above code.

full source ...GridView to CSV

Lynda
Mar 17 '14 #2