//gv:需要导出数据的GridView,filename:导出excel文件名
public void ExportToExcel(GridView gv, string filename)
{
string style = @"<style> .text { mso-number-format:\@; } </style> "; Response.ClearContent();
HttpContext.Current.Response.Charset = "UTF8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;//注意编码
Response.AddHeader("content-disposition", "attachment; filename=" + filename);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.PageSize = Int16.MaxValue;
//导出之前一定要再次绑定数据源,不然导出无数据
gv.DataSource = getGridViewData();
gv.DataBind();
gv.RenderControl(htw);
// Style is added dynamically
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
} //必须加上这个函数,函数中没有任何内容只是重载一下,不然会报错:... type 'GridView' must be placed inside a form tag with runat=server.
public override void VerifyRenderingInServerForm(Control control)
{
} //可以在每行数据绑定的时候设置数据格式
//文本:vnd.ms-excel.numberformat:@
//日期:vnd.ms-excel.numberformat:yyyy/mm/dd
//数字:vnd.ms-excel.numberformat:#,##0.00
//百分比:vnd.ms-excel.numberformat: #0.00%
protected void IBDetailGridView_RowDataBound(object sender, GridViewRowEventArgs e) {
   if (e.Row.RowIndex > -1)
   {
     e.Row.Cells[7].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
     e.Row.Cells[8].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
  }
}
  

  

将GridView中的数据导出到Excel代码与注意事项的更多相关文章

  1. Gridview中的数据导出到excel中

    protected void btnExport_Click(object sender, EventArgs e)    {                   //导出全部数据,取消分页      ...

  2. 机房收费系统——在VB中将MSHFlexGrid控件中的数据导出到Excel

    机房收费系统中,好多查询的窗体都包含同一个功能:将数据库中查询到的数据显示在MSHFlexGrid控件中,然后再把MSHFlexGrid控件中的数据导出到Excel表格中. 虽然之前做过学生信息管理系 ...

  3. Qt中将QTableView中的数据导出为Excel文件

    如果你在做一个报表类的程序,可能将内容导出为Excel文件是一项必须的功能.之前使用MFC的时候我就写过一个类,用于将grid中的数据导出为Excel文件.在使用了QtSql模块后,我很容易的将这个类 ...

  4. 将Datagridview中的数据导出至Excel中

        首先添加一个模块ImportToExcel,并添加引用         然后导入命名空间: Imports Microsoft.Office.Interop Imports System.Da ...

  5. WPF-将DataGrid控件中的数据导出到Excel

    原文:WPF-将DataGrid控件中的数据导出到Excel 导出至Excel是非常常见,我们可以用很多类库,例如Aspose.NOPI.Interop,在这里我们使用微软自家的工具.我的WPF绑定的 ...

  6. 如何将存储在MongoDB数据库中的数据导出到Excel中?

    将MongoDB数据库中的数据导出到Excel中,只需以下几个步骤: (1)首先,打开MongoDB安装目录下的bin文件夹,(C:\Program Files (x86)\MongoDB\Serve ...

  7. Asp.net网页中DataGridView数据导出到Excel

    经过上网找资料,终于找到一种可以直接将GridView中数据导出到Excel文件的方法,归纳方法如下: 1. 注:其中的字符集格式若改为“GB2312”,导出的部分数据可能为乱码: 导出之前需要关闭分 ...

  8. C#将数据集DataSet中的数据导出到EXCEL文件的几种方法

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.W ...

  9. html5中 table数据导出到excel文件

    JS代码: /** * table数据导出到excel * 形参 table : tableId ; * sheetName : 工作薄名 * fileName : 文件名 * linkId :隐藏的 ...

随机推荐

  1. QT5-控件-QScrollArea-可以用于把一个窗口分割为多个-比如根据图片大小显示滚动条

    #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QLabel> #incl ...

  2. [转]extern,static存储空间矛盾

    其实,这两个语句的位置不同,会出现不同的解释.这主要是由于 static 具有的两重意义所导致的: (1) 如果 static int foo; 这一句位于函数中,则 static 表示的是存储属性, ...

  3. 记录Linux下安装elasticSearch时遇到的一些错误

    记录Linux下安装elasticSearch时遇到的一些错误 http://blog.sina.com.cn/s/blog_c90ce4e001032f7w.html (2016-11-02 22: ...

  4. Postman 安装及使用入门教程(转)

    安装 本文只是基于 Chrome 浏览器的扩展插件来进行的安装,并非单独应用程序. 首先,你要台电脑,其次,安装有 Chrome 浏览器,那你接着往下看吧. 1. 官网安装(别看) 打开官网,http ...

  5. nginx_笔记分享_配置篇

    参考http://www.howtocn.org/nginx:directiveindexhttp://blog.s135.com/ nginx 配置文档为 nginx.conf 比如我的配置文档 / ...

  6. 使用scrapy制作的小说爬虫

    使用scrapy制作的小说爬虫 爬虫配套的django网站  https://www.zybuluo.com/xuemy268/note/63660 首先是安装scrapy,在Windows下的安装比 ...

  7. Java比较两个日期的大小

    public static String getComparedSBQDate(String yxqq,String starttime){ String str = starttime; Simpl ...

  8. IE 将“通过域访问数据源”设置为启用(注册表)

    XP HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1\1406 Vista+ HKCU\Softwar ...

  9. SQL 从100万条记录中的到 成绩最高的记录

    从100万条记录中的到 成绩最高的记录 问题分析:要从一张表中找到成绩最高的记录并不难,有很多种办法,最简单的就是利用TOP 1 select top 1 * from student order b ...

  10. 面试问题:关于java并发方面的

    主要是回答一下面试中可能会碰到的问题.慢慢的积累一下.一个星期以后,补全.