Here I will explain how to export gridview data to CSV or text document using asp.net.
Description:
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td align="right"> <asp:ImageButton ID="btnCSV" runat="server" ImageUrl="~/CSVImage.jpg" onclick="btnCSV_Click" /> </td> </tr> <tr> <td> <asp:GridView runat="server" ID="gvdetails" DataSourceID="dsdetails" AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false"> <RowStyle BackColor="#EFF3FB" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:BoundField DataField="UserId" HeaderText="UserId" /> <asp:BoundField DataField="UserName" HeaderText="UserName" /> <asp:BoundField DataField="LastName" HeaderText="LastName" /> <asp:BoundField DataField="Location" HeaderText="Location" /> </Columns> </asp:GridView> </td> </tr> </table> <asp:SqlDataSource ID="dsdetails" runat="server" ConnectionString="<%$ConnectionStrings:dbconnection %>" SelectCommand="select * from UserInformation"/> </div> </form> </body> </html> |
<connectionStrings> <add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/> </connectionStrings> |
using System.IO; |
public override void VerifyRenderingInServerForm(Control control) { /* Verifies that the control is rendered */ } /// <summary> /// This event is used to export gridview data to CSV document /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnCSV_Click(object sender, ImageClickEventArgs e) { Response.ClearContent(); Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.csv")); Response.ContentType = "application/text"; gvdetails.AllowPaging = false; gvdetails.DataBind(); StringBuilder strbldr = new StringBuilder(); for (int i = 0; i < gvdetails.Columns.Count; i++) { //separting header columns text with comma operator strbldr.Append(gvdetails.Columns[i].HeaderText + ','); } //appending new line for gridview header row strbldr.Append("\n"); for (int j = 0; j < gvdetails.Rows.Count; j++) { for (int 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(); } | |
Now if you’re getting any error message like
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|





Subscribe by RSS
Subscribe by Email
9 comments :
Hi
This code is error free and is very useful
Thanks
Hi i used gridview to PDF file this result PDF file is create but it's not come record..
error is created- Token type was not recognized..
i can't solve this error.. Give me the solution
hi to export gridview data to PDF check this link here i explained clearly it will work for you
http://www.aspdotnet-suresh.com/2011/04/how-to-export-gridview-data-to-pdf.html
Hi,
i m using above code it work fine when all column was bound column in grid view,but i m getting stuck when the grid view have a label control for this in CSV found black value.
I am trying to export to csv and I am only getting headers and no data rows. I have data in my data grid. Any help would be appreciated.
how to import .csv file
If I have to enter some notes at the end of csv file then what to do???
Means 4 lines of notes.
but it does not affect above columns.
Dear Sir, I have grid view with template fields. While exporting to CSV only header row is exporting but other rows are showing blank values. Please help
... Ramesh Raghuvanshi
whenever i click on csv image button, a message comes that your current setting do not allowed file to be downloaded