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

 更新时间:2015年03月25日 15:07:53   作者:lele  
这篇文章主要介绍了C#导出GridView数据到Excel文件类,实例分析了C#使用GridView及Excel的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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#程序设计有所帮助。

相关文章

  • Avalonia封装实现指定组件允许拖动的工具类

    Avalonia封装实现指定组件允许拖动的工具类

    这篇文章主要为大家详细介绍了Avalonia如何封装实现指定组件允许拖动的工具类,文中的示例代码讲解详细,感兴趣的小伙伴快跟随小编一起来学习学习吧
    2023-03-03
  • WPF使用FontAwesome字体图标

    WPF使用FontAwesome字体图标

    这篇文章介绍了在WPF中使用FontAwesome字体图标的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • C# 抓图服务的实现

    C# 抓图服务的实现

    这篇文章主要介绍了C# 抓图服务的实现,帮助大家更好的利用c#处理窗口,进行截图,感兴趣的朋友可以了解下
    2021-01-01
  • c#字符串编码编码(encoding)使用方法示例

    c#字符串编码编码(encoding)使用方法示例

    System.Text提供了Encoding的抽象类,这个类提供字符串编码的方法。使Unicode字符数组的字符串,转换为指定编码的字节数组,或者反之,看下面的例子
    2013-12-12
  • C#集合本质之队列的用法详解

    C#集合本质之队列的用法详解

    本文详细讲解了C#集合本质之队列的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • 深入c# GDI+简单绘图的具体操作步骤(一)

    深入c# GDI+简单绘图的具体操作步骤(一)

    本篇文章是对GDI的基础知识进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • 一篇文章带你轻松了解C# Lock关键字

    一篇文章带你轻松了解C# Lock关键字

    这篇文章主要给大家介绍了如何通过一篇文章带你轻松了解C# Lock关键字的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-06-06
  • c# 垃圾回收(GC)优化

    c# 垃圾回收(GC)优化

    这篇文章主要介绍了c# 垃圾回收(GC)优化的相关资料,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下
    2021-02-02
  • 使用checked语句防止数据溢出的解决方法

    使用checked语句防止数据溢出的解决方法

    本篇文章是对用checked语句防止数据溢出的解决方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • winform简单缓存类实例

    winform简单缓存类实例

    这篇文章主要介绍了winform简单缓存类,涉及C#缓存使用技巧,非常简单实用,需要的朋友可以参考下
    2015-09-09

最新评论