Tuesday, May 5, 2009

How Export Data from DataGrid to Excel

In-line Code:

"http://www.w3.org/1999/xhtml" >
"server">




Top of Form
"form1" runat="server">


"Table" AlternatingRowStyle-BackColor="Lavender" PageSize="10" CellPadding="4" runat="server" AllowPaging="True" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" ForeColor="Black">
"TopAndBottom" />
"#CCCCCC" />
"White" />
"#000099" Font-Bold="True" ForeColor="White" />
"Left" BackColor="#CCCCCC" ForeColor="Black" />
"Black" Font-Bold="True" ForeColor="White" />





"BtnExport" runat="server" OnClick="BtnExport_Click"
Text="Export_Data_To_Excel" />


Bottom of Form




Code-Behind:


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OracleClient;

public partial class ExportGridViewDataIntoExcel : System.Web.UI.Page
{

OracleConnection oOrcCon = new OracleConnection();

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
BindDataToGrid(GetDataSetFromSource()); // Binding Data to the Table
}
protected void BtnExport_Click(object sender, EventArgs e)
{
string FileName = "ExportedExcel.xls";
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename="+FileName+"");
Response.Charset = "";

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter oWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(oWriter);
Table.RenderControl(htmlWrite);
Response.Write(oWriter.ToString());
Response.End();
}

///
/// Property for setting ConnectionString
///
private string LocalString
{
get { return @"Password=XXXX;User ID=Intranet;Data Source=XXXX;Persist Security Info=True"; }
}

///
/// Open the connection
///
private void OpenConnection()
{
if (oOrcCon.State != ConnectionState.Open && !string.IsNullOrEmpty(LocalString))
oOrcCon.ConnectionString = LocalString;
oOrcCon.Open();
}
///
/// Close Connection
///
private void CloseConnection()
{
if (oOrcCon.State == ConnectionState.Open)
oOrcCon.Close();
}

///
/// Extacting DataSet from resultant of Query
///
///
public System.Data.DataSet GetDataSetFromSource()
{
DataSet ds = new DataSet();
try
{
OpenConnection();
string str = @"select * from Project order by Project_Name";
OracleCommand Cmd = new OracleCommand(str, oOrcCon);
OracleDataAdapter apter = new OracleDataAdapter(Cmd);
apter.Fill(ds);
return ds;
}
catch (Exception ex)
{
Response.Write("Error in extracting Dataset: " + ex.ToString());
return null;
}
finally
{
CloseConnection();
}
}

///
/// Bind to the DataGrid
///
///
private void BindDataToGrid(DataSet ds)
{
if (ds != null)
{
Table.DataSource = ds;
Table.DataBind();

}

}

public override void VerifyRenderingInServerForm(Control Table)
{

}

}

No comments:

Post a Comment

Locations of visitors to this page