首页 > 其他 > 详细

Excel 导出的方法 之二

时间:2014-05-27 00:43:50      阅读:399      评论:0      收藏:0      [点我收藏+]

 

 

// <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 导出的方法 之二

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!