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();
   }
  }

原文地址:https://www.cnblogs.com/chenliyang/p/6595420.html