在母版页中导出到Excel Gridview [英] Export to Excel Gridview in master pages

查看:85
本文介绍了在母版页中导出到Excel Gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有要求,即在母版页中导出到Excel GridView数据.在尝试导出时,出现 sys.webforms.pagerequestmanagerparsererrorexception 错误.任何人都可以帮助我解决这个问题.我尝试了如下不同方式
1.

DataTable dt = BindCompanies();
        string attachment = "attachment; filename=Employee.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/vnd.ms-excel";
        string tab = "";
        foreach (DataColumn dc in dt.Columns)
        {
            Response.Write(tab + dc.ColumnName);
            tab = "\t";
        }
        Response.Write("\n");
        int i;
        foreach (DataRow dr in dt.Rows)
        {
            tab = "";
            for (i = 0; i < dt.Columns.Count; i++)
            {
                Response.Write(tab + dr[i].ToString());
                tab = "\t";
            }
            Response.Write("\n");
        }
        Response.End();



2.

string attachment = "attachment; filename=Export.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
gv.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv);
frm.RenderControl(htw);

//GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();



问候,
Jeevan.

解决方案

 受保护的 尝试
{
     this  .GridView1.AllowPaging =  false ;
     this  .GridView1.AllowSorting =  false ;
     .GridView1.EditIndex = -1;

    // 让我们将数据绑定到GridView 
     .BindData();

    // 让我们输出GridView的HTML 
    Response.Clear();
    Response.ContentType = " ;
    Response.AddHeader(" " );
    Response.Charset = " ;
    StringWriter swriter =  StringWriter();
    HtmlTextWriter hwriter =  HtmlTextWriter(swriter);
    GridView1.RenderControl(hwriter);
    Response.Write(swriter.ToString());
    Response.End();
}
捕获(异常exe)
{
    抛出 exe;
}

} 



使用此代码并尝试它将对我有用...

并且要注意不要将网格放在更新面板的旁边,否则会导致这样的错误...

控件'  GridView1'类型为 GridView'必须放置在带有runat = server的表单标签中.


请使用以下链接:

从C#中的GridView导出到Excel [ ^ ]

能否请您通过链接[



2.

string attachment = "attachment; filename=Export.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
gv.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv);
frm.RenderControl(htw);

//GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();



Regards,
Jeevan.

解决方案

protected void Export2Excel()
{
try
{
    this.GridView1.AllowPaging = false;
    this.GridView1.AllowSorting = false;
    this.GridView1.EditIndex = -1;

    // Let's bind data to GridView
    this.BindData();

    // Let's output HTML of GridView
    Response.Clear();
    Response.ContentType = "application/vnd.xls";
    Response.AddHeader("content-disposition",
            "attachment;filename=MyList.xls");
    Response.Charset = "";
    StringWriter swriter = new StringWriter();
    HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
    GridView1.RenderControl(hwriter);
    Response.Write(swriter.ToString());
    Response.End();
}
catch (Exception exe)
{
    throw exe;
}

}       



use this code and try it it will work for me...

and also take care that you need not put your grid in side a update panel or it will resulted in error like this...

Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.


Hi use below link:

Export to Excel from GridView in C#[^]


Could you please go through the link [^]. This is a very handy in this case.


这篇关于在母版页中导出到Excel Gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆