码迷,mamicode.com
首页 > 其他好文 > 详细

Excel 导出的方法 之二

时间:2014-05-27 00:44:33      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:c   tar   http   ext   a   int   

 

 

// <summary>
/// 导出到Excel lichenghu
/// </summary>
/// <param name="dt"></param>
public static void ToExcel(DataTable dt)
{
string sb = "";

foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
sb = sb + dr[i].ToString() + "\t";
}
sb = sb + "\n";
}

HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=MyExcel.xls");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";

System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
hw.WriteLine(sb.ToString());
HttpContext.Current.Response.Write(tw.ToString());

HttpContext.Current.Response.End();

hw.Flush();
hw.Close();
tw.Flush();
tw.Close();
}

Excel 导出的方法 之二,布布扣,bubuko.com

Excel 导出的方法 之二

标签:c   tar   http   ext   a   int   

原文地址:http://www.cnblogs.com/chenghu/p/3736330.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!