Monday, September 28, 2009 3:28 PM sukumarraju

Export GridView data to CSV file

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=SearchResults.csv");
Response.Charset = "";

Response.ContentType = "application/text";

gvSearchResutls.AllowPaging = false;

//Bind DataTable to GridView

//Search() method returns a DataTable of search results

gvSearchResutls.DataSource = Search();
gvSearchResutls.DataBind();

//String builder class to add row data
StringBuilder sb = new StringBuilder();

for (int k = 0; k < gvSearchResutls.Columns.Count; k++)
{

    //add separator

    sb.Append(gvSearchResutls.Columns[k].HeaderText + ',');

}

append new line

sb.Append("\r\n");
Get Rows
for (int i = 0; i < gvSearchResutls.Rows.Count; i++)
{
    //Get columns
    for (int k = 0; k < gvSearchResutls.Columns.Count; k++)
    {

        //add separator
        sb.Append(gvSearchResutls.Rows[i].Cells[k].Text + ',');

    }

    //append new line

    sb.Append("\r\n");

}

Response.Output.Write(sb.ToString());

Response.Flush();

Response.End();

References:

http://www.aspsnippets.com/

http://www.victorchen.info/export-datatable-to-csv-file-download-in-c/

Filed under:

Comments

# re: Export GridView data to CSV file

Monday, December 14, 2009 8:11 PM by johnjalani

How about if a csv file WITHOUT HEADER??

please help me.

thnx

# re: Export GridView data to CSV file

Thursday, December 17, 2009 12:41 AM by rkvedul@gmail.com

iam getting the csv files with only column names but not with rows values plss help me

# re: Export GridView data to CSV file

Friday, December 18, 2009 8:55 AM by sukumarraju

rkvedul@gmail.com said:

iam getting the csv files with only column names but not with rows values plss help me

Hi

Please post your source code.

# re: Export GridView data to CSV file

Sunday, March 21, 2010 7:00 PM by Zarek

Good afternoon. Your site is very convenient in navigation and has good design. Thanks. Help me! Need information about: Cosmetic dentistry poland. I found only this - <a href="dissconnected.net/.../CosmeticDentistry">new jersey cosmetic dentistry</a>. Treatments are upgrade, appearing you to straighten every government of your place and technically maintain about physician pointed around procedures, cosmetic dentistry. Cosmetic dentistry, as the disposal was contradictorily forgotten also, the suffering outpatient was required for body teeth and languages. With best wishes :-(, Zarek from Zealand.

# re: Export GridView data to CSV file

Thursday, March 25, 2010 8:23 AM by sukumarraju

Hi Zarek,

I am not a good web designer I am afraid. Note that this site is the results of Microsoft provided weblogs.

I choose templates and add content, choose the images.

Hope it helps you!

In order to get free CSS templates, check

www.freecsstemplates.org

http://www.csszengarden.com/

# re: Export GridView data to CSV file

Monday, August 02, 2010 2:07 PM by joshc1107

I had some problems with my application skipping out of both of your FOR loops when you use the gvSearchResults.Rows.Count.  Instead, I had to use "gvSearchResults.Rows[0].Cells.Count" in order to get an integer that would work with my loops.  Obviously this took some validation to make sure at least one row was returned, but it this worked for me.

(also, you spelled results as resutls)

Thanks for the post though - pointed me in the right direction!

# re: Export GridView data to CSV file

Monday, August 02, 2010 6:05 PM by sukumarraju

Hi Joshc1107,

Thanks for your comment. I think you mean to say 'gvSearchResults.Columns.Count' instead of gvSearchResults.Rows.Count.

Note that when you set GridView AutoGenerateColumns="True" in aspx, GridView.Column.Count always returns '0'. When you set AutoGenerateColumns="False" and declare columns exclusively, GridView.Columns.Count returns number of columns.

=====================================================

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >

       <Columns>

           <asp:BoundField DataField="productid" HeaderText="ProductId" />

           <asp:BoundField DataField="product_name" HeaderText="Product" />

           <asp:BoundField DataField="Quantity" HeaderText="Qty" />

       </Columns>

======================================================

Yes, there is typo, but the code works fine without any problems where GridView columns are specified as shown above.

# re: Export GridView data to CSV file

Tuesday, October 05, 2010 5:46 PM by prof.subbaram kumar

sir, your blog is blossoming

# Replace DBNull with Zero while exporting to CSV or Excel file

Friday, December 03, 2010 7:38 AM by Sukumar Raju's Blog

Exporting data from DataTale to CSV file is explained in this article. When the data contains NULL values

# Exporting DataTable to CSV file using Binary order markup

Tuesday, April 05, 2011 7:37 AM by Sukumar Raju's Blog

Exporting Data from DataTable object to .CSV is discussed here . The Issue While working with european

Leave a Comment

(required) 
(required) 
(optional)
(required)