Click here to Skip to main content
14,878,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai,


I want to export gridview values into csv by using c# .net web application.How can I export this.
Posted

Try this. It should work

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            string strValue = string.Empty;

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                for (int j = 0; j < GridView1.Rows[i].Cells.Count; j++)
                {
                    if (!string.IsNullOrEmpty(GridView1.Rows[i].Cells[j].Text.ToString()))
                    {
                        if (j > 0)
                            strValue = strValue + "," + GridView1.Rows[i].Cells[j].Text.ToString();
                        else
                        {
                            if (string.IsNullOrEmpty(strValue))
                                strValue = GridView1.Rows[i].Cells[j].Text.ToString();
                            else
                                strValue = strValue + Environment.NewLine + GridView1.Rows[i].Cells[j].Text.ToString();
                        }
                    }
                }
                // strValue = strValue + Environment.NewLine;
            }
            string strFile = @"C:\YourCSVFile.csv";

            if (File.Exists(strFile) && !string.IsNullOrEmpty(strValue))
            {
                File.WriteAllText(strFile, strValue);
            }
        }
   
v3
Comments
BlehBlahBah 11-Dec-12 4:18am
   
I tried this code but it doesn't create a csv file. I applied breakpoint and it turns out that the control doesn't even go into the strFile statement. Why is that?
CS2011 11-Dec-12 4:36am
   
Do you get any error message or any thing like that
CS2011 11-Dec-12 4:40am
   
Im case if you only have one row in your grid then
below loop will create problem and its a bug.
for (int i = 0; i < GridView1.Rows.Count - 1; i++)
{
change the capture condition i < GridView1.Rows.Count - 1; to i < GridView1.Rows.Count; and it should work.
BlehBlahBah 11-Dec-12 4:49am
   
It's not a one row grid. Also no error is being generated. I added an extra line between the "}" bracket and the line: string strFile. After removing the line, it enters the statement of the strFile but doesn't create one.
CS2011 11-Dec-12 5:02am
   
Can you post your code.
BlehBlahBah 11-Dec-12 5:07am
   
//string strValue = string.Empty;

//for (int i = 0; i < Gen_Lic_Grid.Rows.Count - 1; i++)
//{
// for (int j = 0; j < Gen_Lic_Grid.Rows[i].Cells.Count; j++)
// {
// if (!string.IsNullOrEmpty(Gen_Lic_Grid.Rows[i].Cells[j].Text.ToString()))
// {
// if (j > 0)
// strValue = strValue + "," + Gen_Lic_Grid.Rows[i].Cells[j].Text.ToString();
// else
// {
// if (string.IsNullOrEmpty(strValue))
// strValue = Gen_Lic_Grid.Rows[i].Cells[j].Text.ToString();
// else
// strValue = strValue + Environment.NewLine + Gen_Lic_Grid.Rows[i].Cells[j].Text.ToString();
// }
// }
// }
// // strValue = strValue + Environment.NewLine;
//}
//string strFile = @"D:\YourCSVFile.csv";

//if (File.Exists(strFile) && !string.IsNullOrEmpty(strValue))
//{
// File.WriteAllText(strFile, strValue);
//}
jj1992 11-Mar-14 3:54am
   
i have done like this but i got only a blank exel page
Abdullah ahmed 26-Apr-14 7:46am
   
Create csv file to D:\ownmake.csv then the data insert your this fille ownmake.csv
Start with this[^].
   
Hi All , It can be done in the another way.

C#
protected void ibtnExporttoCSV_Click(object sender, ImageClickEventArgs e)
{
    Response.ContentType = "application/csv";
    Response.AddHeader("content-disposition",
     "attachment;filename=GridViewExport.csv");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    string strValue = string.Empty;
    grdDisplay.AllowPaging = false;
    grdDisplay.DataBind();
    for (int i = 0; i < grdDisplay.Rows.Count; i++)
    {
        for (int j = 0; j < grdDisplay.Rows[i].Cells.Count; j++)
        {
            if (!string.IsNullOrEmpty(grdDisplay.Rows[i].Cells[j].Text.ToString()))
            {
                if (j > 0)
                    strValue = strValue + "," + grdDisplay.Rows[i].Cells[j].Text.ToString();
                else
                {
                    if (string.IsNullOrEmpty(strValue))
                        strValue = grdDisplay.Rows[i].Cells[j].Text.ToString();
                    else
                        strValue = strValue + "\n" + grdDisplay.Rows[i].Cells[j].Text.ToString();
                }
            }
        }

    }

    Response.Write(strValue.ToString());
    Response.End();


}
   
v2
Quote:
Here is the above code corrected and modified to meet you requiremnets-


StringBuilder fileText = new StringBuilder();
StringBuilder lineText = new StringBuilder();

for (int i = 0; i < grdDisplay.Rows.Count; i++)
    {
        lineText = new StringBuilder();

        for (int j = 0; j < grdDisplay.Rows.Count; j++)
            {
                if (j == 0)
                    lineText.Append(grdDisplay.Rows[i].Cells[j].Value.ToString());
                else
                    {
                        if (!string.IsNullOrEmpty(grdDisplay.Rows[i].Cells[j].Value.ToString()))
                            lineText.Append("," + grdDisplay.Rows[i].Cells[j].Value.ToString());
                        else
                            lineText.Append(",");
                    }
            }
        fileText.AppendLine(lineText.ToString());
    }
string strFile = Application.StartupPath + @"\UpdatedCourseStatus.csv";

if (!File.Exists(strFile) && !string.IsNullOrEmpty(fileText.ToString()))
    {
        File.WriteAllText(strFile, fileText.ToString());
	}
   
v3
asp.net gridview export to CSV file.... Gridview to CSV

kerry
   
   

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