Cc export GridView data to excel file class

From , 5 Years ago, written in C#, viewed 157 times.
URL https://pastebin.vip/view/c42f76f3
  1. using System;
  2. using System.Web;
  3. using System.Web.UI;
  4. using System.IO;
  5. using System.Web.UI.WebControls;
  6.  
  7. namespace DotNet.Utilities
  8. {
  9.     public class ExportExcel
  10.     {
  11.  
  12.         protected void ExportData(string strContent, string FileName)
  13.         {
  14.  
  15.             FileName = FileName + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
  16.  
  17.             HttpContext.Current.Response.Clear();
  18.             HttpContext.Current.Response.Charset = "gb2312";
  19.             HttpContext.Current.Response.ContentType = "application/ms-excel";
  20.             HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
  21.             //this.Page.EnableViewState = false;
  22.             // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
  23.             HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls");
  24.             // 把文件流发送到客户端
  25.             HttpContext.Current.Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
  26.             HttpContext.Current.Response.Write(strContent);
  27.             HttpContext.Current.Response.Write("</body></html>");
  28.             // 停止页面的执行
  29.             //Response.End();
  30.         }
  31.  
  32.         /// <summary>
  33.         /// 导出Excel
  34.         /// </summary>
  35.         /// <param name="obj"></param>
  36.         public void ExportData(GridView obj)
  37.         {
  38.             try
  39.             {
  40.                 string style = "";
  41.                 if (obj.Rows.Count > 0)
  42.                 {
  43.                     style = @"<style> .text { mso-number-format:\@; } </script> ";
  44.                 }
  45.                 else
  46.                 {
  47.                     style = "no data.";
  48.                 }
  49.  
  50.                 HttpContext.Current.Response.ClearContent();
  51.                 DateTime dt = DateTime.Now;
  52.                 string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString();
  53.                 HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=ExportData" + filename + ".xls");
  54.                 HttpContext.Current.Response.ContentType = "application/ms-excel";
  55.                 HttpContext.Current.Response.Charset = "GB2312";
  56.                 HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
  57.                 StringWriter sw = new StringWriter();
  58.                 HtmlTextWriter htw = new HtmlTextWriter(sw);
  59.                 obj.RenderControl(htw);
  60.                 HttpContext.Current.Response.Write(style);
  61.                 HttpContext.Current.Response.Write(sw.ToString());
  62.                 HttpContext.Current.Response.End();
  63.             }
  64.             catch
  65.             {
  66.             }
  67.         }
  68.     }
  69. }
  70. //csharp/8624

Reply to "Cc export GridView data to excel file class"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website