Here is some code that I whpped together which shows how to create a pdf or xls document from a RadGrid and attach it to an email.
I only had time to draft the basics, but you should get the idea and I thought that I would share since members like Princy helped me get here.
I only had time to draft the basics, but you should get the idea and I thought that I would share since members like Princy helped me get here.
<telerik:RadGrid ID="RadGrid1" AllowSorting="false" AutoGenerateColumns="false" AlternatingItemStyle-HorizontalAlign="center" AlternatingItemStyle-BackColor="#ffffff" GroupingEnabled="true" OnExcelExportCellFormatting="RadGrid1_ExcelExportCellFormatting" OnGridExporting="RadGrid1_GridExporting" OnPdfExporting="RadGrid1_PdfExporting" ExportSettings-Excel-Format="Html" ExportSettings-ExportOnlyData="true" Width="100%" runat="server"> <MasterTableView Width="99%"> <Columns> ...using System;using System.Collections.Generic;using System.Configuration;using System.Data;using System.IO;using System.Net;using System.Net.Mail;using System.Text;using System.Web.UI;using System.Web.UI.WebControls;using Mec.CommonLibrary.Cryptography;using MEC.EMA.BLL;using Microsoft.Security.Application;using Telerik.Web.UI;public class EmailRadGridAttachment{ List<Person> _userList = new List<Person>(); UserBLL _userBLL = new UserBLL(); protected void Page_Load(object sender, EventArgs e) { BindGrid(); } protected void BindGrid() { _userList = _userListBLL.GetUserById(_userId); RadGrid1.DataSource = _userList; } // aspx page with a button that fires this event for Excel protected void ButtonExcel_Click(object sender, EventArgs e) { ConfigureExport(); RadGrid Grid1 = (RadGrid)Page.Master.FindControl("Main").FindControl("RadGrid1"); Grid1.MasterTableView.ExportToExcel(); } // aspx page with a button that fires this even for PDF protected void ButtonPdf_Click(object sender, System.EventArgs e) { ConfigureExport(); RadGrid Grid1 = (RadGrid)Page.Master.FindControl("Main").FindControl("RadGrid1"); Grid1.ExportSettings.Pdf.AllowPrinting = true; Grid1.ExportSettings.Pdf.AllowModify = true; Grid1.ExportSettings.Pdf.AllowCopy = true; Grid1.ExportSettings.Pdf.AllowAdd = true; // I found that if I don't use postscript fonts the data does not make it to the attachment Grid1.ExportSettings.Pdf.DefaultFontFamily = "Helvetica"; Grid1.ExportSettings.Pdf.PageLeftMargin = new Unit(2, UnitType.Mm); Grid1.ExportSettings.Pdf.PageRightMargin = new Unit(2, UnitType.Mm); Grid1.ExportSettings.Pdf.PageHeaderMargin = new Unit(0, UnitType.Mm); Grid1.ExportSettings.Pdf.PaperSize = GridPaperSize.Legal; // to get lanscape orientation Grid1.ExportSettings.Pdf.PageHeight = Unit.Parse("210mm"); // Width based on TOU and NON-TOU Columns and the smaller parse the more room for data Grid1.ExportSettings.Pdf.PageWidth = Unit.Parse("420mm"); foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns) { // We have the ability to control the width of columns in the export here if (col.UniqueName != "MiddleInitial") { col.HeaderStyle.Width = Unit.Pixel(50); } } Grid1.MasterTableView.ExportToPdf(); } protected void RadGrid1_PdfExporting(object sender, GridPdfExportingArgs e) { // I have added some more information that will be included in the PDF above the RadGrid here StringBuilder customHTML = new StringBuilder(); customHTML.Append("<p style='color: red; font-weight: bold; font-size: 10pt;'>Title Page</p>"); customHTML.Append("<table style='font-family:Helvetica;font-size:9pt;width:500px;'>"); // Very Important to make sure you have a colgroup and the same number of "<col>" as "<td>" customHTML.Append("<colgroup><col style='width: 200px; white-space:nowrap;' /><col style='text-align:left;' /></colgroup>"); // ColGroup = Number of <td> in a row is needed for Telerik export to PDF customHTML.Append("<tr><td>Customer: " + CustomerName.Text + "</td><td>Date: " + LabelDate.Text + " </td></tr>"); customHTML.Append("<tr><td>Address: " + LabelAddress.Text + "</td><td>Email: " + LabelEmail.Text + " </td></tr>"); customHTML.Append("<tr><td>City: " + LabelCity.Text + "</td><td>Phone: " + LabelPhone.Text + " </td></tr>"); customHTML.Append("<tr><td>State: " + LabelState.Text + "</td><td>Zip: " + LabelZipCode.Text + " </td></tr>"); customHTML.Append("</table>"); e.RawHTML = customHTML + e.RawHTML; } public void ConfigureExport() { RadGrid Grid1 = (RadGrid)Page.Master.FindControl("Main").FindControl("RadGrid1"); Grid1.ExportSettings.ExportOnlyData = true; Grid1.ExportSettings.IgnorePaging = true; // We don't want the Open, Save or Cancel prompt to open for a new window when calling this page Grid1.ExportSettings.OpenInNewWindow = false; } protected void RadGrid1_GridExporting(object source, GridExportingArgs e) { StringBuilder customHTML = new StringBuilder(); MemoryStream gridMemoryStream = new MemoryStream(new ASCIIEncoding().GetBytes(e.ExportOutput)); if (e.ExportType == ExportType.Excel) { SendEMailWithAttachment(gridMemoryStream, "attachment.xls"); } // Stuff the Grid in a memory stream and pass it to the email method if (e.ExportType == ExportType.Pdf) { SendEMailWithAttachment(gridMemoryStream, "attachment.pdf"); } gridMemoryStream.Close(); // Redirect to the same page so the alert does not pop-up Response.Redirect(Request.Url.AbsoluteUri); } public void SendEMailWithAttachment(Stream attachmentStream, string fileName) { //settings from the web.config SmtpClient smtpClient = new SmtpClient(Convert.ToString(ConfigurationManager.AppSettings["SERVER_EMAIL"])); string authType = "Basic"; MailMessage email = new MailMessage(); StringBuilder body = new StringBuilder(); string emaURL = Convert.ToString(ConfigurationManager.AppSettings["BASE_URL"]); email.From = new MailAddress("From@gmail.com"); email.To.Add("To@gmail.com"); email.Subject = "This is the subject"; body.Append("<table style='font-family:Helvetica;font-size:9pt;width:600px;'>"); body.Append("<tr><td>You can put in whatever you want here</td></tr>"); body.Append("</table>"); // Attachment goes here email.Attachments.Add(new Attachment(attachmentStream, fileName)); email.Body = body.ToString(); email.IsBodyHtml = true; NetworkCredential nc = new NetworkCredential(Convert.ToString(ConfigurationManager.AppSettings["CredentialU"]), Convert.ToString(ConfigurationManager.AppSettings["CredentialP"])); smtpClient.Credentials = nc.GetCredential(Convert.ToString(smtpClient), 25, authType); smtpClient.Send(email); }}