/// <summary>
    /// 将DT转换为Execl的方法
    /// </summary>
    /// <param name="dt">需要导出的DT
    /// <param name="page">页面
    /// <param name="fileName">文件名
    public void ToExecl(DataTable dt, Page page, string fileName)
    {
        HttpResponse response = page.Response;
        response.Clear();
        response.ContentType = "application/x-excel";
        response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
        StringBuilder sB = new StringBuilder();
        for (int j = ; j < dt.Columns.Count; j++)
        {
            sB.Append(dt.Columns[j].Caption + "\t");
        }
        sB.Append("\n");
        for (int i = ; i < dt.Rows.Count; i++)
        {
            for (int k = ; k < dt.Columns.Count; k++)
            {
                sB.Append("=\"" + dt.Rows[i][k].ToString() + "\"\t"); //解决导出的单元格以科学计数法显示的问题
            }
            sB.Append("\n");
        }
        response.Write(sB.ToString());
        response.End();
    } public void ToWord(DataTable dt, Page page, string filName)
{
HttpResponse response = page.Response;
response.Clear();
response.ContentType = "application/msword";
response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
response.AddHeader("Content-Disposition","attachment:filename="+System.Web.HttpUtility.UrlEncode(filName,System.Text.Encoding.UTF8)+".doc");
StringBuilder sBuilder = new StringBuilder();
for (int i = ; i < dt.Rows.Count; i++)
{
sBuilder.Append(dt.Rows[i][].ToString()+"\n");
}
response.Write(sBuilder.ToString());
response.End();
} public void ToXML(DataTable dt, Page page, string filename)
{
HttpResponse response = page.Response;
//DataSet ds = new DataSet();
response.Clear();
response.ContentType = "application/x-excel";
response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename,System.Text.Encoding.UTF8) + ".xls");
System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();
System.Xml.XmlTextWriter xw = new XmlTextWriter(response.OutputStream, utf8);
xw.Formatting = Formatting.Indented;
xw.Indentation = ;
xw.IndentChar = ' ';
dt.TableName = "dd";
dt.WriteXml(xw);
dt = null;
GC.Collect();
xw.Flush();
xw.Close();
response.End();
}

来源:http://blog.csdn.net/smartsmile2012/article/details/8182862

C#将DataTable导出Execl、Word、Xml的更多相关文章

  1. c#(NPOI)DataTable导出execl,execl(支持解析公式)导入DataTable

    NPOI(C#)DataTable导出execl using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserMod ...

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

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

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

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

  4. 关于datatable导出execl

    导出主要考虑响应流的问题 curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Resp ...

  5. .net中将DataTable导出到word、Excel、txt、htm的方法

    dt:DataTable strFile:fileName strExt:type private void GridExport(DataTable dt, string strFile, stri ...

  6. asp.net中导出Execl的方法

    一.asp.net中导出Execl的方法: 在 asp.net中导出Execl有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址 输出在浏览器上:一种是将文件直接将文件输出流写给 ...

  7. DataTable导出到Excel

    简单的导出到Excel中: 代码如下: using System; using System.Collections.Generic; using System.Data; using System. ...

  8. Java 实现HTML富文本导出至word完美解决方案

    一. 问题的提出 最近用java开发一个科技项目信息管理系统,里面有一个根据项目申请书的模板填写项目申报信息的功能,有一个科技项目申请书word导出功能. 已有的实现方式:采用标准的jsp模板输出实现 ...

  9. java导出生成word(类似简历导出)

    参考帖子: http://www.cnblogs.com/lcngu/p/5247179.html http://www.cnblogs.com/splvxh/archive/2013/03/15/2 ...

随机推荐

  1. 【POJ 2987】Firing (最小割-最大权闭合子图)

    裁员 [问题描述] 在一个公司里,老板发现,手下的员工很多都不务正业,真正干事员工的没几个,于是老板决定大裁员,每开除一个人,同时要将其下属一并开除,如果该下属还有下属,照斩不误.给出每个人的贡献值和 ...

  2. 实现ImageView中两张图片重叠显示

    第一种XML配置 使用layer-list标签 <layer-list xmlns:android="http://schemas.android.com/apk/res/androi ...

  3. String.format 细节

    String.format(Locale.US, format, args); format 参数 如果有% 那么%后面必须跟一个合法的字符,否则崩溃, 因为在String.format中 %为特殊字 ...

  4. BZOJ_3670_[NOI2014]_动物园_(kmp)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=3670 对于一个字符串,求出num数组,其中num[i]表示前i个字符构成的字串中不相重合的相同 ...

  5. Windows SharePoint Services 默认母版页

    转:http://msdn.microsoft.com/zh-cn/library/ms467402(v=office.12).aspx 最终用户可以自定义的 Windows SharePoint S ...

  6. java 正则表达式例子, 查找字符串

    import java.util.regex.Matcher;import java.util.regex.Pattern; public class Main { public static voi ...

  7. JavaScript---网络编程(5)-自定义对象Json、Dom模型概念讲解

    这节博客主要讲解Dom模型概念~和JSON的简单介绍 首先,还是先上out.js的代码: function println(param){ document.write(param+"< ...

  8. C语言调用汇编实现字符串对换

    1. 前面配置arm交叉编译环境. 2. 配置好qemu-arm C语言代码string-switch.c: #include <stdio.h> #include <stdlib. ...

  9. Joomla必备模块(转自joomla8)

    1.Akeeba Backup Core-joomla!网站备份组件. 不论是国内收费主机还是国外的收费主机甚至是免费主机,都必须考虑到数据安全问题.没有哪个主机商可以保证你的网站数据不会丢失,况且很 ...

  10. 基于TCP协议的服务器(多线程)

    import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; impo ...