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
 
Ad

Advertisements

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

Top