How to send grid view data in email

CustomerTable

 

Customer.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
   GridView1.DataSource = this.GetData();
   GridView1.DataBind();
}
}</pre><pre>

    private DataTable GetData()
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }
    }
    protected void ExportExcel(object sender, EventArgs e)
    {
       
        DataTable dt = GetData();
        dt.TableName = "GridData";
        using (XLWorkbook wb = new XLWorkbook())
        {
          
            wb.Worksheets.Add(dt);

            using (MemoryStream memoryStream = new MemoryStream())
            {
                //Save the Excel Workbook to MemoryStream.
                wb.SaveAs(memoryStream);

                //Convert MemoryStream to Byte array.
                byte[] bytes = memoryStream.ToArray();
                memoryStream.Close();

                //Send Email with Excel attachment.
                using (MailMessage mm = new MailMessage("sender_email_id", "receiver_email_id"))
                {
                    mm.Subject = "Puppy bhai demo";
                    mm.Body = "GridView Exported Excel Attachment";

                    //Add Byte array as Attachment.
                    mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "GridView.xlsx"));
                    mm.IsBodyHtml = true;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.EnableSsl = true;
                    System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
                    credentials.UserName = "sender_email_id";
                    credentials.Password = "password";
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = credentials;
                    smtp.Port = 587;
                    smtp.Send(mm);
                }
            }
        }
    }

Customer.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<br />
<form id="form1" runat="server" class="container">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CssClass="table table-border table-condensed">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Customer Id" ItemStyle-Width="80" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="120" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="120" />
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Export" OnClick="ExportExcel" runat="server" class="btn btn-success"/>
</form>
</body>
</html>

 

 

 

 

Tags: , ,