如何将数据从gridview传输到Excel文件 [英] how to transfer the data from a gridview to an excel file

查看:68
本文介绍了如何将数据从gridview传输到Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我想要将网格视图数据传输到excel文件的代码.单击按钮时必须执行什么操作?

解决方案

Interop是我发现处理的最简单方法通过C#精益求精.您可以在 http://support.microsoft.com/kb/302084 [使用OLEDB读写Excel [ 受保护的 对象发​​件人,EventArgs e) { .EnableViewState= false ; // 如果发生错误,请将其与在源中设置的EnableEventValidation ="false"一起添加 Response.Clear(); Response.Buffer = true ; Response.AddHeader(" " 附件; filename = exp.xls"); Response.Charset = " ; Response.ContentType = " ; StringWriter sw = StringWriter(); HtmlTextWriter ht = HtmlTextWriter(sw); GridView1.AllowPaging = false ; GridView1.DataBind(); GridView1.RenderControl(ht); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } 公共 覆盖 void VerifyRenderingInServerForm(System.Web .UI.Control控件) { }


hi i wanted the code for transferring the grid view data to an excel file.on button click what action has to be performed?

Interop is easiest way I found dealing with excel through C#. You will find many articles on this[^].


Two choises:

Interop:
http://support.microsoft.com/kb/302084[^]

OleDb:
Reading and Writing Excel using OLEDB[^]

Other options could be found on Google.


protected void Button1_Click(object sender, EventArgs e)
    {
        this.EnableViewState = false;    //in case error occurs add this along with EnableEventValidation="false" set in source
        Response.Clear();

        Response.Buffer = true;

        Response.AddHeader("content-disposition", "attachment;filename=exp.xls");

        Response.Charset = "";

        Response.ContentType = "application/vnd.ms-excel";

        StringWriter sw = new StringWriter();

        HtmlTextWriter ht = new HtmlTextWriter(sw);

        GridView1.AllowPaging = false;

        GridView1.DataBind();

        GridView1.RenderControl(ht);

        Response.Output.Write(sw.ToString());

        Response.Flush();

        Response.End();

    }
    public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
    {

    }


这篇关于如何将数据从gridview传输到Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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