zoukankan      html  css  js  c++  java
  • 导出Excel随心所欲的使用GridView听后记

          今天下了Microsft的视频教程的《深入挖掘ASP.NET 2.0 》系列。刚才大体听了一下第一个教程——随心所欲的使用GridView。发现东西虽然都很简单,但将其融入到开发中,相信也是会非常不错的。尤其是本人对其中的导出Excel比较感兴趣。将代码测试了一下,效果还可以[虽然直接用来做项目,还略显粗糙,但毕竟有了个葫芦,我们也还是可以画出瓢来的,呵呵]。闲话打住,看代码:
     1protected void Button1_Click(object sender, EventArgs e)
     2    {
     3        Response.Clear();
     4        Response.Buffer = true;
     5        Response.Charset = "GB2312";
     6        Response.AppendHeader("Content-Disposition""attachment;filename=FileName.xls");
     7        // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
     8        Response.ContentEncoding = System.Text.Encoding.UTF8;
     9        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
    10        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
    11        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
    12        gvProduct.RenderControl(oHtmlTextWriter);
    13        Response.Output.Write(oStringWriter.ToString());
    14        Response.Flush();
    15        Response.End();
    16    }

    17    //确认在运行时为指定的 ASP.NET 服务器控件呈现 HtmlForm 控件
    18    public override void VerifyRenderingInServerForm(Control control)
    19    { }
    我在这里在着重说一下VerifyRenderingInServerForm()这个方法,他的作用注释里都有,在这里主要是避免产生“GridView 类型 的gvProduct 不再<form runat=server> 标记中”之类的错误。详情可以查看MSDN。
  • 相关阅读:
    JavaScript中的__proto__
    移动前端调试页面–weinre
    nodo合并多个mp3文件
    enctype和Content-type有什么关系
    vscode 实用的插件
    前端跨域问题及解决方案
    小小的js
    如何使用eslint
    RN记录
    numpy的索引
  • 原文地址:https://www.cnblogs.com/xpengfee/p/756251.html
Copyright © 2011-2022 走看看