码迷,mamicode.com
首页 > 其他好文 > 详细

C#将DataTable导出Execl、Word、Xml

时间:2014-07-01 21:57:44      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   文件   os   

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


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

C#将DataTable导出Execl、Word、Xml,布布扣,bubuko.com

C#将DataTable导出Execl、Word、Xml

标签:style   blog   http   color   文件   os   

原文地址:http://www.cnblogs.com/liuswi/p/3816676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!