将GridView导出到Excel时出错

时间:2013-04-10 16:12:21

标签: c# asp.net excel gridview

当我将网格视图导出到Excel时我有此消息错误 我的数据库是SQL Server,我的表有8000行 当我只选择1行到gridview时,它导出到excel没有错误 但是当我选择所有行时,会出现此问题 这是我的代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    { 
    System.Data.SqlClient.SqlDataReader reader = objCalep.SelectAll(true, false, "[تاريخ الاستشهاد]");
    //    System.Data.SqlClient.SqlDataReader reader = objCCP.CPSelectNo(2700);
    DataTable dataTable = new DataTable();
    dataTable.Load(reader);
    GridView1.DataSource = dataTable;
    GridView1.DataBind();
    }

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //System.Data.Common.DbDataRecord drv = e.Row.DataItem as System.Data.Common.DbDataRecord;
        DataRowView dataRowView = e.Row.DataItem as DataRowView;
        DataRow drv = dataRowView.Row;

        Object ob = drv["تاريخ الاستشهاد"];
        if (!Convert.IsDBNull(ob))
        {
            DateTime dateTime = (DateTime)ob;
            e.Row.Cells[4].Text = dateTime.ToString("dd/MM/yyyy");
            e.Row.Cells[4].Width = 100;
        }
    }
}
protected void btn_Add_Click(object sender, ImageClickEventArgs e)
{
    PrepareGridViewForExport(GridView1);
    ExportGridView();
}
private void ExportGridView()
{
    string attachment = "attachment; filename=حلب.xls";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    GridView1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}

private void PrepareGridViewForExport(Control gv)
{
    LinkButton lb = new LinkButton();
    Literal l = new Literal();
    string name = String.Empty;
    for (int i = 0; i < gv.Controls.Count; i++)
    {
        if (gv.Controls[i].GetType() == typeof(LinkButton))
        {
            l.Text = (gv.Controls[i] as LinkButton).Text;
            gv.Controls.Remove(gv.Controls[i]);
            gv.Controls.AddAt(i, l);
        }
        else if (gv.Controls[i].GetType() == typeof(DropDownList))
        {
            l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
            gv.Controls.Remove(gv.Controls[i]);
            gv.Controls.AddAt(i, l);
        }
        else if (gv.Controls[i].GetType() == typeof(CheckBox))
        {
            l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
            gv.Controls.Remove(gv.Controls[i]);
            gv.Controls.AddAt(i, l);
        }
        if (gv.Controls[i].HasControls())
        {
            PrepareGridViewForExport(gv.Controls[i]);
        }
    }
}

这是图片错误Error message

1 个答案:

答案 0 :(得分:0)

好吧,我无法阅读很多错误消息,但我猜测网格的ViewState数据大于您网站上配置的最大请求长度(我相信默认值是4MB)。

尝试更改web.config的<httpRuntime maxRequestLength="158334976" />部分中的<system.web>

如果可行,您可以将maxRequestLength保持在足够高的值,或者为网格禁用ViewState,但是在回发后您必须再次从数据库中检索数据。