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. LDAP 后缀操作

    创建目录服务器实例之后,必须为服务器的目录信息树(Directory Information Tree,DIT)创建一个或多个后缀.DIT由服务器中的所有条目组成,这些条目使用各自的标识名(Disti ...

  2. hdu 1076

    水题 AC代码: #include <iostream> using namespace std; int main() { int i,k,t,y,n; cin>>t; wh ...

  3. 常见Oracle数据库问题总结及解决办法(一)

    开发中常使用Oralce数据库,使用中也许会碰到形形色色的各类错误提示,如:ORA-00933:SQL命令未正确结束.ORA-009242等等,为此记录积累对于自己来说还是很有帮助的,今天就记录以前出 ...

  4. 使用证书部署出现System.Security.Cryptography.CryptographicException 错误解决方案

    一.System.Security.Cryptography.CryptographicException: 找不到对象 at System.Security.Cryptography.Cryptog ...

  5. C++中的dynamic_cast和static_cast

    代码: #include <cstdio> #include <iostream> using namespace std; class A{ public: virtual ...

  6. Wireshark对ping报文的解码显示(BE与LE) 转自作者:易隐者

    Wireshark对ping报文的解码显示(BE与LE) 我们非常熟悉ping报文的封装结构,但是,在这个报文解码里,我们发现wireshark的解码多了几个参数:Identifier(BE).Ide ...

  7. 用jquery向网页添加背景图片 拉伸 模糊 遮罩层 代码

    方法一:手动添加 1.在body内任意位置添加html代码 <div id="web_bg" style=" position:fixed; _position:a ...

  8. javascript改变背景/字体颜色(Through the javascript to change the background and font color)

    鼠标移动到.移出DIV时修改DIV的颜色: 1.Change the font and Div background color--function <div style="width ...

  9. centos6.7下 编译安装MySQL5.7

    centos6.7下编译安装MySQL5.7 准备工作 #-----依赖包及MySQL和boost安装包----- #yum包安装: shell> yum -y install gcc-c++ ...

  10. C程序设计语言练习题1-6

    练习1-6 验证表达式getchar() != EOF的值是0还是1. 代码如下: #include <stdio.h> // 包含标准库的信息. int main() // 定义名为ma ...