错误:无法加载文件或程序集'DocumentFormat.OpenXml,

时间:2016-08-17 08:19:44

标签: c# mysql email

我收到的错误是VS2015

  

其他信息:无法加载文件或程序集   'DocumentFormat.OpenXml,Version = 2.5.5631.0,Culture = neutral,   PublicKeyToken = 31bf3856ad364e35'或其依赖项之一。该   找到程序集的清单定义与程序集不匹配   参考。 (HRESULT异常:0x80131040)

我正在尝试使用c#

中的数据库表附件发送邮件

这里是我的代码ID

using ClosedXML.Excel;
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.IO;
using System.Net.Mail;
using System.Windows.Forms;

namespace Email
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        String MyConString = "SERVER=10.0.30.125;" + "DATABASE=test;" + "UID=root;" + "PASSWORD=asterisk;" + "Convert Zero Datetime = True";        
        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = GetData();
            dt.TableName = "data";
            using (XLWorkbook wb = new XLWorkbook())
            {
                wb.Worksheets.Add(dt);
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    wb.SaveAs(memoryStream);
                    byte[] bytes = memoryStream.ToArray();
                    memoryStream.Close();
                    using (MailMessage Msg = new MailMessage())
                    {
                        Msg.From = new MailAddress("my@mail.com");
                        Msg.To.Add("to@mail.com");
                        Msg.Subject = "DATA";
                        Msg.Body = "Excel Attachment";
                        Msg.Attachments.Add(new Attachment(new MemoryStream(bytes), "DATA.xlsx"));
                        Msg.IsBodyHtml = true;
                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = "smtp.gmail.com";
                        smtp.EnableSsl = true;
                        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
                        credentials.UserName = "my@mail.com";
                        credentials.Password = "password";
                        smtp.UseDefaultCredentials = true;
                        smtp.Credentials = credentials;
                        smtp.Port = 587;
                        smtp.Send(Msg);
                        MessageBox.Show("MAIL SENT SUCCESSFULLY TO " + txtTo.Text);
                        txtTo.Text = "";
                    }
                }
            }
        }
        private DataTable GetData()
        {
            using (MySqlConnection conn = new MySqlConnection(MyConString))
            {
                using (MySqlCommand cmd = new MySqlCommand("SELECT * from table;", conn))
                using (MySqlDataAdapter sda = new MySqlDataAdapter())
                {
                    cmd.Connection = conn;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }
    }
}

我在AddReference

中添加它
ClosedXml.dll
DocumentFormat.OpenXml.dll
MySql.Data.dll

帮助解决我的问题。为什么我得到这个错误?

1 个答案:

答案 0 :(得分:2)

下载并安装DocumentFormat.OpenXml版本2.5 https://www.microsoft.com/en-us/download/details.aspx?id=30425