I'm using this button to create the Excel based on a DataGridView
:
private void exportbutton_Click(object sender, EventArgs e){
if (mesakemGrid.Rows.Count > 0)
{
Excel._Application XcelApp = new Excel.Application();
XcelApp.Application.Workbooks.Add(Type.Missing);
for (int i = 1; i < mesakemGrid.Columns.Count + 1; i++)
{
XcelApp.Cells[1, i] = mesakemGrid.Columns[i - 1].HeaderText;
Excel.Borders border = XcelApp.Cells[1, i].Borders;
border.LineStyle = Excel.XlLineStyle.xlContinuous;
border.Weight = 2d;
}
for (int i = 0; i < mesakemGrid.Rows.Count; i++)
{
for (int j = 0; j < mesakemGrid.Columns.Count; j++)
{
Excel.Borders border = XcelApp.Cells[i + 2, j + 1].Borders;
border.LineStyle = Excel.XlLineStyle.xlContinuous;
border.Weight = 2d;
XcelApp.Cells[i + 2, j + 1] = mesakemGrid.Rows[i].Cells[j].Value.ToString();
}
}
XcelApp.Columns.AutoFit();
XcelApp.Visible = true;
}
}
I want to send this Excel file to an Email address. Is it possible? I saw in some guide this method, But it requires an Attachment, and i don't know if it's possible to convert Excel._Application to Attachment.
public void send(System.Net.Mail.Attachment attachment)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("SMTP_INFO");
mail.From = new MailAddress("YOUR_EMAIL@XXX.com");
mail.To.Add("TO_ADDRESS@XXX.COM");
mail.Subject = "THIS IS THE SUBJECT";
mail.Body = "THIS IS THE BODY";
attachment = new System.Net.Mail.Attachment(@"C:\EXCEL_FILE.XLS");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("USERNAME", "PASSWORD");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}