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 is my code to export gridview into excel :
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=MyFiles.xls");
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
gvFiles.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
                From your main display, select one or all chart(s) with the tag data you want to export. Navigate to the Action drop-down menu. From this drop-down menu, select Export Raw Data. The system automatically names the file and you see the download and an automatic save occurs.
You can do this:
private void ExportGridView()
{
    System.IO.StringWriter sw = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
    // Render grid view control.
    gvFiles.RenderControl(htw);
    // Write the rendered content to a file.
    string renderedGridView = sw.ToString();
    System.IO.File.WriteAllText(@"C:\Path\On\Server\ExportedFile.xlsx", renderedGridView);
}
                        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.
}
                        public partial class exportgridtoexcel : System.Web.UI.Page
{
    SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString.ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetData();
        }
    }
    public void GetData()
    {
        SqlDataAdapter sda = new SqlDataAdapter("select * from EmpData", con);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void btnDownload_Click(object sender, EventArgs e)
    {
        GetData();
        exporttoexcel("Report.xls", GridView1);
        GridView1 = null;
        GridView1.Dispose();
    }
    public void exporttoexcel(string filename,GridView gv)
    {
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment;filename=" + filename);
        Response.ContentType = "applicatio/excel";
        StringWriter sw = new StringWriter(); ;
        HtmlTextWriter htm=new HtmlTextWriter(sw);
        gv.RenderControl(htm);
        Response.Write(sw.ToString());
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With