使用此代碼從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();
}
}
嘗試調試並檢查您是否正在調試器中獲取數據 – BNN
@nadeem是的,我試過並確定我確實從gridview的數據源獲取數據,但在檢查mBarray時它檢索到零維。 – user2705620
@ User6675636b20796f7521從哪裏得到哪些數據將寫入excel? – Mairaj