加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#导出GridView数据到Excel文件类实例

发布时间:2020-12-15 05:58:36 所属栏目:百科 来源:网络整理
导读:本篇章节讲解C#导出GridView数据到Excel文件类。供大家参考研究。具体如下: 这段C#代码自定义了一个封装类,用于将GridView数据导出到Excel文件 using System;using System.Web;using System.Web.UI;using System.IO;using System.Web.UI.WebContro

本篇章节讲解C#导出GridView数据到Excel文件类。分享给大家供大家参考。具体如下:

这段C#代码自定义了一个封装类,用于将GridView数据导出到Excel文件

using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
namespace DotNet.Utilities
{
  public class ExportExcel
  {
    protected void ExportData(string strContent,string FileName)
    {
      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();
      HttpContext.Current.Response.Clear();
      HttpContext.Current.Response.Charset = "gb2312";
      HttpContext.Current.Response.ContentType = "application/ms-excel";
      HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
      //this.Page.EnableViewState = false;
      // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
      HttpContext.Current.Response.AddHeader("Content-Disposition","attachment; filename=" + FileName + ".xls");
      // 把文件流发送到客户端
      HttpContext.Current.Response.Write("<html><head><meta http-equiv=Content-Type content="text/html; charset=utf-8">");
      HttpContext.Current.Response.Write(strContent);
      HttpContext.Current.Response.Write("</body></html>");
      // 停止页面的执行
      //Response.End();
    }
    /// <summary>
    /// 导出Excel
    /// </summary>
    /// <param name="obj"></param>
    public void ExportData(GridView obj)
    {
      try
      {
        string style = "";
        if (obj.Rows.Count > 0)
        {
          style = @"<style> .text { mso-number-format:@; } </script> ";
        }
        else
        {
          style = "no data.";
        }
        HttpContext.Current.Response.ClearContent();
        DateTime dt = DateTime.Now;
        string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString();
        HttpContext.Current.Response.AddHeader("content-disposition","attachment; filename=ExportData" + filename + ".xls");
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        HttpContext.Current.Response.Charset = "GB2312";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        obj.RenderControl(htw);
        HttpContext.Current.Response.Write(style);
        HttpContext.Current.Response.Write(sw.ToString());
        HttpContext.Current.Response.End();
      }
      catch
      {
      }
    }
  }
}

希望本文所述对大家的C#程序设计有所帮助。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读