Export Grid view to excel and save excel file to folder

前端 未结 7 743
Happy的楠姐
Happy的楠姐 2020-12-16 06:05

I want to save excel file which export data of grid view. I have written code to export gridview data to excel but I don\'t know how to save exported file.

Following

7条回答
  •  时光取名叫无心
    2020-12-16 06:43

    this may help you//

    protected void exporttoexcel_Click(object sender, EventArgs e)
    {
        Response.Clear();
    
        Response.AddHeader("content-disposition", "attachment;filename=" attachment" + ".xls");
    
        Response.Charset = "";
    
        // If you want the option to open the Excel file without saving than
    
        // comment out the line below
    
        // Response.Cache.SetCacheability(HttpCacheability.NoCache);
    
        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();
    
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    
        // Confirms that an HtmlForm control is rendered for the
        //specified ASP.NET server control at run time.
    
    }
    

提交回复
热议问题