如何在特定日期和特定时间将Excel文件发送给特定用户 [英] How To send Excel file to particular user at specific day and specific time

查看:103
本文介绍了如何在特定日期和特定时间将Excel文件发送给特定用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个应用程序,一个应用程序将一个文件发送给用户在特定时间和特定日期将sql数据导出到excel,发送功能正常的电子邮件但问题是它无法执行特定时间和特定日期我粘贴在这里代码请帮帮我





 私人  void  Form1_Load( object  sender,EventArgs e)
{

if ((DateTime.Now.DayOfWeek == DayOfWeek.Saturday)&&(DateTime.Now.Hour == 19 )&&(DateTime.Now.Minute == 15 ))
{
exportsqldatatoexcle();

emailattachment();


}
}

< pre> public void exportsqldatatoexcle()
{
string con1 = 数据源= ADMIN \\SQLEXPRESS;初始目录= PhysioCure;集成安全性=真;
SqlConnection connection = new SqlConnection(con1);
connection.Open();
sda = new SqlDataAdapter( 选择PationName ,RegistrationDate,ContactNo,年龄,性别,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( 详细文件成功创建!);

}

private void emailattachment()
{
// 尝试
// {
MailMessage mail = new MailMessage() ;
SmtpClient SmtpServer = new SmtpClient( smtp .gmail.com);
mail.From = new MailAddress( 我的电子邮件ID);
mail.To.Add( 我的朋友电子邮件ID);
mail.Subject = 向您发送Patient_Details文件 - 1;
mail.Body = 请检查邮件附件亲爱的.....;

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

SmtpServer.Port = 587 ;
SmtpServer.Credentials = new System.Net.NetworkCredential( 我的电子邮件ID password);
SmtpServer.EnableSsl = true ;

SmtpServer.Send(mail);

MessageBox.Show( 详细信息文件发送邮件请检查邮件);
// }
/ * catch(Exception ex)
{
MessageBox.Show(邮件无法发送请联系开发者!!);

} * /



}

解决方案

< blockquote>花时间检查程序并使用Windows任务计划程序在所需的时间启动程序。


I am create one application that application one file send to user at specific time and specific day export sql data into excel ,sending email that functions working fine but problems is that it cannot execute particular time and particular day I am paste here my code please help me out


private void Form1_Load(object sender, EventArgs e)
        {

            if ((DateTime.Now.DayOfWeek== DayOfWeek.Saturday) &&(DateTime.Now.Hour==19) &&(DateTime.Now.Minute==15))
            {
                exportsqldatatoexcle();

                emailattachment();


            }
}

<pre>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!!");

            }*/


        }

解决方案

Take the time checking out of your program and use Windows Task Scheduler to start your program at the desired time.


这篇关于如何在特定日期和特定时间将Excel文件发送给特定用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆