zoukankan      html  css  js  c++  java
  • 将Gridview中的数据出到excel或word中

    在以下按钮单击事件中实现:
    private void btnMIME_Click(object sender, System.EventArgs e)
    {
    dgShow.AllowPaging = false;
     BindData(); 

      Response.ContentType = "application/vnd.ms-excel";
     Response.AddHeader("Content-Disposition", "inline;filename="
       +   HttpUtility.UrlEncode("下载文件.xls",Encoding.UTF8   )   );   
     

     //如果输出为Word,修改为以下代码
     //Response.ContentType = "application/ms-word" 
     //Response.AddHeader("Content-Disposition", "inline;filename=test.doc") 
     StringBuilder sb=new StringBuilder(); 
     System.IO.StringWriter sw = new System.IO.StringWriter(sb);
     System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
     sb.Append("<html><body>");
     dgShow.RenderControl(hw);
     sb.Append("</body></html>"); 
     Response.Write(sb.ToString());
     Response.End();
    dgShow.AllowPaging = true;
    BindData(); 

    }
    注:1.若DataGrid中有按钮列,则在导出前应先将其隐藏.
        2.若DataGrid有分页,而又要打印所有数据的话就应先取消分页.

    2006年7月19日-补充:
    若为VS.NET2005中的数据绑定新控件GridView,以上代码会提示异常错误,经网上搜索,解决方案如下:

    1、在导出界面中重载
        public override void VerifyRenderingInServerForm(Control control)
        {
            //base.VerifyRenderingInServerForm(control);
        }
    2、
    web.config中修改<pages enableEventValidation ="false" ></pages>

    <%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true"

     CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>

  • 相关阅读:
    day01-html
    Word中划线的方法(五种)
    关于获取请求参数并处理显示的总结
    确定目标,把握时间,创造机会,努力加油!(关于大三下学期人生规划浅谈)
    重载运算符(一个例子)
    关于连接数据库的那点总结(感觉挺好的)
    Ribbon负载均衡
    idea中一份代码模拟集群
    Eureka集群
    Eureka配置服务提供者、服务消费者、失效剔除和自我保护
  • 原文地址:https://www.cnblogs.com/xianyin05/p/3187272.html
Copyright © 2011-2022 走看看