GridView as email attachment
How do I send the contents of a GridView as an email attachment?
GridView can be rendered into a string using the RenderControl() method. From the string, a buffer (of bytes) can be extracted assuming ASCII encoding. This buffer can be used as email attachment by streaming it through a MemoryStream object. The code is below:
protected void Submit_Click(object sender, EventArgs e) { // Render the contents of the GridView into a string StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); HtmlTextWriter htw = new HtmlTextWriter(sw); GridView1.RenderControl(htw); // Get the byte buffer from the string byte[] buffer = new byte[sb.Length]; ASCIIEncoding enc = new ASCIIEncoding(); buffer = enc.GetBytes(sb.ToString()); // Create attachment from MemoryStream MemoryStream ms = new MemoryStream(buffer); Attachment att = new Attachment(ms, "GridReport.xls"); }
Posted by Vijay on 25-Oct-2009 01:34 AM
Category : ASP.NET GridView
Category : ASP.NET GridView



Hey Vijay,
Your post was really helpful. Thanks for sharing!
Regards,
Neh