2015-06-05 51 views
5

使用此代碼從gridview發送附帶excel附件的郵件時,它對於郵件部分可以正常工作,但excel附件始終爲空白我已經調試了代碼並確保數據源的Gridview將所需的數據傳遞給gridview,看起來im沒有渲染gridview的excel是正確的。整個過程在每個循環取決於foreach中的元素數量。在郵件中附加excel時檢索空白Excel

我錯過了一些代碼?

protected void MailButton_Click(object sender, EventArgs e) 
{ 
    List<FOAM> foamList = new List<FOAM>(servs.GetAreaList()); 
    foreach (FOAM foam in foamList) 
    { 
     Session["Area"] = foam.ItemAreaCode; 
     Session["Principal"] = foam.PrincipalAmount; 
     Session["Accountability"] = foam.AccountableAmount; 
     Session["Count"] = foam.ItemCount; 

     foamdetails.ItemAreaCode = foam.ItemAreaCode; 
     FOAMTemplateGridview.DataSource = servs.GetFoamAsOfUnsettledforAOM(foamdetails); 
     FOAMTemplateGridview.DataBind(); 

     StringWriter _writer = new StringWriter(); 
     HttpContext.Current.Server.Execute("AreaManagersMail.aspx", _writer); 

     StringWriter stw = new StringWriter(); 
     HtmlTextWriter hw = new HtmlTextWriter(stw); 
     FOAMTemplateGridview.RenderControl(hw); 

     MailMessage newMail = new MailMessage(); 
     newMail.Priority = MailPriority.High; 
     newMail.To.Add("[email protected]"); 
     newMail.Subject = "Unsettled FOAM As of " + DateTime.Today.ToString("MMMM dd, yyyy") + "-A" + foam.ItemAreaCode; 

     System.Text.Encoding Enc = System.Text.Encoding.ASCII; 
     byte[] mBArray = Enc.GetBytes(stw.ToString()); 
     System.IO.MemoryStream mAtt = new System.IO.MemoryStream(mBArray, false); 

     newMail.Attachments.Add(new Attachment(mAtt, "test.xls")); 
     newMail.Body = _writer.ToString(); 
     newMail.From = new MailAddress("[email protected]"); 
     newMail.IsBodyHtml = true; 
     SmtpClient SmtpSender = new SmtpClient(); 
     SmtpSender.Port = 25; 
     SmtpSender.Host = "MailHost"; 
     SmtpSender.Send(newMail); 

     newMail.Dispose(); 
    } 


} 
+0

嘗試調試並檢查您是否正在調試器中獲取數據 – BNN

+0

@nadeem是的,我試過並確定我確實從gridview的數據源獲取數據,但在檢查mBarray時它檢索到零維。 – user2705620

+0

@ User6675636b20796f7521從哪裏得到哪些數據將寫入excel? – Mairaj

回答

1

那麼找到自己的答案,

,而不是直接呈現在GridView我在表中通過在GridView的值,則該表格是我用於渲染的表格,

樣本如下:

Table table = new Table();  
    table.Rows.AddAt(3, FOAMTemplateGridview.HeaderRow); 

       foreach (GridViewRow row in FOAMTemplateGridview.Rows) 
       { 
        int index = 0; 
        table.Rows.Add(row); 
        foreach (TableCell tc in row.Cells) 
        { 
         switch (index) 
         { 

          case 0: tc.HorizontalAlign = HorizontalAlign.Left; 
           break; 
          case 1: tc.HorizontalAlign = HorizontalAlign.Right; 
           break; 
          case 2: tc.HorizontalAlign = HorizontalAlign.Right; 
           break; 
          case 3: tc.HorizontalAlign = HorizontalAlign.Left; 
           break; 
          case 4: tc.HorizontalAlign = HorizontalAlign.Right; 
           break; 
          case 5: tc.HorizontalAlign = HorizontalAlign.Left; 
           break; 
          case 6: tc.HorizontalAlign = HorizontalAlign.Left; 
           break; 
          case 7: tc.HorizontalAlign = HorizontalAlign.Right; 
           break; 
          case 8: tc.HorizontalAlign = HorizontalAlign.Left; 
           break; 
          case 9: tc.HorizontalAlign = HorizontalAlign.Right; 
           break; 
          case 10: tc.HorizontalAlign = HorizontalAlign.Right; 
           break; 
          case 11: tc.HorizontalAlign = HorizontalAlign.Right; 
           break; 
          case 12: tc.HorizontalAlign = HorizontalAlign.Right; 
           break; 
          case 13: tc.HorizontalAlign = HorizontalAlign.Right; 
           break; 
          case 14: tc.HorizontalAlign = HorizontalAlign.Left; 
           break; 
          case 15: tc.HorizontalAlign = HorizontalAlign.Left; 
           break; 
          case 16: tc.HorizontalAlign = HorizontalAlign.Left; 
           break; 
         } 
         tc.Attributes.Add("class", "text"); 
         tc.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid; 
         tc.BorderColor = Color.Black; 
         tc.Font.Name = "Arial"; 
         tc.Font.Size = 10; 
         index++; 
        } 

       } 
      StringWriter stw = new StringWriter(); 
      HtmlTextWriter hw = new HtmlTextWriter(stw); 
      hw.WriteLine(@"<style>.text { mso-number-format:\@; } </style>"); 
      table.RenderControl(hw); 

      MailMessage newMail = new MailMessage(); 
      newMail.Priority = MailPriority.High; 
      newMail.To.Add(foam.AomMail); 
      newMail.Subject = "Subject"; 
      System.Text.Encoding Enc = System.Text.Encoding.ASCII; 
      byte[] mBArray = Enc.GetBytes(stw.ToString()); 
      System.IO.MemoryStream mAtt = new System.IO.MemoryStream(mBArray, false); 
      newMail.Attachments.Add(new Attachment(mAtt, "sales.xls")); 

然後,你去我可以附加正確的文件發送郵件,而不是空白。

1

你得到空白Excel,因爲你正在從stw其定義bytes,但它從來沒有得到一個值。

stw只是沒有價值,這就是爲什麼你在mBArray得到0 bytes array一個新的對象。

而且比你指定的mAtt = new System.IO.MemoryStream(mBArray, false);。所以mBArray爲空,所以mAtt,你已經創建了mAtt這是空的文件,這就是爲什麼你在電子郵件中獲得空文件。我猜你需要改變這一行

byte[] mBArray = Enc.GetBytes(stw.ToString()); 

byte[] mBArray = Enc.GetBytes(_writer.ToString());