如何导出到Excel父级和嵌套GridView数据? [英] How To exporting to Excel parent and nested GridView data?
本文介绍了如何导出到Excel父级和嵌套GridView数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个gridview和一个嵌套的gridview,当我尝试将gridview数据导出到Excel时,我下载了整个网页,我确信这是因为嵌套的gridviews.
I have a gridview and some nested gridview inside it, when I try to export gridview data to Excel I get downloaded entire web page, I am sure it is beacause of nested gridviews.
如何将父级和嵌套的gridview数据导出到Excel工作表中?
How can I export parent and nested gridview data into an Excel sheet?
我使用以下代码导出到excel
I use the following code to export to excel
gvExportToExcel.DataSource = objDs;
gvExportToExcel.DataBind();
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
curContext.Response.Clear();
curContext.Response.Buffer = true;
curContext.Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("SearchSubmissionResult", System.Text.Encoding.UTF8) + ".xls");
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
this.ClearControls(gvExportToExcel);
gvExportToExcel.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
推荐答案
使用以下代码解决了此问题.
Cleared this issue with following code.
//Clear the controls inside the parent grid-view before render.
gvExportToExcel.DataSource = objDs;
gvExportToExcel.DataBind();
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
curContext.Response.Clear();
curContext.Response.Buffer = true;
curContext.Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("SearchSubmissionResult", System.Text.Encoding.UTF8) + ".xls");
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
this.ClearControls(gvExportToExcel);
gvExportToExcel.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
//Clear method.
private void ClearControls(Control control)
{
try
{
for (int i = control.Controls.Count - 1; i >= 0; i--)
{
ClearControls(control.Controls[i]);
}
if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control, null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else
if (control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control, null);
control.Parent.Controls.Remove(control);
}
}
}
catch (Exception ee)
{
lblError.Visible = true;
lblError.Text = ee.Message;
}
return;
}
这篇关于如何导出到Excel父级和嵌套GridView数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文