zoukankan      html  css  js  c++  java
  • .net中将DataTable导出到word、Excel、txt、htm的方法

    dt:DataTable

    strFile:fileName

    strExt:type


    private void GridExport(DataTable dt, string strFile, string strExt)
        {
            string strAppType = "";
            switch (strExt)
            {
                case "xls":
                    strAppType = "application/ms-excel";
                    break;
                case "doc":
                    strAppType = "application/ms-word";
                    break;
                case "txt":
                    strAppType = "application/ms-txt";
                    break;
                case "html":
                case "htm":
                    strAppType = "application/ms-html";
                    break;
                default: return;
            }
            GridView MyGridView = new GridView();
            MyGridView.DataSource = dt;
            MyGridView.DataBind(); 
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.AddHeader("Content-Type", "text/html; charset=GB2312");
            HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.{1}", HttpUtility.UrlEncode(strFile,Encoding.GetEncoding("GB2312")), strExt));
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); 
            HttpContext.Current.Response.ContentType = strAppType; 
            //MyGridView.Page.EnableViewState = false;
            //二、定义一个输入流
            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            //三、将目标数据绑定到输入流输出
            MyGridView.RenderControl(oHtmlTextWriter);
            HttpContext.Current.Response.Write(oStringWriter.ToString());
            HttpContext.Current.Response.End();
        }

  • 相关阅读:
    7.$a = 'abcdef'; 请取出$a的值并打印出第一个字母
    8.PHP可以和sql server/oracle等数据库连接吗?
    6.能够使HTML和PHP分离开使用的模板
    4.用PHP打印出前一天的时间格式是2006-5-10 22:21:21
    5.echo(),print(),print_r()的区别
    3.数据库中的事务是什么?
    spring中配置quartz调用两次及项目日志log4j不能每天生成日志解决方法
    tomcat7性能调优与配置(以windows版为例)
    eclipse中maven下载不了私服上面的第三方包问题
    birt4.6部署到tomcat及启动服务报错解决方法
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5182121.html
Copyright © 2011-2022 走看看