从工作簿中获取Excel工作表并将其作为电子邮件附件发送

时间:2019-04-12 00:34:50

标签: c#

任何人都可以帮助我如何获取excel工作表并将其作为附件发送。电子邮件代码已经很好,我唯一的问题是获取工作表。

2 个答案:

答案 0 :(得分:0)

您可以使用EPPLUS来做到这一点。这是一个代码片段 从他们在github上的文档中。

   //Open the workbook (or create it if it doesn't exist)
    var fi=new FileInfo(@"c:\workbooks\myworkbook.xlsx")
    using (var p = new ExcelPackage(fi))
    {
       var ws=p.Workbook.Worksheets["MyWorksheetName"];

       //Save and close the package.
       p.Save();
    }

答案 1 :(得分:0)

    public void sendMail()
    {
        Console.WriteLine("Mail sending...");
        MailMessage ePosta = new MailMessage();
        ePosta.From = new MailAddress("From mail adress");
        ePosta.To.Add("To Mail Adress");
        ePosta.CC.Add("CCmail adress (optional)")

        ePosta.Attachments.Add(new Attachment(@"C:/xx/yy/abc.xlsx"));
        ePosta.Subject = "Subject here"
        ePosta.Body = "Body Message here"
        SmtpClient smtp = new SmtpClient();

        smtp.Credentials = new System.Net.NetworkCredential(From mail adress, From mail password);
        smtp.Port = 587;
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        object userState = ePosta;
        try
        {
            smtp.SendAsync(ePosta, (object)ePosta);
        }
        catch
        {
            MessageBox.Show("Mail sending error");
        }
        Console.WriteLine("Mail sent");