2011-12-22 6 views
1

親グリッドビューとネストされたグリッドビューをExcelにエクスポートしたい。ネストされたグリッドビューをExcelにエクスポートする、asp.net c#

通常の単純なグリッドビューをエクスポートするためのコードしかありません。 コードをデバッグしようとしましたが、エラーはありません。

エクスポートボタンをクリックするとすぐに、エクセルにエクスポートされません。

は、ここに私のコード

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コントロールを有するものです。

+0

はhttp://lakshmik.blogspot.com/2006/04/aspnet-export-to-excelword-from-nestedに試してみました.html その他それでもそれは仕事を得ることはできません。うん? –

+0

レスポンスに書き込む前に、デバッグするときにhtmlWriterに内容がありますか?したがって、form.RenderControl(htmlWriter)の後; –

答えて

0

私が経験した過去にエラー「にrunat =サーバーとのformタグの内側に配置されなければならない」と私のページの分離コードで、次のオーバーライドメソッドを追加することで問題を解決:

public override void VerifyRenderingInServerForm(Control control) {} 

More info on MSDN

は参考のために、私はまた私のGridViewsをエクスポートするためにこのコードを使用:

Response.Clear(); 
Response.Buffer = true; 
Response.AddHeader("content-disposition", "attachment;filename=your_file.xls"); 
Response.Charset = ""; 
Response.ContentEncoding = System.Text.Encoding.Default; 
Response.ContentType = "application/ms-excel"; 

StringWriter sw = new StringWriter(); 
HtmlTextWriter hw = new HtmlTextWriter(sw); 
your_GridView.RenderControl(hw); 

Response.Output.Write(sw.ToString()); 
Response.Flush(); 
Response.End(); 
関連する問題