【问题标题】:nested gridview export to excel, asp.net c#嵌套gridview导出到excel,asp.net c#
【发布时间】:2011-12-22 07:17:58
【问题描述】:

我想将父网格视图和嵌套网格视图导出到 excel。

我的代码只能用于导出普通的简单网格视图。 尝试调试代码,发现没有任何错误。

只是在我点击导出按钮后,它并没有导出到excel。

这是我的代码

 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。

【问题讨论】:

  • lakshmik.blogspot.com/2006/04/… n 上试过。但仍然无法让它发挥作用。嗯?
  • 调试的时候 htmlWriter 中是否有任何内容,然后再写入响应?所以在 form.RenderControl(htmlWriter); 之后

标签: c# asp.net excel gridview


【解决方案1】:

我过去曾遇到过“必须将其放置在带有 runat=server 的表单标签中”的错误,并通过在我的页面代码隐藏中添加以下覆盖方法解决了该问题:

public override void VerifyRenderingInServerForm(Control control) {}

More info on MSDN.

作为参考,我还使用此代码导出了我的 GridView:

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();

【讨论】:

    猜你喜欢
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 2016-11-30
    相关资源
    最近更新 更多