定时器定时触发报表,导出Excel,PDF, 以附件形式发送
一、定时器示例代码(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发送
}
}
{
// 在应用程序启动时运行的代码
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,PDF
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 " , null , out 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);
}
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 " , null , out 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( "XXX @XXX.CN " ); // 设置发件人的email地址
Mess.To.Add("YYY @YYY.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); // 发送邮件
}
{
MailMessage Mess = new MailMessage(); // 提供用于构造电子邮件的属性和方法
Mess.From = new MailAddress( "XXX @XXX.CN " ); // 设置发件人的email地址
Mess.To.Add("YYY @YYY.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); // 发送邮件
}
Other:
using
System.Net.Mail;
private void SendMail(ReportViewer reportViewer)
{
Warning[] warnings;
string [] streamids;
string mimeType;
string encoding;
string extension;
byte [] bytes = reportViewer.LocalReport.Render
( " Excel " , null , out mimeType, out encoding, out extension, out
streamids, out warnings);
MemoryStream memoryStream = new MemoryStream(bytes);
memoryStream.Seek( 0 , SeekOrigin.Begin);
MailMessage message = new MailMessage();
Attachment attachment = new Attachment(memoryStream, " BusinessReport.xls " );
message.Attachments.Add(attachment);
message.From = new MailAddress( " santosh.poojari@gmail.com " );
message.To.Add( " santosh.poojari@gmail.com " );
message.CC.Add( " santosh.poojari@gmail.com " );
message.Subject = " Business Report " ;
message.IsBodyHtml = true ;
message.Body = " Please find Attached Report herewith. "
if (ConfigurationManager.AppSettings[ " SendMail " ].ToString() == " Y " )
{
SmtpClient smtp = new SmtpClient( " SMTP Server Name " );
smtp.Send(message);
}
else
{
// This is for testing.
SmtpClient smtp = new SmtpClient();
smtp.Send(message);
}
memoryStream.Close();
memoryStream.Dispose();
}
private void SendMail(ReportViewer reportViewer)
{
Warning[] warnings;
string [] streamids;
string mimeType;
string encoding;
string extension;
byte [] bytes = reportViewer.LocalReport.Render
( " Excel " , null , out mimeType, out encoding, out extension, out
streamids, out warnings);
MemoryStream memoryStream = new MemoryStream(bytes);
memoryStream.Seek( 0 , SeekOrigin.Begin);
MailMessage message = new MailMessage();
Attachment attachment = new Attachment(memoryStream, " BusinessReport.xls " );
message.Attachments.Add(attachment);
message.From = new MailAddress( " santosh.poojari@gmail.com " );
message.To.Add( " santosh.poojari@gmail.com " );
message.CC.Add( " santosh.poojari@gmail.com " );
message.Subject = " Business Report " ;
message.IsBodyHtml = true ;
message.Body = " Please find Attached Report herewith. "
if (ConfigurationManager.AppSettings[ " SendMail " ].ToString() == " Y " )
{
SmtpClient smtp = new SmtpClient( " SMTP Server Name " );
smtp.Send(message);
}
else
{
// This is for testing.
SmtpClient smtp = new SmtpClient();
smtp.Send(message);
}
memoryStream.Close();
memoryStream.Dispose();
}
<
rsweb:ReportViewer
ID
="ReportViewer1"
runat
="server"
Font-Names ="Verdana" Font-Size ="8pt"
>
< LocalReport ReportPath ="Report.rdlc" >
< DataSources >
< rsweb:ReportDataSource / >
</ DataSources >
</ LocalReport >
</ rsweb:ReportViewer >
Font-Names ="Verdana" Font-Size ="8pt"
>
< LocalReport ReportPath ="Report.rdlc" >
< DataSources >
< rsweb:ReportDataSource / >
</ DataSources >
</ LocalReport >
</ rsweb:ReportViewer >
<
system
.net
>
< mailSettings >
< smtp deliveryMethod ="SpecifiedPickupDirectory" >
< specifiedPickupDirectory pickupDirectoryLocation ="C:\Test\" / >
</ smtp >
</ mailSettings >
</ system.net >
< mailSettings >
< smtp deliveryMethod ="SpecifiedPickupDirectory" >
< specifiedPickupDirectory pickupDirectoryLocation ="C:\Test\" / >
</ smtp >
</ mailSettings >
</ system.net >