Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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 29-Mar-13 20:05pm
Edited 29-Mar-13 21:11pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
rawatsanjay08 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 531
1 OriginalGriff 334
2 DamithSL 235
3 Maciej Los 170
4 Bruno Sprecher 130
0 DamithSL 95
1 OriginalGriff 65
2 Gopu Raj 30
3 Mahsa Hassankashi 25
4 Raul Iloc 25


Advertise | Privacy | Mobile
Web04 | 2.8.141030.1 | Last Updated 30 Mar 2013
Copyright © CodeProject, 1999-2014
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