在ASP.NET中将GridView数据导出到Word、Excel

asp.net,导出gridview数据到Word,Excel,PDF
 

#region Export to Word, Excel and PDF
    protected void btnword_Click(object sender, EventArgs e)
    {
        Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.word";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

// Create a form to contain the grid
        HtmlForm frm = new HtmlForm();
        GridView4.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(GridView4);
        frm.RenderControl(htmlWrite);

//GridView1.RenderControl(htw);
        Response.Write(stringWrite.ToString());
        Response.End();
    }
    protected void btnexcel_Click(object sender, EventArgs e)
    {
        string attachment = "attachment; filename=Contacts.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

// Create a form to contain the grid
        HtmlForm frm = new HtmlForm();
        GridView1.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(GridView1);
        frm.RenderControl(htw);

//GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }
    protected void btnpdf_Click(object sender, EventArgs e)
    {
        Response.Clear();

StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.RenderControl(htw);

Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment; filename=MypdfFile.pdf");
        Response.Write(sw.ToString());
        Response.End();
    }

在ASP.NET中将GridView数据导出到Word、Excel的更多相关文章

  1. ASP.NET中GridView数据导出到Excel

    /// <summary> /// 导出按钮 /// </summary> /// <param name="sender"></para ...

  2. gridview数据导出到word和excel以及excel的导入

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  3. 【asp.net】将GridView数据导出Excel

    概要: 中午睡了一会,醒来的时候看到老师叫我去办公室,需求是这样的,把excel表中的每个同学,判断图片目录中是否有对应的照片(图片的名字用的学号或身份证号码) 没有对应图片的学生记录,存入自己的数据 ...

  4. C# DevExpress 的gridControl或gridView数据导出失败解决方法

    来自:http://blog.csdn.net/lybwwp/article/details/8049464 谢谢 在使用DevExpress 的GridPanel控件的时候出现了一个莫名其妙的现象, ...

  5. Gridview数据导出到ExcelWord 防止出现乱码

    1.页面中添加绿色字体代码<%@ Page Language="C#" CodeFile="111.aspx.cs" Inherits="111 ...

  6. Gridview数据导出excel时身份证号码为科学计数法的解决方法

    if (e.Row.RowType == DataControlRowType.DataRow) { string id = this.GridView1.DataKeys[e.Row.RowInde ...

  7. 使用NPOI将数据导出为word格式里的table

    开发环境:VS2013+MySQL5.5+EF6+NPOI2.0.6 格式:WinForm+CodeFirst PS:vs2013的CodeFirst很方便了啊 CodeFirst方式就不再赘述了. ...

  8. Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,htm

    Ireport 报表导出 Poi + ireport 导出pdf, doc ,excel ,html 格式 下面是报表导出工具类reportExportUtils 需要导出以上格式的报表 只需要调用本 ...

  9. C#中将ListView数据导出为excel(转载)

      首先引用excel库,下面我把代码贴出来.                 /// <summary>        /// 将ListView的内容写入Excel表中         ...

随机推荐

  1. UChome Feed 机制

    Feed,本意是“饲料.饲养.(新闻的)广播等”. 我们就拿用户发表日志这个动作来简单看看Uchome的feed机制. 用户发布日志所使用的函数是 source/function_blog.php文件 ...

  2. PAT 1075 PAT Judge[比较]

    1075 PAT Judge (25 分) The ranklist of PAT is generated from the status list, which shows the scores ...

  3. Gitlab汉化为中文版

    查看当前的gitlab版本号 cat /opt/gitlab/embedded/service/gitlab-rails/VERSION 11.1.4 打开这个网址:https://gitlab.co ...

  4. sqlnet.ora的作用

    sqlnet.ora的作用 1.限制客户端访问(如指定客户端域为不允许访问) 2.指定命名方法(local naming,directory nameing...)的优先级 3.启用日志及跟踪(log ...

  5. 4.3 Routing -- Generated Objects

    就像在routing guide中介绍的那样,不管什么时候你在路由器中定义一个新路径,Ember.js就会尝试寻找一个对应的route,controller,template,它们的命名都是根据命名约 ...

  6. bzoj1009 / P3193 [HNOI2008]GT考试

    P3193 [HNOI2008]GT考试 设$f[i][j]$表示主串匹配到第$i$个位置,不吉利数字匹配到第$j$个位置 $g[i][j]$表示加上某数字使子串原来最多能匹配到第$i$个数字,现在只 ...

  7. 如何让.gitignore文件生效

    改动过.gitignore文件之后,在repo的根目录下运行 # 先将当前仓库的文件的暂存区中剔除 git rm -r --cached . # 再添加所有的文件到暂存区,这时.gitignore文件 ...

  8. Vim编程常用命令

    1.全文覆盖 程序发布到测试.开发环境后,经常需要远程登录Linux更改代码.平时在IDE中直接Ctrl+A.Ctrl+V覆盖整个文档,在vim中需要这样做 vim filename gg --跳到首 ...

  9. 【熊猫TV】《程序员》:聚光灯下的熊猫TV技术架构演进

    2015年开始的百播大战,熊猫TV是其中比较特别的一员. 说熊猫TV是含着金钥匙出生的公子哥不为过.还未上线,就频频曝光,科技号,微博稿,站上风口浪尖.内测期间更是有不少淘宝店高价倒卖邀请码,光内测时 ...

  10. root权限和sudo得到权限的区别

    参考: 知乎 命令前加sudo执行和用真正的root用户执行有什么区别?pansz的回答 root用户和sudo使用root权限的区别 变换用户身份为root的方法su 与 sudo root权限和s ...