assalam...saya ade code utk eksport gridview to excel tp ade error yg sy xfaham..ni coding saya..FYI sy baru lg dlm c#..huhuh..sy gune vs2008 mssqlserver2008..dlm page design ade gune Ajax toolkit..sy mahu report dari gridview bile click buttonexport dier bole pegi ke excel format...mintak tolong ya..
coding ni dlm page aspx.Cs
using System;
using System.Data;
using System.IO;
using System.Web.UI;
using App_Code.Utility;// namespace ni bile declare jadi kelabu n bgtaw "using directive is not required"
using System.Web.Security;//namespace ni bile declare jadi kelabu n bgtaw "using directive is not required"
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;//namespace ni bile declare jadi kelabu n bgtaw "using directive is not required"
using System.Web.UI.HtmlControls;//namespace ni bile declare jadi kelabu n bgtaw "using directive is not required"
protected void Button3_Click(object sender, EventArgs e)//ni buttonexport.
{
PrepareGridViewForExport(GridView1);
ExportGridView();
}
private void ExportGridView()
{
string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
private void PrepareGridViewForExport(Control gv)
{
LinkButton lb = new LinkButton();
Literal l = new Literal();
string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++)
{
if (gv.Controls[i].GetType() == typeof(LinkButton))
{
l.Text = (gv.Controls[i] as LinkButton).Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(DropDownList))
{
l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(CheckBox))
{
l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
if (gv.Controls[i].HasControls())
{
PrepareGridViewForExport(gv.Controls[i]);
}
}
coding ni dlm page aspx.Cs
using System;
using System.Data;
using System.IO;
using System.Web.UI;
using App_Code.Utility;// namespace ni bile declare jadi kelabu n bgtaw "using directive is not required"
using System.Web.Security;//namespace ni bile declare jadi kelabu n bgtaw "using directive is not required"
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;//namespace ni bile declare jadi kelabu n bgtaw "using directive is not required"
using System.Web.UI.HtmlControls;//namespace ni bile declare jadi kelabu n bgtaw "using directive is not required"
protected void Button3_Click(object sender, EventArgs e)//ni buttonexport.
{
PrepareGridViewForExport(GridView1);
ExportGridView();
}
private void ExportGridView()
{
string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
private void PrepareGridViewForExport(Control gv)
{
LinkButton lb = new LinkButton();
Literal l = new Literal();
string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++)
{
if (gv.Controls[i].GetType() == typeof(LinkButton))
{
l.Text = (gv.Controls[i] as LinkButton).Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(DropDownList))
{
l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(CheckBox))
{
l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
if (gv.Controls[i].HasControls())
{
PrepareGridViewForExport(gv.Controls[i]);
}
}