asp.net中rdlc报表定时自动推送服务(定时器、email)

本文介绍了一个简单的定时任务实现方案,通过定时器触发任务生成Excel文件,并利用邮件发送功能将文件发送给指定用户。该方案适用于快速开发需求,可在短时间内完成基本功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基本想法是采用定时器定时触发某个任务,生成excel文件,并利用email发送出去。开发时间只有2天不到,故以实现功能为第一目标。

一、定时器示例代码(Global.asax文件中)

void Application_Start(object sender, EventArgs e) 
{
    
// 在应用程序启动时运行的代码
    System.Timers.Timer timer = new System.Timers.Timer(60000); // 1 * 60 * 1000毫秒的时间间隔
    timer.AutoReset = true//设置是执行一次(false)还是一直执行(true); 
    timer.Enabled = true//到达时间的时候执行事件; 

    timer.Elapsed 
+= new System.Timers.ElapsedEventHandler(timer_Elapsed);
}


void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    
// 每周一早晨8:30左右,用email给固定收益部报发送周报表
    System.DateTime currentTime = DateTime.Now;
    
if (currentTime.DayOfWeek == DayOfWeek.Monday &&
        currentTime.Hour 
== 8 &&
        currentTime.Minute 
>= 30 && currentTime.Minute <= 40)
    
{
        
// 生成excel文件,并用email发送
    }

}


二、使用ReportViewer,编写代码导出到excel文件
Warning[] Warnings;
string strMimeType;
string strEncoding;
string strFileNameExtension;
string[] strStreamIds;

FundStat fundStat 
= new FundStat(); // 基金统计
ReportViewer ReportViewer1 = new ReportViewer();
ReportDataSource datasource 
= null;
DataSet ds 
= null;

ReportViewer1.LocalReport.ReportPath 
= "c:Stat und_stat.rdlc";
ReportViewer1.LocalReport.DataSources.Clear();

ds 
= fundStat.GetHolderStruct(inFundCode, inEndDate);
datasource 
= new ReportDataSource("DSFundStat_HolderStruct", ds.Tables[0]);
ReportViewer1.LocalReport.DataSources.Add(datasource);

ReportViewer1.LocalReport.Refresh();

// 写出到excel文件
byte[] bytes = ReportViewer1.LocalReport.Render("Excel"nullout strMimeType, out strEncoding, out strFileNameExtension, out strStreamIds, out Warnings);
using (System.IO.FileStream fs = new System.IO.FileStream(arrFilePath[i], System.IO.FileMode.Create))
{
    fs.Write(bytes, 
0, bytes.Length);
}


三、Email发送示例代码(将磁盘上的excel文件作为附件发送到邮件列表)
private void SendMail()
{
    MailMessage Mess 
= new MailMessage();            //提供用于构造电子邮件的属性和方法
    Mess.From = new MailAddress("JSDC@JSFUND.CN");    //设置发件人的email地址
    Mess.To.Add("luocm@jsfund.cn");                    //设置收件人的email地址

    Mess.Subject 
= "统计数据-20080414";                //设置电子邮件的主题行
    
//Mess.IsBodyHtml = true;                        //设置电子邮件正文的内容类型
    Mess.Body = "固定收益部";                        //设置电子邮件正文的内容
    Mess.Priority = MailPriority.High;                //设置电邮的优先级。High为高,Low为低 优先级,Normal邮件具有普通优先级
    
    Attachment att 
= new Attachment("d:/fundstat_20080414.xls");
    Mess.Attachments.Add(att);                      
// 添加附件文件

    SmtpClient smtp 
= new SmtpClient("mail.jsfund.cn");
    smtp.Send(Mess);                                
//发送邮件
}


总结:一、定时器模块程序结构上可再梳理一下;二、任务调度模块可引入xml机制,将任务定制标准化,增强灵活配置能力和程序的扩展性(当然开发规模会大大增加)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值