关于Gridview导出 [英] Regarding Gridview Export

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

问题描述

我希望将自己的gridview数据导出到excel.
我的Gridview和用于导出的按钮在更新面板中.
我已经使用了< triggers>< asp:asynPost ...> button> ...
但这不能解决我的问题
我正在使用以下代码来导出按钮单击内部.

I want my gridview data to be exported to excel.
My Gridview and the button used for exporting is inside update panel.
I have used <triggers><asp:asynPost...>button>...
but this is not solving my problem
I am using following code for exporting inside button click.

Response.Clear();
Response.ContentType = "application/vnd.xls";
Response.AddHeader("content-disposition",
"attachment;filename=contacts.xls");

StringWriter swriter = new StringWriter();
HtmlTextWriter hwriter = new HtmlTextWriter(swriter);

HtmlForm frm = new HtmlForm();
this.gv_productsRequests.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(this.gv_productsRequests);
frm.RenderControl(hwriter);

Response.Write(swriter.ToString());
Response.End();



任何帮助将不胜感激..



Any help will be appreciated..

推荐答案

您可以尝试使用此自定义工具
导出到Excel
You can try this custom tool
Export to Excel


嗨....

您可以在下面的代码中尝试使用此代码.
Hi ....

You can try this below code.
private void ExportGridView()
    {
        string attachment = "attachment; filename=Employee details.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]);
           }
       }







protected void Expbtn_Click(object sender, EventArgs e)
    {
        PrepareGridViewForExport(GridView1);
        ExportGridView();

   }


它与我共享的代码相同.
只需在源代码视图的第一行中添加--EnableEventValidation ="false"
Its just the same code i had shared.
Just add in the first line of your source view--EnableEventValidation="false"


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

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