惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
腾讯CDC
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园_首页
量子位
博客园 - 聂微东
Jina AI
Jina AI
小众软件
小众软件
The Cloudflare Blog
有赞技术团队
有赞技术团队
V
V2EX
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志
B
Blog
MongoDB | Blog
MongoDB | Blog
L
LangChain Blog
宝玉的分享
宝玉的分享
C
Check Point Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
N
Netflix TechBlog - Medium
I
InfoQ
J
Java Code Geeks
S
SegmentFault 最新的问题
V
Visual Studio Blog
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
D
DataBreaches.Net
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
B
Blog RSS Feed
S
Schneier on Security
Webroot Blog
Webroot Blog
P
Proofpoint News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
Project Zero
Project Zero
Scott Helme
Scott Helme
C
CERT Recently Published Vulnerability Notes
P
Privacy International News Feed
T
The Exploit Database - CXSecurity.com
D
Darknet – Hacking Tools, Hacker News & Cyber Security

博客园 - shenpeng

C#中的具名参数和可选参数 C#中使用ExcelDataReader读取Excel文件 使用OleDbCommandBuilder时出现“Insert into 语句的语法错误”的解决方法(转) 在eclipse中使用中文JAVA api文档 .NET、ASP.NET控件及源码大汇总 60多个精品源码站 ASP.net常用代码(常用技巧备忘) C#程序转为VB.NET程序的一个小问题 Any和Some和ALL 的使用,以及交操作差操作的嵌套查询(Oracle) (转) CEIL和FLOOR函数查询(Oracle,MSSQL) (转) 字符串分割自定义函数(SQL) (转) ORACLE中巧用一条SQL 实现其它进制到十进制转换(转) 数据库优化设计方案(转) 浅谈反射与特性在接口系统中的应用(编码表转化) (转) 数字格式化(转) 纵表变横表经典中的经典 Web页面的数据导出excel时的格式问题(转) Access中使用SQL语句应掌握的几点技巧(转) 在SQL Server中修改sa的密码
将DataSet导出成XLS、XML、HTML、CSV、TSV等格式
shenpeng · 2008-03-17 · via 博客园 - shenpeng

  1        /// <summary>
  2        /// 导出DataSet到客户端
  3        /// </summary>
  4        /// <param name="dsResults">源DataSet</param>
  5        /// <param name="enExport">导出类型</param>
  6        /// <param name="strColDelim">列字符</param>
  7        /// <param name="strRowDelim">行字符</param>
  8        /// <param name="strFileName">导出文件名</param>

  9        public static void ExportDataSet(DataSet dsResults , ExportFormat enExport,string strColDelim, string strRowDelim, string strFileName)
 10        {
 11            DataGrid dgExport = new DataGrid();        
 12            dgExport.AllowPaging =false;
 13            dgExport.DataSource =dsResults;
 14            dsResults.DataSetName ="NewDataSet";
 15            dgExport.DataMember =  dsResults.Tables[0].TableName;
 16            dgExport.DataBind();            
 17            System.Web.HttpContext.Current.Response.Clear();            
 18            System.Web.HttpContext.Current.Response.Buffer= true;
 19            System.Web.HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("gb2312");
 20            System.Web.HttpContext.Current.Response.Charset = "";
 21            System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition""attachment; filename=" +strFileName );
 22            switch(enExport.ToString().ToLower())
 23            {
 24                case "xls":
 25                {
 26                    System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";                                        
 27                    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();                                        
 28                    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
 29                    dgExport.RenderControl(oHtmlTextWriter);
 30                    System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString());
 31                    break;
 32                }

 33                case "custom":
 34                {
 35                    string strText;                            
 36                    System.Web.HttpContext.Current.Response.ContentType = "text/txt";                                                    
 37                    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();                    
 38                    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);                    
 39                    dgExport.RenderControl(oHtmlTextWriter);
 40                    strText = oStringWriter.ToString();                    
 41                    strText = ParseToDelim(strText ,strRowDelim,strColDelim);
 42                    System.Web.HttpContext.Current.Response.Write(strText);
 43                    
 44                    break;
 45                }

 46                case "csv":
 47                {
 48                    string strText;        
 49                    strRowDelim = System.Environment.NewLine;
 50                    strColDelim = ",";
 51                    System.Web.HttpContext.Current.Response.ContentType = "text/txt";
 52                    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();                    
 53                    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);                                        
 54                    dgExport.RenderControl(oHtmlTextWriter);
 55                    strText = oStringWriter.ToString();                    
 56                    strText = ParseToDelim(strText ,strRowDelim,strColDelim);
 57                    System.Web.HttpContext.Current.Response.Write(strText);
 58                    break;
 59                }
            
 60                case "tsv":
 61                {
 62                    string strText;        
 63                    strRowDelim = System.Environment.NewLine;
 64                    strColDelim = "\t";
 65                    System.Web.HttpContext.Current.Response.ContentType = "text/txt";                                                    
 66                    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();                    
 67                    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);                                        
 68                    dgExport.RenderControl(oHtmlTextWriter);
 69                    strText = oStringWriter.ToString();                    
 70                    strText = ParseToDelim(strText ,strRowDelim,strColDelim);
 71                    System.Web.HttpContext.Current.Response.Write(strText);
 72                    break;
 73                }

 74                case "xml":
 75                {
 76                    System.Web.HttpContext.Current.Response.ContentType = "text/xml";                    
 77                    System.Web.HttpContext.Current.Response.Write(dsResults.GetXml());                                        
 78                    break;
 79                }

 80                case "htm":
 81                {                    
 82                    System.Web.HttpContext.Current.Response.ContentType = "text/html";
 83                    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();                    
 84                    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
 85                    dgExport.RenderControl(oHtmlTextWriter);                    
 86                    System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString());                    
 87                    break;
 88                }

 89            }

 90            System.Web.HttpContext.Current.Response.End ();
 91        }

 92
 93        "Export To a Delim Format"
107    }
108
109
110        
111    public enum ExportFormat
112    {
113        XML,
114        XLS,
115        HTML,
116        CSV,
117        CUSTOM,
118        TSV    
119    }