调用 ExportGridViewToExcel(dt, HttpContext.Current.Response);

private void ExportGridViewToExcel(DataTable tb, HttpResponse response)
{

response.Clear();
response.Charset = "";
response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
response.ContentEncoding = System.Text.Encoding.UTF8;//System.Text.Encoding.GetEncoding("GB2312");
response.ContentType = "application/ms-excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

GridView newDataGrid = new GridView();
newDataGrid.EnableViewState = false;
newDataGrid.AllowSorting = false;
newDataGrid.GridLines = GridLines.Vertical;
newDataGrid.HeaderStyle.Font.Bold = true;

newDataGrid.DataSource = tb.DefaultView;
newDataGrid.DataBind();
newDataGrid.RenderControl(htmlTextWriter);
response.Write(stringWriter.ToString());
}

gridview汇出EXCEL (ExportGridViewToExcel(dt, HttpContext.Current.Response);)的更多相关文章

  1. GridView导出Excel的超好样例

    事实上网上有非常多关于Excel的样例,可是不是非常好,他们的代码没有非常全,读的起来还非常晦涩.经过这几天的摸索,最终能够完毕我想要导出报表Excel的效果了.以下是我的效果图. 一.前台的页面图 ...

  2. C#实现GridView导出Excel

    using System.Data;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System. ...

  3. C# GridView 导出Excel表

    出错1:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内解决方案:在后台文件中重载VerifyRenderingInServerForm方法,如 ...

  4. ASP.NET gridview导出excel,防止繁体产生有乱码的方式

    //1.先引用比如 : using System; using System.Collections.Generic; using System.Linq; using System.Web; usi ...

  5. GridView导出Excel(中文乱码)

    public void OUTEXCEL(string items,string where) { DataSet ds = new StudentBLL().GetTable(items,where ...

  6. gridview导excel及解决导出中文乱码的方法

    要全部展示数据,所以导出时要先将翻页取消,重新绑定数据:gridview的allowpaging属性设置为false,然后databind()一下 <%@ Page Language=" ...

  7. 慎用System.Web.HttpContext.Current

    每当控制流离开页面派生的Web表单上的代码的时候,HttpContext类的静态属性Current可能是有用的. 使用这个属性,我们可以获取当前请求(Request),响应(Response),会话( ...

  8. Response.End(); 用HttpContext.Current.ApplicationInstance.CompleteRequest 代替

    Response.End(); 会报异常 HttpContext.Current.ApplicationInstance.CompleteRequest 这里有个讨论的帖子很有意思:http://q. ...

  9. Context.Response.End(); VS HttpContext.Current.ApplicationInstance.CompleteRequest();

    今天遇到一個問題,頁面Client端send一個ajax請求,然後在server端返回一個json的字符串 $.ajax({ url: "xxxxx.aspx", type: &q ...

随机推荐

  1. centos7安装apue.3e时出错处理

    错误代码如下: /tmp/ccb9gvom.o: In function `thr_fn': barrier.c:(.text+0x6e): undefined reference to `heaps ...

  2. sizeof 跟 strlen 的区别

    1.参数 sizeof是c/c++ 中的一个操作符,其作用是返回对象或数据类型所占的内存字节数. 用法:sizeof(对象).sizeof 对象.sizeof(类型) 如果类型做参数,返回的是该类型所 ...

  3. 枚举全排列(包括数列中有重复数)的C语言实现

    据说是用了DFS的思想--然鹅并不知道这是DFS. 主要就是选取一个数放到数组相应位置上,然后递归的排列剩下的数组,将剩下的数组递归排列完了之后再把数放回去,然后这一层递归就返回了-- 有重复数的话遇 ...

  4. SQL Server 通用分页存储过程

    create proc commonPagination ), --要显示的列名,用逗号隔开 ), --要查询的表名 ), --排序的列名 ), --排序的方式,升序为asc,降序为 desc ), ...

  5. 安装好grunt,cmd 提示"grunt不是内部或外部命令" 怎么办?

    Grunt和所有grunt插件都是基于nodejs来运行的,因此,必须安装node.js. (一) 去官网http://nodejs.org/ 下载安装包 node-v6.9.2.msi,直接点击安装 ...

  6. C入门---位运算

    程序中的所有数在计算机内存中都是以二进制的形式储存的.位运算直接对整数在内存中的二进制位进行操作.由于位运算直接对内存数据进行操作,不需要转成十进制,因此处理速度非常快. (1),与(&)运算 ...

  7. java中switch、while、do...while、for

    一.Java条件语句之 switch 当需要对选项进行等值判断时,使用 switch 语句更加简洁明了.例如:根据考试的名次,给予前 4 名不同的奖品.第一名,奖励笔记本一台:第二名,奖励 IPAD  ...

  8. ItextDemo<二>

    Nested tables TableTemplate.java /** * Example written by Bruno Lowagie in answer to the following q ...

  9. Jquery动态在td中添加checkbox

    如图:想要在这个id为headId的<td>中,用jquery动态添加checkbox 代码如下 : data是我用ajax 从后台获取的数据,里面含有若干个user类,我想把所有的人名字 ...

  10. [LeetCode][Java]Triangle@LeetCode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...