如何在特定日期和特定时间将Excel文件发送给特定用户

时间:2013-01-26 15:19:58

标签: sql-server ado.net

我创建了一个应用程序,一个应用程序在特定时间和特定日期将一个文件发送给用户将数据导出到excel,发送功能正常的电子邮件,但问题是它无法执行特定的时间和特定的一天我自己尝试但是它不会工作

private void Form1_Load(object sender, EventArgs e)
{ 
    if ((DateTime.Now.DayOfWeek== DayOfWeek.Saturday) &&(DateTime.Now.Hour==19) &&(DateTime.Now.Minute==15))
    {
        exportsqldatatoexcle();
        emailattachment(); 
    }
}

public void exportsqldatatoexcle()
{
    string con1 = "Data Source=ADMIN\\SQLEXPRESS;Initial Catalog=PhysioCure; Integrated Security=true";
    SqlConnection connection = new SqlConnection(con1);
    connection.Open();
    sda = new SqlDataAdapter("select PationName,RegistrationDate,ContactNo,Age,Sex,Chief_Complain,Investigation_Result,PastHistoryAny,Physical_Examination,Medications,Prognosis,Electro_Therapy,Neuro_Rehabilitation,Ortho_Rehabilitation,Cardio_Pulmonery_Rehabilitation,Sports_Rehabilitation from Physio_cureTable order by RegistrationDate desc", con1);
    dt = new DataTable();
    sda.Fill(dt);

    DataColumnCollection dccollection = dt.Columns; 
    Microsoft.Office.Interop.Excel.ApplicationClass excelapp = new Microsoft.Office.Interop.Excel.ApplicationClass();
    excelapp.Application.Workbooks.Add(Type.Missing); 

    for (int i = 1; i < dt.Rows.Count + 1; i++)
    {
        for (int j = 1; j < dt.Columns.Count + 1; j++)
        {
            if (i == 1)
            {
                excelapp.Cells[i, j] = dccollection[j - 1].ToString();
            }
            else
            {
                excelapp.Cells[i, j] = dt.Rows[i - 1][j - 1].ToString();
            }
        }
    }

    excelapp.ActiveWorkbook.SaveCopyAs("E:\\Winform n console\\PhysioCure\\PhysioCure\\Patient_Details.xls");
    excelapp.ActiveWorkbook.Saved = true;
    object m = Type.Missing;
    excelapp.ActiveWorkbook.Close(m, m, m);
    excelapp.Quit();
    connection.Close();
    MessageBox.Show("Detail file successfully create!");
}

private void emailattachment()
{
    //try
    //{
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
        mail.From = new MailAddress("my email id");
        mail.To.Add("my friend email id");
        mail.Subject = "Sending you Patient_Details File - 1";
        mail.Body = "Please Check Mail With Attachment dear..... ";

        System.Net.Mail.Attachment attachment;
        attachment = new Attachment("E:\\Winform n console\\PhysioCure\\PhysioCure\\Patient_Details.xls");
        mail.Attachments.Add(attachment);

        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("my email id", "password");
        SmtpServer.EnableSsl = true;

        SmtpServer.Send(mail);

        MessageBox.Show("Details File send on your mail please check your mail");
    //}
    /*catch (Exception ex)
    {
        MessageBox.Show("Mail cannot send please contact developer!!");
    }*/ 
}

1 个答案:

答案 0 :(得分:0)

如果您可以封装进程以通过命令行应用程序中的电子邮件发送文件,则可以使用Windows任务计划程序安排它。

我推荐使用SSIS和SQL Agent,或SSRS,但由于看起来您使用的是SQL Express,因此可能没有这些选项。