//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. mobx react

    目录结构: Model/index.js 'use strict'; import { action, autorun, observable, computed } from "mobx& ...

  2. JS实现定时器(类似工行网银支付限时操作)

      js脚本内容: //5秒倒计时 var num = 0 ; var max = 5 ; var id = null ; id = setInterval(box , 1000) ; //1秒钟调用 ...

  3. 解决animate动画连续播放bug

    在animate动画中,如果几个div之间频繁切换,会导致鼠标移开后,动画仍在继续,解决方法有两个 一个,判断当前是否在运行动画: if(!$(".block").is(" ...

  4. 【Ecstore2.0】计划任务/队列/导入导出 的执行问题

    [环境]CENTOS6.3 + wdcp(php5.3) [症状]可正常加入队列,但不执行队列 [原因]大部份都是用户权限造成 [原理] Ecstore2.0的导入导出.发送邮件.日常清理备份等任务操 ...

  5. win7开机密码忘记了

    开机到欢迎界面时,出现输入用户名密码的提示框,按Ctrl+Alt+Delete,跳出帐号窗口,输入用户名:administrator,回车即可. 如果这个administrator帐号也有密码,那么可 ...

  6. MBProgressHUD的基本使用

    MBProgressHUD的基本使用 分类: IOS2012-10-30 11:19 12047人阅读 评论(2) 收藏 举报 和gitHub上的Demo其实差不多,就是小整理了下,当备忘,想做复杂的 ...

  7. LINUX下为LVM磁盘增加硬盘空间

    总结: ~~~~~~~~~~~~~~~~~~~~ fdisk -lpvcreate /dev/sdbvgextend VolGroup /dev/sdblvextend -L +180G /dev/m ...

  8. 一本QT书,连接MySQL图文并茂

    http://qtdebug.com/index.html http://qtdebug.com/DB-AccessMySQL.html

  9. Android比较字符串是否为空(isEmpty)

    StringUtils.java: package com.yx.equipment_collection.utils; import android.annotation.SuppressLint; ...

  10. Yii 引入 js和css

    //向控制器指定的页面注册新的css文件 Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/my ...