export to excel

J

JohnE

Hello. I have a gridview that has a nested gridview. I want to export the
main gridview information. I have been able to export other gridviews but
none had a nested gridview. My first thought was to export the underlying
sqldatasource rather then the gridview. Also, the information is already
available so a trip to the server would not be necessary. I have googled and
found some but couldn't get then to work properly. By that I mean
formatting, etc. What I am trying now is the following;

protected void btnExportToExcel_Click(object sender, EventArgs e)
{
gvwChangeRequestList.DataSourceID = "ChangeRequestListSqlDataSource";
DataTable table = new DataTable();
gvwChangeRequestList.DataBind();
gvwChangeRequestList.AllowPaging = false;
gvwChangeRequestList.AllowSorting = false;
gvwChangeRequestList.AutoGenerateEditButton = false;
gvwChangeRequestList.Columns[0].Visible = false;
PrepareGridViewForExport(gvwChangeRequestList);
//ExportToExcel();
ExportToSpreadsheet(table,"ChangeList");
}


public static void ExportToSpreadsheet(DataTable table, string name)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();
foreach (DataColumn column in table.Columns)
{
context.Response.Write(column.ColumnName + ";");
}
context.Response.Write(Environment.NewLine);
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
context.Response.Write(row.ToString().Replace(";",
string.Empty) + ";");
}
context.Response.Write(Environment.NewLine);
}
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.AppendHeader("Content-Disposition",
string.Format("attachment; filename=" + name + ".xls"));
context.Response.End();
}

This is not returning anything back. I get no error, just no information.
Is someone willing to review and see if I missed something? I'm also open to
ideas of a better way to export.

Thanks.
John
 
J

JohnE

Nevermind. I got it figured on the exporting of the sqldatasource rather the
the gridview with the nested gridview.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Create and open Excel spreadsheet from ASP.NET 2
gridview to excel 6
Export to Excel 0
export gridview to excel format error 6
export to excel 0
Export to Excel 3
Export Grid View to Excel 1
Export to Excel 2

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top