Exporting to microsoft word Using .NET from a GRIDVIEW

Exporting to word using contents from a bound grid view. Please follow the comments for further explanation.

 //additional references to import using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html; using iTextSharp.text.html.simpleparser; public static void Export_Word(HttpResponse Response, GridView grid) { //filename for file to be generated string filename = DateTime.Now.ToString("0:yyyyMMddhhmmss") + ".doc"; Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment; filename=" + filename); Response.Charset = ""; Response.ContentType = "application/vnd.ms-word "; grid.AllowPaging = false; //Initialize a new string writer StringWriter sw = new StringWriter(); //initialize a new HtmlTextWriter HtmlTextWriter hw = <div><a href="http://canadianpharmacy-rxstorein.com">canada pharmacy</a> @ <a href="http://generic-viagraonline2sex.com">generic viagra</a> @ <a href="http://canadianpharmacy-rxstorein.com">canadianpharmacy-rxstorein</a> @ <a href="http://cialisonline-bestrxshop.com" rel="nofollow">cialisonline-bestrxshop</a> @ <a href="http://cialisonline-bestrxshop.com" rel="nofollow">http://cialisonline-bestrxshop.com/</a></div>  new HtmlTextWriter(sw); HtmlForm frm = new HtmlForm(); grid.Parent.Controls.Add(frm); frm.Attributes["runat"] = "server"; frm.Controls.Add(grid); frm.RenderControl(hw); //write the contents from the grid view Response.Write(sw.ToString()); Response.Flush(); Response.End(); }

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.