Export data from Grid view to excel

Posted by Kondapallisrinivas under ASP.NET on 9/7/2012 | Points: 10 | Views : 6441 | Status : [Member] | Replies : 6
Hello All

Please suggest how i export data from Grid to excel????




Responses

Posted by: Ranjeet_8 on: 9/7/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down

Posted by: Ranjeet_8 on: 9/7/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Download attached sample file from this article
http://www.dotnetfunda.com/articles/article1150-all-in-one-export-data-in-aspnet-gridview-to-doc-gridview-to-excel-gr.aspx

Kondapallisrinivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Megan00 on: 9/9/2012 [Member] Starter | Points: 25

Up
0
Down
Hello, you can use a free source, free Data Export component (http://www.e-iceblue.com/Introduce/free-dataexport-component.html )to realize it quickly, this free component also can export data from Gridview to Word and Html , detals can be seen:

http://blog.csdn.net/l_vincent/article/details/6738725

Never give up! Smile to the world!
http://excelcsharp.blog.com/

Kondapallisrinivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Johnbhatt on: 9/10/2012 [Member] Starter | Points: 25

Up
0
Down
Simple and Plain code is here...

http://www.dotnetfunda.com/codes/code2691-export-to-excel-from-datagridview-in-aspnet.aspx

John Bhatt
Glad to Know, Free to Share.....
http://www.johnbhatt.com

Kondapallisrinivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 9/10/2012 [Member] Starter | Points: 25

Up
0
Down
protected void lnkExport_Click(object sender, EventArgs e)
{
if (gv.Rows.Count > 0)
{
Response.Buffer = true;
string attachment = "attachment; filename=Excel.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
else
{
lblError.Text="No Records Found";
}
}
public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{



}
private void PrepareGridViewForExport(System.Web.UI.Control gv)
{
LinkButton lb = new LinkButton();
Literal l = new Literal();
string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++)
{
PrepareGridViewForExport(gv.Controls[i]);
}
}

Kondapallisrinivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 1/24/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Default.aspx code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<asp:GridView ID="g1" runat="server">
</asp:GridView>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Export data to Excel"
BackColor="Yellow" Font-Bold="True" ForeColor="#FF3300"></asp:Label><br />

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl= "~/images/Excel2.JPG" />

</form>
</body>
</html>

Default.aspx.vb code

Imports System.Data
Imports System.Data.SqlClient
Imports System.IO

Partial Class _Default
Inherits System.Web.UI.Page
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
Dim con As New SqlConnection(strConnString)
Dim str As String
Dim com As SqlCommand
Dim sqlda As SqlDataAdapter
Dim ds As DataSet
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
bindgrid()
g1.Visible = False
End Sub
Sub bindgrid()
con.Open()
str = "select * from SampleCustomer"
com = New SqlCommand(str, con)
sqlda = New SqlDataAdapter(com)
ds = New DataSet
sqlda.Fill(ds, "SampleCustomer")
g1.DataSource = ds
g1.DataMember = "SampleCustomer"
g1.DataBind()
con.Close()
End Sub
Private Sub ExportToExcel(ByVal strFileName As String, ByVal dg As GridView)
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False
Dim oStringWriter As New System.IO.StringWriter
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)

g1.RenderControl(oHtmlTextWriter)

Response.Write(oStringWriter.ToString())
Response.[End]()

End Sub

Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
g1.Visible = True
ExportToExcel("Report.xls", g1)
End Sub
Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)

End Sub
End Class


If this post helps you mark it as answer
Thanks

Kondapallisrinivas, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response