加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

Sqlserver2008 数据库导出Excel

发布时间:2020-12-12 14:51:55 所属栏目:MsSql教程 来源:网络整理
导读:方法一: 调用方法:StringToCSV(DtToString(你的DataTable),"pd","派单"); ? /// summary /// 将DataTable转换为字符串 /// /summary /// param name="dt"/param /// returns/returns public static string DtToString(DataTable dt) { string data = ""; t

方法一:

调用方法:StringToCSV(DtToString(你的DataTable),"pd","派单");

?

        /// <summary>
        /// 将DataTable转换为字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DtToString(DataTable dt)
        {    
            string data = "";    
            try    
            {        
                foreach (DataColumn column in dt.Columns)       
                {            
                    data += column.ColumnName + ",";        
                }        
                data += "rn";//写出数据        
                foreach (DataRow row in dt.Rows)        
                {            
                    foreach (DataColumn column in dt.Columns)            
                    {                
                        string t = row[column].ToString();                
                        if (!string.IsNullOrEmpty(t))                
                        {                    
                            t = t.Replace(",","");                    
                            t = t.Replace("r","");                    
                            t = t.Replace("n","");                    
                            t = HttpContext.Current.Server.HtmlEncode(t);                    
                            data += t + ",";                
                        }                
                        else                    
                            data += ",";           
                    }            
                    data += "rn";        
                }        
                data += "rn";    
            }    
            catch { 
            }    
            return data;
        }
        
        /// <summary>
        /// 将字符串转换为CSV格式输出
        /// </summary>
        /// <param name="content">字符串内容</param>
        /// <param name="fileName">文件名(无扩展名)</param>
        /// <param name="msg">表头消息</param>
        public static void StringToCSV(string content,string fileName,string msg)
        {    
            try    
            {        
                string temp = string.Format("attachment;filename={0}.csv",fileName);        
                HttpContext.Current.Response.ClearHeaders();        
                HttpContext.Current.Response.Clear();        
                HttpContext.Current.Response.Buffer = true;        
                HttpContext.Current.Response.Charset = "UTF-8";        
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;        
                HttpContext.Current.Response.ContentType = "application/ms-excel";        
                HttpContext.Current.Response.AppendHeader("Content-disposition",temp);        
                if (!string.IsNullOrEmpty(msg))            
                    HttpContext.Current.Response.Write(msg + "rn");        
                HttpContext.Current.Response.Write(content);        
                HttpContext.Current.Response.End();    
            }   
            catch { }
        }


?

方法二:在sql数据库 将字段中的逗号换成分号, 将回车或换行换成分号,组装字符串。

在sql数据库 将字段中的逗号换成分号, 将回车或换行换成分号,组装字符串。

格式如下:

a,b,c

d,e,f

g,h,j

?

保存为.csv文件

?

?

推荐阅读:

【推荐】.NET使用NPOI组件将数据导出Excel??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读