0

I have C# code. Is it possible to export the DataGridView to an Excel file and send it by e-mail without saving the Excel file?

mail sending function with attachment and for that I need file location

 Attachment attachment = new Attachment(labelLocation.Text);
 mail.Attachments.Add(attachment);
Yael
  • 93
  • 7
  • Shoirt answer: probably ses. Lonjg answer depends on what you´ve tried and where you´re stuck. – MakePeaceGreatAgain Dec 03 '20 at 10:20
  • Have you tried anything yet? There are a lot of SO questions asking how to create a *real* Excel file without using Excel directly, or hacks like generating HTML with a fake extension. There are a lot of SO questions that show how to send emails with attachments – Panagiotis Kanavos Dec 03 '20 at 10:21
  • I have a function to export a table to a file and it saves the file on my computer. and I have a mail sending function with a Attachment and for that I need the file location, but I don't want to save the file on my computer – Yael Dec 03 '20 at 10:25
  • This question does exactly what you asked [EPPlus with MemoryStream as email attachment — file is empty](https://stackoverflow.com/questions/12847412/epplus-with-memorystream-as-email-attachment-file-is-empty). DataGridView can't be used to create Excel files. It's used to fake it though, which is extremely annoying to recipients. To create real Excel files, you need either the OpenXML SDK or libraries like EPPlus. The (probably duplicate) SO question sends the Excel file generated by EPPlus as an attachment – Panagiotis Kanavos Dec 03 '20 at 10:27
  • There are a *lot* of SO questions asking how to create real Excel files. All of the relevant libraries (EPPlus, ClosedXML, NPOI, Open XML SDK) work with streams, which means they can store to `MemoryStream` as well. There are a lot of SO questions asking how to send emails with attachments, create them in memory. Or which library to use since `SmtpClient` is obsolete (use MailKit) – Panagiotis Kanavos Dec 03 '20 at 10:29

0 Answers0