how to move the table from ms sql server to Excel using asp.net with c#

Posted by sailulakshmid-21627 under ASP.NET on 7/26/2013 | Points: 10 | Views : 999 | Status : [Member] | Replies : 3
code to how to move the table data from ms sql server to Excel using asp.net with c# code




Responses

Posted by: Bandi on: 7/26/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
I can refer the following link to export table data to Excel. Here you can get step by step procedure to follow
http://www.aspsnippets.com/Articles/Export-DataSet-or-DataTable-to-Word-Excel-PDF-and-CSV-Formats.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

sailulakshmid-21627, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 7/26/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="import_the_gridview_data_in_excel._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>



<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Button ID="btn_import" runat="server"
Text="Import to Excel" onclick="btn_import_Click" />


</div>

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



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;
namespace import_the_gridview_data_in_excel
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand cmd;
SqlDataAdapter sqlda;
DataSet ds;
string str;


protected void btn_import_Click(object sender, EventArgs e)
{
ExportToExcel("Report.xls", GridView1);
}

private void ExportToExcel(string strFileName, GridView dg)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
GridView1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();

}
public override void VerifyRenderingInServerForm(Control control)
{
}




}
}


If this post helps you mark it as answer
Thanks

sailulakshmid-21627, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: aswinialuri-19361 on: 7/30/2013 [Member] Starter | Points: 25

Up
0
Down
hi ,
try this
<body>

<form id="form1" runat="server">
<div>
<table>
<tr>
<td align="right">
<asp:ImageButton ID="btnExcel" runat="server" ImageUrl="~/ExcelImage.jpg"
onclick="btnExcel_Click" />
<%--<asp:ImageButton ID="btnWord" runat="server" ImageUrl="~/WordImage.jpg"
onclick="btnWord_Click" />--%>
</td>
</tr>
<tr>
<td>
<asp:GridView runat="server" ID="gvdetails" DataSourceID="SqlDataSource1"
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="id" HeaderText="id"
SortExpression="id" />
<asp:BoundField DataField="salary" HeaderText="salary"
SortExpression="salary" />
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:EmpConnectionString %>"
SelectCommand="SELECT * FROM [mytab]"></asp:SqlDataSource>

</td>
</tr>
</table>
</div>
</form>
</body>

code in.aspx.cs page:
public override void VerifyRenderingInServerForm(Control control)

{
/* Verifies that the control is rendered */
}
protected void btnExcel_Click(object sender, ImageClickEventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Funds tRANSFER.xls"));
Response.ContentType = "application/ms-excel";
//StringWriter sw = new StringWriter();
//HtmlTextWriter htw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
Response.End();
}

ihope you can understand

Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

sailulakshmid-21627, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response