asp.net(C#)中将DataTable导出Execl、Word、Xml、C#超级工具类

    /// <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 = 0; j < dt.Columns.Count; j++)
        {
            sB.Append(dt.Columns[j].Caption + "\t");
        }
        sB.Append("\n");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int k = 0; 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 = 0; i < dt.Rows.Count; i++)  
       {  
           sBuilder.Append(dt.Rows[i][1].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 = 4;  
    xw.IndentChar = ' ';  
    dt.TableName = "dd";  
    dt.WriteXml(xw);  
    dt = null;  
    GC.Collect();  
    xw.Flush();  
    xw.Close();  
    response.End();  
}  

MSCL超级工具类库
基于C#开发的超强工具类,包含数据库操作,字符串处理,文件或者文件夹处理
网络请求,缓存处理,数据容器等上百个常用工具类封装,附带调用示例和参数说明,
提供CHM详细文档,上百个生产环境使用,稳定高效,简单易用。
真正做到“工具在手,一切尽有”,让你大幅度的提高编程效率,提高编程水平。
联系QQ:7400799(请备注 "MSCL")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

smartsmile2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值