导出Gridview Itemplate数据到Excel [英] Export Gridview Itemplate data to Excel
问题描述
嗨
我有一个带有itemtemplates的gridview。当我想将网格数据导出为excel& sendas附件邮件我看不到Excel工作表中的数据。
下面是我的C#代码,按下按钮点击。
在此先感谢
StringBuilder sb = new StringBuilder();
System.IO.StringWriter stringWrite = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
this.gvSample .RenderControl(htmlWrite);
byte [] buffer = new byte [sb.Length];
ASCIIEncoding enc = new ASCIIEncoding();
buffer = enc.GetBytes(sb.ToString());
MemoryStream memorystream = new MemoryStream(buffer);
string body = sb.ToString();
MailMessage msg = new MailMessage(sample@gmail.com,sample@gmail.com,,body);
msg.Subject =email test ;
msg.IsBodyHtml = true;
msg.Attachments.Add(new Attachment(memorystream,Report.xls));
SmtpClient客户端=新的SmtpClient(sSMTPServer,Convert.ToInt32(iSMTPPort));
client.EnableSsl = true;
client.Credentials =新的System.Net.NetworkCredential(sSMTPUser,sSMTPPassword);
client.Send(msg);
我还添加了以下代码。
Hi
I have a gridview with itemtemplates . When i want to export the grid data as excel & sendas a attachment to the mail i see no data in the excel sheet .
Below is my C# Code which is wriiten in button Click .
Thanks in Advance
StringBuilder sb = new StringBuilder();
System.IO.StringWriter stringWrite = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
this.gvSample.RenderControl(htmlWrite);
byte[] buffer = new byte[sb.Length];
ASCIIEncoding enc = new ASCIIEncoding();
buffer = enc.GetBytes(sb.ToString());
MemoryStream memorystream = new MemoryStream(buffer);
string body = sb.ToString();
MailMessage msg = new MailMessage("sample@gmail.com", "sample@gmail.com", "", body);
msg.Subject = "email test";
msg.IsBodyHtml = true;
msg.Attachments.Add(new Attachment(memorystream, "Report.xls"));
SmtpClient client = new SmtpClient(sSMTPServer, Convert.ToInt32(iSMTPPort));
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential(sSMTPUser, sSMTPPassword);
client.Send(msg);
I also added the following code .
public override void VerifyRenderingInServerForm(Control control)
{
}
推荐答案
CodeProject常见问题系列1 :ASP.NET GridView [ ^ ]可能会有所帮助。
CodeProject Frequently Asked Questions Series 1: The ASP.NET GridView[^] might help.
这篇关于导出Gridview Itemplate数据到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!