zoukankan      html  css  js  c++  java
  • C# asp.net 把GridView数据 导出到 Excel

    1        Response.Clear();
     2        Response.Buffer = true;
     3        Response.Charset = "GB2312";
     4        Response.AppendHeader("Content-Disposition""attachment;filename=jtkc.xls");
     5        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
     6        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
     7        this.EnableViewState = false;
     8        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN"true);
     9        System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
    10        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
    11        grvExcel.RenderControl(oHtmlTextWriter);
    12        Response.Write(oStringWriter.ToString());
    13        Response.End(); 

    1.这过程中遇到一些问题,刚开始是提示GridView必须放在runat="server"的窗体中,但是<from>已经设置了runat="server"但还是报错,问题解决如下:

    只需要重写VerifyRenderingInServerForm方法,将内容清空就可以了。

        public override void VerifyRenderingInServerForm(Control control)
        
    {
        }

    2。第二个问题是提示:只能在 Render(); 期间呼叫 RegisterForEventValidation

        这个问题比较好解决,只需要在前台页面的@Page指令中加上一个属性:EnableEventValidation="false"

  • 相关阅读:
    在 WF 4 中编写自定义控制流活动
    CLS(公共语言规范)的CLSCompliant(跨语言调用)
    public类型中internal成员
    最短路径—Dijkstra算法和Floyd算法
    System.Windows.Forms
    第一个Xamarin的 Android 应用程序!
    内地开源镜像网站
    Xamarin C# Android for Windows 安装
    TortoiseSVN 源代码下载
    Install Visual Studio Tools for Apache Cordova
  • 原文地址:https://www.cnblogs.com/hyd309/p/1407900.html
Copyright © 2011-2022 走看看