1
부모 그리드보기 및 중첩 된 gridview를 Excel로 내보내려고합니다.Excel로 중첩 된 gridview 내보내기, asp.net C#
일반 간단한 gridview 만 내보내는 코드가 있습니다. 코드를 디버깅하려고했으나 아무런 오류가없는 것으로 나타났습니다.
내보내기 버튼을 클릭하면 바로 내보낼 수 없습니다.
여기 내 코드
protected void asd()
{
string title = "";
string attempt = "SELECT * FROM Poll Where PollID = '" + Session["abc"] + "'";
cmd = new SqlCommand(attempt, con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
title = dr["PollTitle"].ToString();
}
string filename = String.Format(title + " RESULTS. " + "{0}_{1}.xls",
DateTime.Today.Month.ToString(), DateTime.Today.Year.ToString());
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
Response.Charset = "";
// SetCacheability doesn't seem to make a difference (see update)
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
// Replace all gridview controls with literals
ClearControls(GridView3);
// Throws exception: Control 'ComputerGrid' of type 'GridView'
// must be placed inside a form tag with runat=server.
// ComputerGrid.RenderControl(htmlWrite);
// Alternate to ComputerGrid.RenderControl above
System.Web.UI.HtmlControls.HtmlForm form
= new System.Web.UI.HtmlControls.HtmlForm();
Controls.Add(form);
form.Controls.Add(GridView3);
form.RenderControl(htmlWriter);
Response.Write(stringWriter.ToString());
Response.End();
}
private void ClearControls(Control control)
{
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);
}
}
return;
}
protected void Export_buttonClick(object sender, EventArgs e)
{
asd();
}
이 코드는 정상적인있는 gridview를 위해 노력하고 있지만 중첩의 gridview와 하나입니다.
는 http://lakshmik.blogspot.com/2006/04/aspnet-export-to-excelword-from-nested에 시도 .html n others. 그러나 아직도 위선적 인 말투는 그것을 너무 일하게한다. 흠? –
응답 할 때 디버그 할 때 htmlWriter에 내용이 있습니까? 그래서 form.RenderControl (htmlWriter); –