473,385 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

How to Export a GridView Data to CSV

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
1 7795
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

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Nalaka | last post by:
Hi, I have two questions about gridViews. 1. How can I intercept the row/column values at loading to change values? 2. After I update a row (using default update functionality), how can I...
0
by: johnlim20088 | last post by:
Hi, Hi, currently I have a code below on my Listcontact.aspx file to export my gridview data to excel:- It is work success, and export the 'Contacts.xls' to my folder. But have following...
1
by: feltra | last post by:
Hi, The following is from my friend who has only restricted net access from his office and hence cannot post.... ...
0
by: feltra | last post by:
Hi all, I am trying to export a GridView data to multiple file formats. The requirement is that when more than one file format is selected and the "Submit" button is clicked, the data from the...
0
by: mahesh123 | last post by:
Hi, I am dispalying data in the gridview.I want to export gridview data to excel sheet. Can any one help me how to write code in asp.net with VB. Thanks
2
by: newtodotnet | last post by:
Hi, How to Export Gridview to pdf in asp.net I searched in many forums. but many of them said to use third party tools. I don't want any third party tools. Suggest me any sample for without...
2
by: satwinder singh | last post by:
Please help me regarding how to Export the Data from DataSet or GridView into Excel sheet. Kind Regards, Satwinder singh
11
by: ulai | last post by:
Hello everyone, I want to export GridView (25000 records) to Excel. But i got an error message like : System.OutOfMemoryException was Thrown. But i need to export the records (even more than...
0
by: vivek kapile | last post by:
Language: ASP.net Platform: Visual Studio 2008 with ASP.net Technology: Used in ASP.net Introduction 1. Add a gridview into a aspx file 2. Add a button into a aspx file and give the name as...
0
by: vivek kapile | last post by:
Language: ASP.net Platform: Visual Studio 2010 with ASP.net Technology: Used in ASP.net Level: Beginner Note: You have to download the itextsharp.dll file and add as a reference to your...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.