简体   繁体   English

如何将GridView导出到Excel? sharepoint2013

[英]how to export GridView To Excel? sharepoint2013

I have the following code: 我有以下代码:

this is my button: <asp:Button ID="Button1" runat="server" Text="Export" /> 这是我的按钮: <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();
    }

but when I click the button only page is being refreshed and nothing else happens, Please help me 但是,当我单击该按钮时,仅页面正在刷新,并且没有其他任何反应,请帮助我

I am not able to use this method: 我无法使用此方法:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM