• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.netdatatable导出为txt

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
如下是导出到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();
		}

 


 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap