[英]how to export GridView To Excel? sharepoint2013
我有以下代碼:
這是我的按鈕: <asp:Button ID="Button1" runat="server" Text="Export" />
protected void Button1_Click(object sender, EventArgs e)
{
string attachment = string.Empty;
attachment = "attachment; filename=ReportName" + ".xls"; //Setting the attachment name.
HttpContext.Current.Response.ClearContent();//clears all content output from the buffer stream.
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType ="application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
htw.WriteLine("<center><b><u><font size='5'> " + attachment + " </font></u></b></center>");//will be displayed in excel as a heading.
GridView1.Parent.Controls.Add(frm);
frm.Controls.Add(GridView1);
frm.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
但是,當我單擊該按鈕時,僅頁面正在刷新,並且沒有其他任何反應,請幫助我
我無法使用此方法:
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control)
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;
filename=FileName.xls");
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.