Need to Export Gridview1 box to Excel

giasone777

Active member
Joined
Jul 19, 2019
Messages
29
Programming Experience
1-3
I have a webpage that is using ASP.net textboxes and an ASP.net GridView1. I am using C# to place the SQL data into the asp boxes, one of them being the GridView1 box.

On the web page there are several buttons that fetch VMware object information and then send the information to the gridview. What I want to do is have another button that sends the information from the GridView1 to an excel file.

None of the examples that I have searched for have worked - Please help, see below an example from my c# code - Thank-you - Jason.

Ok removed it.
 
Last edited:
A friendly reminder, I am at the beginner level and my programmer friend is telling me this is advanced level code, which is above my pay grade, so I don't know what to do with it, I thought if you don't mind looking at this code and telling me why it isn't working, the document downloads but there is no data in it - Thank-you Sir.
C#:
    Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=Employee.csv");
            Response.Charset = "";
            Response.ContentType = "application/text";
            GridView1.AllowPaging = false;
            GridView1.DataBind();

            StringBuilder columnbind = new StringBuilder();
            for (int k = 0; k < GridView1.Columns.Count; k++)
            {

                columnbind.Append(GridView1.Columns[k].HeaderText + ',');
            }

            columnbind.Append("\r\n");
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                for (int k = 0; k < GridView1.Columns.Count; k++)
                {

                    columnbind.Append(GridView1.Rows[i].Cells[k].Text + ',');
                }

                columnbind.Append("\r\n");
            }
            Response.Output.Write(columnbind.ToString());
            Response.Flush();
            Response.End();
 
Do you have at least the column headers in the file that get downloaded? Or is that missing as well?
 
If you set a breakpoint on this line:
C#:
for (int k = 0; k < GridView1.Columns.Count; k++)
what is the value of GridView1.Columns.Count?

Where is the GridView1.DataSource set?
 
I will check tomorrow when back at work, but I am thinking after the gridview1.databind() command the grid displays the data but then resets itself to zero, which is why nothing can be pulled from it, does that make sense?
 
Anyway, why iterate over the rows and columns of the DataGrid? You should be iterating over the rows and columns of the data that you are feeding into the grid.
 
I found the code below to export a gridview to an excel file and it works but the grid lines do not show up in the spreadsheet. I have tried all kinds of variations of different code with no luck. If anyone has any ideas please feel free to share. Thank-you.

C#:
             Response.Clear();
             Response.Buffer = true;
             Response.Charset = "";
             Response.ContentType = "application / vnd.openxmlformats - officedocument.spreadsheetml.sheet";
             Response.AddHeader("content-disposition", "attachment;filename=SqlExport.xls");
             System.IO.StringWriter stringWrite = new System.IO.StringWriter();
             System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
             GridView1.RenderControl(htmlWrite);
             Response.Write("<Gridlines=both>");
             GridView1.GridLines = GridLines.Both;
             GridView1.DataBind();
             GridView1.RenderControl(htmlWrite);
             Response.Write(stringWrite.ToString());
             Response.Write("</table>");
             Response.End();
 
Back
Top Bottom