• C#将DataTable导出Execl、Word、Xml


        /// <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 + "	");
            }
            sB.Append("
    ");
            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() + ""	"); //解决导出的单元格以科学计数法显示的问题
                }
                sB.Append("
    ");
            }
            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()+"
    ");  
           }  
           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

  • 相关阅读:
    linux/unix下 pid文件作用浅析
    gunicorn启动django时静态文件的加载
    分享30道Redis面试题,面试官能问到的我都找到了
    python利用mongodb上传图片数据 : GridFS 与 bson两种方式
    GridFS大文件的添加、获取、查看、删除
    你真的懂redis吗?
    mongoDB的复制集5----复制集安全(认证,用户,权限)
    MongoDB复制集安全认证
    MongoDB 用户名密码登录
    MongoDB 分片
  • 原文地址:https://www.cnblogs.com/liuswi/p/3816676.html
Copyright © 2020-2023  润新知