Excel导出到浏览器(个人备份)

  /// <summary>
  /// Excel导出
  /// </summary>
  /// <param name="dt"></param>
  /// <returns></returns>
  public static void DataTableToExcel(DataTable dt, string ExcleName)
  {

   if (dt != null && dt.Rows.Count > 0)
   {
    MemoryStream ms = ExcelHelper.DataTableToExcel(dt);
    string fileName = ExcleName +
          HttpUtility.UrlEncode(DateTime.Now.ToString("yyyyMMddHHmmssfff"), System.Text.UTF8Encoding.UTF8);
    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
    HttpContext.Current.Response.AddHeader("Content-Disposition",
     String.Format("attachment;filename={0}", fileName + ".xls"));
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.BinaryWrite(ms.GetBuffer());
    HttpContext.Current.ApplicationInstance.CompleteRequest();
   }
  }

posted @ 2017-03-21 17:07  陈立扬  阅读(310)  评论(0编辑  收藏  举报
友情链接:回力球鞋 | 中老年服装 | 新航道英语 | 托福网课 | 雅思培训班