Click here to Skip to main content
13,559,182 members
Rate this:
Please Sign up or sign in to vote.
See more:

I am trying to export grid view data to excel file.I have done this successfully but the problem is Excel file is storing in solution itself.but i want to save that excel file in downloads can i solve this? Please help me anyone....

here is my code

 protected void btnExport_Click(object sender, EventArgs e)

        DataTable dt = new DataTable();
        dt = Session["data"] as DataTable;

        if (dt == null)
            throw new Exception("No Records to Export");
        string Path = "D:\\ImportExcelFromDatabase\\Karvyexcel_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + ".xls";

        //string Path = "D:\\Sujatha\\Karvy_27Nov2012\\Inventory_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + ".xls";
        FileInfo FI = new FileInfo(Path);
        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
        DataGrid DataGrd = new DataGrid();
        DataGrd.DataSource = dt;

        ////string directory = Path.Substring(0, Path.LastIndexOf("\\"));// GetDirectory(Path);
        ////if (!Directory.Exists(directory))
        ////    Directory.CreateDirectory(directory);

        //System.IO.StreamWriter vw = new System.IO.StreamWriter(Path, true);
        WriteAttachment(FI.Name, "application/", stringWriter.ToString());

    public static void WriteAttachment(string FileName, string FileType, string content)
        HttpResponse Response = System.Web.HttpContext.Current.Response;
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
        Response.ContentType = FileType;


private void BindCustomerData()
            string strSelect = "select * from CustomerData";
            DataSet ds = new DataSet();
            cnn.ConnectionString = connStr;
            // if the connection will be closed the below code poen the connection when the page will be loaded.
            if (cnn.State == ConnectionState.Closed)
            // here i am using the store procedure named tb_gallery_insert to insert the record inside the database.
            SqlDataAdapter da = new SqlDataAdapter(strSelect, cnn);
            if (ds.Tables[0].Rows.Count > 0)
                gvData.DataSource = ds.Tables[0];

                Session["data"] = ds.Tables[0];

                lblMessage.Text = "No Records Found";
                lblMessage.ForeColor = System.Drawing.Color.Red;
        catch (Exception ex)
            lblMessage.Text = ex.Message;
            lblMessage.ForeColor = System.Drawing.Color.Red;
Posted 28-Nov-12 0:11am
Updated 28-Nov-12 10:50am
Richard C Bishop 28-Nov-12 16:58pm
Change the file path location you are passing to your WriteAttachment method.
Please stop posting non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership.
Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

i have resolved the problem was due to update pannel
Not an answer. Please stop doing such things.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02-2016 | 2.8.180515.1 | Last Updated 29 Nov 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100