调用 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. MySQL的存储引擎

    MySQL的一个重要特性就是支持插件式存储引擎.其存储引擎接口定义良好,有兴趣的开发者可以通过阅读文档编写自己的存储引擎. 接下来简单介绍一下MySQL的常见存储引擎: 一.MyISAM MySQL ...

  2. JavaScript学习(一)—处理事件2

    下面的表中为最常用的 12 种 JavaScript 事件处理程序. 一.处理窗口事件(onload事件,onunload事件,onresize事件,onmove事件,onabort事件,onerro ...

  3. PTA Insertion or Heap Sort

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  4. SSH的端口转发:本地转发Local Forward和远程转发Remote Forward

    关于使用ssh portforwarding来进行FQ的操作,网络上已经有很多很好的文章,我在这里只是画两个图解释一下. 首先要记住一件事情就是: SSH 端口转发自然需要 SSH 连接,而 SSH ...

  5. 关于Intent的七大重要属性

    在Android 中,Intent用来封装两个Activity之间的调用意图,实现两个Activity之间的跳转,并传递信息. Intent的七大重要属性:ComponentName Action   ...

  6. [题解]vijos & codevs 能量项链

    a { text-decoration: none; font-family: "comic sans ms" } .math { color: gray; font-family ...

  7. (转)SQL Server 性能调优(cpu)

    摘自:http://www.cnblogs.com/Amaranthus/archive/2012/03/07/2383551.html 研究cpu压力工具 perfom SQL跟踪 性能视图 cpu ...

  8. mysql 用命令操作

    本篇文章来源于http://c.biancheng.net/cpp/html/1441.html mysql:连接数据库 mysql命令用户连接数据库. mysql命令格式: mysql -h主机地址 ...

  9. [Notes] AWS Automation using script and AWS CLI

    (c) 2014 Amazon Web Services, Inc. and its afflialtes, All rights reserved. The content in this file ...

  10. SEO和SEM的区别

    SEO是属于SEM的一部分,SEO和SEM最主要的是最终目标的不同: SEO主要是为了关键词的排名.网站的流量.网站的结构.搜索引擎中页面收录的数据. SEM是通过SEO技术基础上扩展为搜索引擎中所带 ...