//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. Mysql 视图笔记2

    这学期开了数据库的课,对sql注入颇感兴趣.因此,对数据库语句也颇为喜爱.遇到了with check option 问题.这属于sql view里面的一个问题.在此略做小结.大牛勿喷! 先自定义一个t ...

  2. Convert String to Long

    问题: Given a string, write a routine that converts the string to a long, without using the built in f ...

  3. nginx实现负载均衡

    A服务器IP :192.168.5.149 (主) B服务器IP :192.168.5.27 C服务器IP :192.168.5.126 A服务器配置: 打开nginx.conf,文件位置在nginx ...

  4. Java解析和生成XML

    1.Jaxb处理java对象和xml之间转换常用的annotation有: @XmlType @XmlElement @XmlRootElement @XmlAttribute @XmlAccesso ...

  5. .net概述1

    1.什么是.net 首先我先说说这个词的读音,很多外行朋友读作"点net"甚至许多圈内朋友也这样读,其实它正确读法应该是读作"dot net",音译即为&quo ...

  6. Array and its point.

    a is the array name. &a is the ponit of 2-D array which contains a[5]. the type of &a should ...

  7. 信息安全实验二:return-to-libc

    title: return-to-libc date: 2016-01-11 17:40:30 categories: information-security tags: return-to-lib ...

  8. MemCache内存缓存系统

    memcached是一种缓存技术, 他可以把你的数据放入内存,从而通过内存访问提速,因为内存最快的, memcached技术的主要目的提速, 默认情况下占用的端口号为:11211. 在memachec ...

  9. PHP Java

    http://my.oschina.net/lajp/blog/5121 http://blog.163.com/lijianwei_123/blog/static/18489289120115244 ...

  10. Android开发程序获取GPS信息步骤

    1.获取LOCATION_SERVICE系统服务.2.创建Criteria对象,调用该对象的set方法设置查询条件.3.调用LocationManager.getBestProvider(Criter ...