Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi............

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 folder.how can i solve this? Please help me anyone....

here is my code


C#
 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;
        DataGrd.DataBind();

        DataGrd.RenderControl(htmlWrite);
        ////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);
        stringWriter.ToString().Normalize();
        //vw.Write(stringWriter.ToString());
        //vw.Flush();
        //vw.Close();
        WriteAttachment(FI.Name, "application/vnd.ms-excel", stringWriter.ToString());

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

    }

private void BindCustomerData()
    {
        try
        {
            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)
            {
                cnn.Open();
            }
            // here i am using the store procedure named tb_gallery_insert to insert the record inside the database.
            SqlDataAdapter da = new SqlDataAdapter(strSelect, cnn);
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                gvData.DataSource = ds.Tables[0];
                gvData.DataBind();

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

            }
            else
            {
                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
Updated 28-Nov-12 10:50am
v2
Comments
Richard C Bishop 28-Nov-12 16:58pm    
Change the file path location you are passing to your WriteAttachment method.
Sergey Alexandrovich Kryukov 1-Jul-13 9:41am    
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.
—SA

1 solution

i have resolved the problem was due to update pannel
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Jul-13 0:44am    
Not an answer. Please stop doing such things.
—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900