How to save a CSV File on Client (ASP.NET C#)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • minhtran
    New Member
    • Feb 2008
    • 28

    How to save a CSV File on Client (ASP.NET C#)

    HI All
    I have a project as to load data in Gridview from Client, then let Client to save a CSV file (filename.csv) on the client side as wherever they want not on the web server. I have a code as below is to save direct to "root of server", If anyone has this experience please help me. Thank you very much for your time and help;
    Here is code

    //How to change this code line to save on Client wherever they want Please
    Code:
     StreamWriter sw = new StreamWriter(Server.MapPath("~/GridDataMINH.txt"), false);
    
      // First we will write the headers.
            int columns = GridView1.Columns.Count;
             int rows = GridView1.Rows.Count;
            int y;
            int count = 0;
            for (int x = 0; x < columns; x++)
            {
                count = 0;
                for (y = 0; y < rows; y++)
                {
                    if (GridView1.Rows[y].Cells[x].Text == "&nbsp;")
                    {
                        count++;
                        GridView1.Rows[y].Cells[x].Text = GridView1.Rows[y].Cells[x].Text.Replace("&nbsp;", "   ");
                    }
                    else
                    {
                        y = rows;
                    }
    
                    if (count == rows)
                    {
                        GridView1.Columns[x].Visible = false;
                    }
               }
            }
    
            int iColCount = GridView1.Columns.Count;
            for (int i = 0; i < iColCount; i++)
            {
                sw.Write(GridView1.Columns[i]);
                if (i < iColCount - 1)
                {
                    sw.Write(",");
                }
            }
            sw.Write(sw.NewLine);
            // Now write all the rows.
            int myrows = GridView1.Rows.Count;
            // foreach (DataRow dr in GridView2.Rows)
            for (int ty = 0; ty < myrows; ty++)
            {
                for (int i = 0; i < iColCount; i++)
                {
                    if (!Convert.IsDBNull(GridView1.Rows[ty].Cells[i].Text))
                    {
                        sw.Write(GridView1.Rows[ty].Cells[i].Text);
                    }
                    if (i < iColCount - 1)
                    {
                        sw.Write(",");
                    }
                }
                sw.Write(sw.NewLine);
    
            }
            sw.Close();
            GridView1.Visible = true;
           // // Response.End();
    Last edited by Curtis Rutland; Jul 2 '08, 12:55 AM. Reason: Added code tags -- Please use the # button
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    You need to understand that ASP.NET cannot write anything to the client machine. At all.

    You can, however, produce a .CSV on the server. Write it a directory reserved for these files, and then present that file as a download. You can delete the file later through code if you need to, or maybe a daily/weekly script.
    Code:
    Response.Clear();
    Response.ContentType = "text/csv"
    Response.AddHeader("Content-Disposition","attachment;filename=myfile.csv");
    Response.WriteFile("myfile.csv");
    Also:
    Please enclose your posted code in [code] tags (See How to Ask a Question).

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    Please use [code] tags in future.

    MODERATOR

    Comment

    Working...