Sunday, November 22, 2009

Export dataset to word in c#

The follwing function export dataset to word in c#,

public static void Convertword(DataTable dt, HttpResponse Response)
{
try
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Product.doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
System.Web.UI.WebControls.GridView dg = new System.Web.UI.WebControls.GridView();
dg.DataSource = dt;
dg.DataBind();
dg.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
catch(Exception err)
{
throw err;
}
}

No comments:

Post a Comment