【Vegas原创】ASP.NET读取Excel,并以邮件正文方式和附件方式发送实例

summary.aspx.cs

using System.Web.Mail;
using System.Data.OleDb; 
using System.Text; 
using System.Configuration; 
using System.IO;
using Excel;

 

public static string strConn 
        { 
            
get 
            { 
                StringBuilder builder1 
= new StringBuilder(); 
                builder1.Append(
"Provider = Microsoft.Jet.OLEDB.4.0"); 
                builder1.Append(
""); 
                builder1.Append(
"Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1\""); 
                builder1.Append(
"");  
                builder1.Append(
"Data Source = "); 
                builder1.Append(HttpContext.Current.Server.MapPath(
".")); 
                builder1.Append(
@"\"); 
                builder1.Append(ConfigurationSettings.AppSettings[
"DataPath4"]); 
                
return builder1.ToString(); 
            } 
        } 

 

        private void Page_Load(object sender, System.EventArgs e)
        {
            
// 在此处放置用户代码以初始化页面
            
// 在此处放置用户代码以初始化页面
            
//H
            OleDbConnection conn3 = new OleDbConnection(strConn); 
            OleDbCommand com3 
=new OleDbCommand("SELECT * FROM [Sheet1$A3:B3]",conn3); 
            OleDbDataAdapter da3
=new OleDbDataAdapter(com3); 
            DataSet ds3
=new DataSet(); 
            da3.Fill(ds3); 
            DataGrid3.DataSource
=ds3.Tables[0].DefaultView; 
            DataGrid3.DataBind();
            
//A
            OleDbConnection conn = new OleDbConnection(strConn); 
            OleDbCommand com 
=new OleDbCommand("SELECT * FROM [Sheet1$A5:K10]",conn); 
            OleDbDataAdapter da
=new OleDbDataAdapter(com); 
            DataSet ds
=new DataSet(); 
            da.Fill(ds); 
            DataGrid1.DataSource
=ds.Tables[0].DefaultView; 
            DataGrid1.DataBind(); 
            
// B
            OleDbConnection conn2 = new OleDbConnection(strConn); 
            OleDbCommand com2 
=new OleDbCommand("SELECT * FROM [Sheet1$A12:M15]",conn2); 
            OleDbDataAdapter da2
=new OleDbDataAdapter(com2); 
            DataSet ds2
=new DataSet(); 
            da2.Fill(ds2); 
            DataGrid2.DataSource
=ds2.Tables[0].DefaultView; 
            DataGrid2.DataBind();
            
//Mail
            MailMessage mailObj=new MailMessage();
            StringWriter sWriter
=new StringWriter(); 
            HtmlTextWriter htmlWriter
=new HtmlTextWriter(sWriter); //以这个流类作为参数来进行传输数据。
            DataGrid1.RenderControl(htmlWriter);    
            StringWriter s2Writer
=new StringWriter();       //DataGrid2
            HtmlTextWriter htmlWriter2=new HtmlTextWriter(s2Writer);
            DataGrid2.RenderControl(htmlWriter2);
            StringWriter s3Writer
=new StringWriter();   //DataGrid3
            HtmlTextWriter htmlWriter3=new HtmlTextWriter(s3Writer);
            DataGrid3.RenderControl(htmlWriter3);
            mailObj.From
="**"//发信人的邮件地址
           mailObj.To="**";     //收信人的邮件地址
            mailObj.Bcc="";  //收取密件副件人的地址
            mailObj.Subject="【Summary Daily Report】";  //邮件的主题
            mailObj.Body=mailObj.Body+"            <table>" ;
            mailObj.Body
=mailObj.Body+"                <tr>";
            mailObj.Body
=mailObj.Body+"                    <font size='8'  face='Broadway BT'>Summary</font>";
            mailObj.Body
=mailObj.Body+"                </tr>";
            mailObj.Body
=mailObj.Body+"                <tr>";
            mailObj.Body
=mailObj.Body+"    <font face='Xirod' ><u>&nbsp;&nbsp;&nbsp;";
            mailObj.Body
=mailObj.Body+"DT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            mailObj.Body
=mailObj.Body+"</u></font>";
            mailObj.Body
=mailObj.Body+"                </tr>";
            mailObj.Body
=mailObj.Body+"            </table>";
            mailObj.Body
=mailObj.Body+s3Writer.ToString()+sWriter.ToString()+"&nbsp;&nbsp;"+s2Writer.ToString()+"";  //将流类的内容作为邮件的正文部分
            mailObj.BodyFormat=MailFormat.Html;  //格式化为html格式
            string attaches = @"D:\a\*.xls"//附件
            MailAttachment myAttachment = new MailAttachment(attaches); //这里以附件作为参数
            mailObj.Attachments.Add(myAttachment); 
         
            
try
            {   
                SmtpMail.SmtpServer 
= "**";
                SmtpMail.Send(mailObj); 
//调用SmtpMail类来发送邮件
            }
            
catch(Exception pp)
            {
                Response.Write(pp.Message);
            }
            Label1.Text
=DateTime.Now.ToString()+"发送成功!";

        }


web.config

  <identity impersonate="true" userName="" password=""/> 

 

<add key="DataPath4" value="*/*.xls" />

 

posted @ 2006-10-27 13:35  李济宏(Amadeus)  阅读(204)  评论(0编辑  收藏  举报