i have done sending gridview data as excel to mail, it is working fine
then i had 5 columns in gridview but i want to send 4 column instead of 5 column.
please tell me
. Thanks in advance
protected void Button1_Click(object sender, EventArgs e)
{
fn_AttachGrid(); // here calling function to send mail gridview data as excel format
}
public void fn_AttachGrid()
{
try
{
StringWriter stw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(stw);
GridView1.RenderControl(hw);
MailMessage mail = new MailMessage();
mail.IsBodyHtml = true;
mail.To.Add(new MailAddress("rafi045@gmail.com"));
mail.Subject = "Sales Report";
System.Text.Encoding Enc = System.Text.Encoding.ASCII;
byte[] mBArray = Enc.GetBytes(stw.ToString());
System.IO.MemoryStream mAtt = new System.IO.MemoryStream(mBArray, false);
mail.Attachments.Add(new Attachment(mAtt, "sales.xls"));
mail.Body = "Order Form" + txtname.Text + "," + txtemail.Text + "," + txtaddress.Text;
SmtpClient smtp = new SmtpClient();
mail.From = new MailAddress("rafi045@gmail.com", "Your Name");
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(@"rafi045@gmail.com", "pwd");
smtp.EnableSsl = true;
smtp.Send(mail);
lblerror.Text = "your order Sent to admin panel. We will contact you soon";
}
}
catch (Exception ex)
{
lblerror.Text = ex.Message;
}
}