asp.net datatable 导出为 txt

如下是导出到TXT的方法
public static void ToTxt(DataTable dv, string FileName){System.IO.StringWriter sw = new System.IO.StringWriter();StringBuilder sb = new StringBuilder();//写标题for (int i = 0; i < dv.Columns.Count; i++){if (i > 0){sb.Append("\t ");}sb.Append(dv.Columns[i].ColumnName);}sw.WriteLine(sb.ToString());//写内容for (int rowNo = 0; rowNo < dv.Rows.Count; rowNo++){StringBuilder sbTemp = new StringBuilder();for (int columnNo = 0; columnNo < dv.Columns.Count; columnNo++){if (columnNo > 0){sbTemp.Append("\t ");}sbTemp.Append(dv.Rows[rowNo][columnNo].ToString());}sw.WriteLine(sbTemp.ToString());}System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;response.Clear();response.Buffer = true;response.ContentEncoding = System.Text.Encoding.Default;response.ContentType = "text/plan";response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".txt");response.Charset = "gb2312";response.Write(sw.ToString());response.End();sw.Close();}如下是导出到EXCEL的方法
/// <summary>/// Renders the html text before the datagrid./// </summary>/// <param name="writer">A HtmlTextWriter to write html to output stream</param>private static void FrontDecorator(HtmlTextWriter writer){writer.WriteFullBeginTag("HTML");writer.WriteFullBeginTag("Head");writer.WriteEndTag("Head");writer.WriteFullBeginTag("Body");}/// <summary>/// Renders the html text after the datagrid./// </summary>/// <param name="writer">A HtmlTextWriter to write html to output stream</param>private static void RearDecorator(HtmlTextWriter writer){writer.WriteEndTag("Body");writer.WriteEndTag("HTML");}public static void ToExcel(System.Web.UI.WebControls.DataGrid DataGrid2Excel,string FileName,string Title, string Head){System.IO.StringWriter sw = new System.IO.StringWriter();System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);FrontDecorator(hw);if ( Title != "")hw.Write(Title + "<br>");if ( Head != "")hw.Write(Head + "<br>");DataGrid2Excel.EnableViewState = false;DataGrid2Excel.RenderControl(hw);RearDecorator(hw);System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;response.Clear();response.Buffer = true;response.ContentEncoding = System.Text.Encoding.Default;response.ContentType ="application/Excel";response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls");response.Charset = "gb2312";response.Write(sw.ToString());response.End();}

 


 

转载于:https://www.cnblogs.com/chenshulin01/p/3296486.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/575782.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

pdftk的使用介绍

首先像下面的一页pdf&#xff0c;如果想把它分成两页&#xff0c;每一页只是一个ppt页面&#xff08;为了在kindle里读比较方便&#xff09;&#xff0c; 那么可以首先用A-pdf page cut, 将pdf 切成这样12个部分 然后我们现在要的只是第5和第8部分。 如何实现只要弟5和第8部分呢…

java throws catch_java中throws与try...catch的区别点

throws是将异常抛出&#xff0c;后续代码不再执行。而try…catch是将异常抛出&#xff0c;并且要继续执行后面的代码。package com.oracle;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class Demo01Exception {/*Excepti…

要提升asp.net工作能力。应急于提升的是哪些技术?

A:使用Web服务去解决分布式开发所遇到的一些共同的问题&#xff0c;以及使用Microsoft .NET WCF有效地建立可扩缩的、分布式的应用程序所需的知识和技能&#xff0c;及 通过使用Microsoft Visual Studio .NET 2008,UDDI使学员能够去构造、开发、使用XML Web Service&#xff0c…

java 多线程 notify_Java多线程8:wait()和notify()/notifyAll()

轮询线程本身是操作系统中独立的个体&#xff0c;但是线程与线程之间不是独立的个体&#xff0c;因为它们彼此之间要相互通信和协作。想像一个场景&#xff0c;A线程做int型变量i的累加操作&#xff0c;B线程等待i到了10000就打印出i&#xff0c;怎么处理&#xff1f;一个办法就…

详解http和https的区别

详解http和https的区别 &#xff08;转自http://www.jzxue.com/jianzhanzhinan/jianzhanrumen/201103/29-6925.html&#xff09; http与https有什么区别呢&#xff1f;本文详解http和https的区别。 只要上过网的朋友一定接触过“HTTP”&#xff0c;每次开网页的 时候&#xff0…

ntlm java_深讨Java SE 6 在 HTTP 方面的新特性(NTLM)

概述Java 语言从诞生的那天起&#xff0c;就非常注重网络编程方面的应用。随着互联网应用的飞速发展&#xff0c;Java 的基础类库也不断地对网络相关的 API 进行加强和扩展。在 Java SE 6 当中&#xff0c;围绕着 HTTP 协议出现了很多实用的新特性&#xff1a;NTLM 认证提供了一…

76Byte让你的JQuery更快

原文链接&#xff1a;http://james.padolsey.com/javascript/76-bytes-for-faster-jquery/ When jQuery fires a callback function, whether it is an event handler, an each iterator, or a filter function, it will normally give you a DOM element as the function’s c…

价差 量差

这两个概念属于成本控制的范畴 成本控制有广义和狭义之分&#xff1b; 广义&#xff1a;生产经营各个环节和各个方面全过程的限制 狭义&#xff1a;生产阶段产品成本控制 标准成本就是通过一些方法制定的在有效的经营条件下应该实现的成本&#xff0c;根据产品的耗费标准和耗费…

java response 输出流_java-springmvc+filter 替换输出流、response、响应内容

java-springmvcfilter 替换输出流、response、响应内容一、问题1.描述&#xff1a;在使用 filter 替换、修改 response 输出内容时常见的错误如下异常提示getWriter() has already been called for this responsegetOutputStream() has already been called for this response2…

c# 关于WebBrowser的模拟提交InvokeMember方法是什么意思!

一开始接触InvokeMember方法我以为他就是向页面插入javascript脚本&#xff01;然后我想找系统帮我插入的这个脚本&#xff0c;不过找不到&#xff01;&#xff0c;因为我不理解模拟这个词&#xff01;哈哈 其实呢&#xff0c;不是这样子的&#xff01; InvokeMember("cli…

java继续_Java中消除实现继续和面向接口编程

在匆忙之际理清消除实现继续和面向接口编程这样两个大题目可不是一件轻易的事情&#xff0c;尤其考虑到自身的熟悉水平。坦白的说&#xff0c;这又是一篇“炒冷饭”的文章&#xff0c;但这“冷饭”又确实不好炒。因此&#xff0c;在阅读了这篇文章之后&#xff0c;你可要批判地…

《转》VC++多线程编程

原地址&#xff1a;http://www.cnblogs.com/wxfasdic/archive/2010/09/23/1833522.html留个纪念&#xff0c;不错的总结。十个例子清晰列举啦多线程编程的奥妙。 VC中多线程使用比较广泛而且实用,在网上看到的教程.感觉写的挺好. 一、问题的提出编写一个耗时的单线程程序&#…

array函数参数 scala_scala – 在Spark SQL中将数组作为UDF参数传递

很可能不是最漂亮的解决方案,但你可以尝试这样的事情&#xff1a;def getCategory(categories: Array[String]) {udf((input:String) > categories(input.toInt))}df.withColumn("newCategory", getCategory(myArray)(col("myInput")))您还可以尝试一系…

Java数据类型BooleanDemo

转载于:https://www.cnblogs.com/suncoolcat/p/3320306.html

beetle.java 分析_Beetl模板引擎入门教程

最近项目中有个邮件发送的需求&#xff0c;不过要求发送的HTML格式的邮件。由于Beetl对java语言的良好支持和很好的性能&#xff0c;我们决定使用Beetl作为我们的模板引擎。Beetl官网已经有了很详细的教程&#xff0c;所以本篇侧重于实战应用&#xff0c;适合需要不懂beetl或其…

WebScoket 规范 + WebSocket 协议

WebSocket握手协议 1、客户端握手请求&#xff08;注意&#xff1a;键值之间有一个空格,行间有换行符号0x13x10或者说\r\n&#xff09; GET /WebSocket/LiveVideo HTTP/1.1 Upgrade: WebSocket Connection: Upgrade Host: localhost:8080 (客户端请求主机) Origin:…

heap python_数据结构-堆(Heap) Python实现

堆(Heap)可以看成近似完全二叉树的数组&#xff0c;树中每个节点对应数组中一个元素。除了最底层之外&#xff0c;该树是完全充满的&#xff0c;最底层是从左到右填充的。堆包括最大堆和最小堆&#xff1a;最大堆的每一个节点(除了根结点)的值不大于其父节点&#xff1b;最小堆…

多个 App 间启动

http://developer.nokia.com/: URI associations for Windows Phone http://msdn.microsoft.com/: Auto-launching apps using file and URI associations for Windows Phone 8 代码示例转载于:https://www.cnblogs.com/sirkevin/p/3325035.html

im4java 文档_im4java学习---阅读documentation文档

Utilities----im提供的一些工具类①、读取图片文件信息---Info类我们之前的做法&#xff1a;op.format("width:%w,height:%h,path:%d%f,size:%b%[EXIF:DateTimeOriginal]");IdentifyCmd identifyCmd new IdentifyCmd(useGM);使用工具类Info&#xff1a;Info imageIn…

函数体中定义的结构体和类型

源代码&#xff1a; 1 #include <stdio.h>2 struct smonth // point 13 {4 int a;5 int b;6 };7 8 int func1()9 { 10 struct smonth{ 11 int a; 12 int b; 13 }; 14 15 ty…