Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string path = "D:\\SYNC\\Reports.xls";
        FileInfo filexist = new FileInfo(path);
        if (filexist.Exists)
        {
            File.Delete(path);
        }
        else if (!Directory.Exists("D:\\SYNC\\"))
        {
            Directory.CreateDirectory("D:\\SYNC\\");
        }

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                bid.MainCompCode = Session["Comp"].ToString();
                bid.MainLocCode = Session["Loc"].ToString();
                bid.UserName = Session["Userid"].ToString();
                bid.Procedure = "gridfetch";
                bid.ScreenName = Request.QueryString["proc"].ToString();
                bid.Date4Filter = DateTime.Today.AddDays(-1).Date;
                adddatapage = Request.QueryString["page"].ToString();
                bid.CompCode = "";
                DataTable dt = new DataTable();
                dt = cn.fillgrid(bid);
                GridView gr = new GridView();
                gr.DataSource = dt;
                //gr.Columns[0].Visible = false;
                //gr.Columns[1].Visible = false;

                gr.DataBind();


                //Change the Header Row back to white color
               
                //Applying stlye to gridview header cells
                for (int i = 0; i < gr.HeaderRow.Cells.Count; i++)
                {
                    if (i != 0 && i != 1)
                    {
                        gr.HeaderRow.Style.Add("background-color", "#FFFFFF");
                        gr.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
                    }
                }
                int j = 1;
                //This loop is used to apply stlye to cells based on particular row
                foreach (GridViewRow gvrow in gr.Rows)
                {
                    //gvrow.BackColor = Color.White;
                    if (j <= gr.Rows.Count)
                    {
                        if (j % 2 != 0)
                        {
                            for (int k = 0; k < gvrow.Cells.Count; k++)
                            {
                                if (k != 0 && k != 1)
                                {
                                    gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                                }
                            }
                        }
                    }
                    j++;
                }
                FileStream fs = new FileStream("D:\\SYNC\\Reports.xls",FileMode.OpenOrCreate,FileAccess.ReadWrite);

                StreamWriter writer = new StreamWriter(fs);
                gvCountry.RenderControl(hw);
                writer.WriteLine(sw.ToString());

                writer.Close();
            }
        }
        msg("File downloaded at D:\\SYNC\\Reports.xls");
        bindgrid();


My application running on server ip, when i am downloading data, it is checking server local folder to download,instead of client machine folder for download....
Posted
Updated 29-Mar-13 21:11pm
v2

For security reason you cannot access client file system from your web application(from browser). Only Internet Explorer can do it with its ActiveX component. Besides that You can access it with Silgerlight/Adobe flash based application which run in a sandbox with browser plugin. What you can do you write your excel file stream to http response object with set some property. Then browse ask the client where it need to store. Client can client his file system path and file will be saved there.
 
Share this answer
 
Comments
rawatsanjay08 30-Mar-13 2:52am    
thanks for the reply but problem is that response will download the file at download folder and i want to upload the downloaded file from clientside. can we access the local pc download folder to upload the data..
S. M. Ahasan Habib 30-Mar-13 4:41am    
No way you can access client file system. What it you can do is, then it will automatically saved download folder without asking client to give location.
C#
writer.Close();
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=" + PDF_Name + ".xls");
            Response.ContentType = "application/vnd.ms-excel";
            Response.BinaryWrite(msReport.ToArray());
            Response.End();

Write this code after writer.close()
have a look at this line
Response.BinaryWrite(msReport.ToArray());
msReport is object of memory stream, try to get your excel into object of memory steram and then add above code which will download excel on client machine.
 
Share this answer
 
v2

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