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
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