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();

    }

.net中将DataTable导出到word、Excel、txt、htm的方法的更多相关文章

  1. datatable导出到Word / Excel / PDF / HTML .NET

    原文发布时间为:2011-01-21 -- 来源于本人的百度文章 [由搬家工具导入] IEnumerable - DataTable Export to Word / Excel / PDF / HT ...

  2. DataTable导出为word,excel,html,csv,pdf,.txt

    using System; using System.Data; using System.Configuration; using System.Collections; using System. ...

  3. Delphi 导出数据至Excel的7种方法【转】

    一; delphi 快速导出excel   uses ComObj,clipbrd;   function ToExcel(sfilename:string; ADOQuery:TADOQuery): ...

  4. Delphi 导出数据至Excel的7种方法

    一; delphi 快速导出excel uses ComObj,clipbrd; function ToExcel(sfilename:string; ADOQuery:TADOQuery):bool ...

  5. 2、Python djang 框架下的word Excel TXT Image 等文件的下载

    2.python实现文件下载 (1)方法一.直接用a标签的href+数据库中文件地址,即可下载.缺点:word excel是直接弹框下载,对于image txt 等文件的下载方式是直接在新页面打开. ...

  6. Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,htm

    Ireport 报表导出 Poi + ireport 导出pdf, doc ,excel ,html 格式 下面是报表导出工具类reportExportUtils 需要导出以上格式的报表 只需要调用本 ...

  7. NPOI通过DataTable导出和读取Excel

    Excel导入及导出问题产生: 从接触.net到现在一直在维护一个DataTable导出到Excel的类,时不时还会维护一个导入类.以下是时不时就会出现的问题: 导出问题: 如果是asp.net,你得 ...

  8. C#中将DataTable导出为HTML的方法

    今天我要向大家分享一种将DataTable导出为到HTML格式的方法.有时我们需要HTML格式的输出数据, 以下代码就可以帮助我们达到目的,. 首先,我们要绑定DataTable和 DataGridV ...

  9. 第九课: - 导出到CSV / EXCEL / TXT

    第 9 课 将数据从microdost sql数据库导出到cvs,excel和txt文件. In [1]: # Import libraries import pandas as pd import ...

随机推荐

  1. 详解C++ friend关键字

    1. 为什么要使用友元? 通常对于普通函数来说,要访问类的保护成员是不可能的,如果想这么做那么必须把类的成员都生命成为 public( 共用的) ,然而这做带来的问题遍是任何外部函数都可以毫无约束的访 ...

  2. LIB文件和DLL文件的作用

    (1)lib是编译时需要的,dll是运行时需要的.如果要完成源代码的编译,有lib就够了.如果也使动态连接的程序运行起来,有dll就够了.在开发和调试阶段,当然最好都有.(2)一般的动态库程序有lib ...

  3. Web的工作机制

    简要的介绍一下Web的工作机制,以便对开发JavaWeb项目有个更好的理解. 一.Web的概念     1.1    何为Web:Web是万维网(World Wide Web)的简称.Web出现以前, ...

  4. python 元类与定制元类

    1:元类 元类:类的创建与管理者 所有类的元类是type class a: pass print(type(a)) 结果:<class 'type'> 2:定制元类 类的实例化过程:(可看 ...

  5. 关于html页面图片自动撑开的问题

    如下列代码: <div id="divnr" style="text-align: center; margin: 10px; width:600px;" ...

  6. 学习okhttp wiki--HTTPS

    HTTPS OkHttp尝试平衡两个相互竞争的要素: 连通性(Connectivity):连接到尽可能多的服务器.这包括运行最新版本 boringssl 的服务器和不太过时的老版本 OpenSSL 的 ...

  7. Map Resource Definition 中type为 'ArcGIS Server Local'.

    在使用 MapResourceManager, Map进行 地图显示 时, 在 Map Resource Definition 的 type 为 ArcGIS Server Local时概述. 1,打 ...

  8. Mediawiki.org的PHP编码约定

    http://www.mediawiki.org/wiki/Manual:Coding_conventions/PHP assignment作为expression来用看起来像个错误(looks su ...

  9. jQuery Mobile 网格

    在列容器中,根据不同的列数,子元素可设置类 ui-block-a|b|c|d|e.这些列将依次并排浮动. 网格中的列是等宽的(总宽是 100%),无边框.背景.外边距或内边距. 例: 对于 ui-gr ...

  10. 遗传算法matlab实现

    我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 以下运用MATLAB实现遗传算法:   clc clear   %参数 a = 0 ; b = 4 ; e ...