在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. 用virtualenv建立多个Python独立开发环境(转)

    add by zhj: 在virtualenv环境下,安装第三方包时,不要用sudo pip install xxx,要直接用pip install xxx,如果用sudo时,那会安装在原来的系统Py ...

  2. Day27-28 基础加强

    day28基础加强 今日内容 泛型 注解 Servlet3.0 动态代理 类加载器   泛型 回顾泛型类 泛型类:具有一个或多个泛型变量的类被称之为泛型类. public class A<T&g ...

  3. 阿里云centos7.2 安装mysql 6.5

    [root@111 ~]# uname -aLinux bxhs 3.10.0-327.22.2.el7.x86_64 #1 SMP Thu Jun 23 17:05:11 UTC 2016 x86_ ...

  4. Replication--复制与CDC和镜像

    复制和CDC 都是使用logreader来从日志中读取数据的变更,然后写入到分发库(复制)或变更表中. 1> 单独创建复制发布 在该情况下,会在分发服务器上创建日志读取代理作业(ServerNa ...

  5. 【转】Deep Learning(深度学习)学习笔记整理系列之(四)

    九.Deep Learning的常用模型或者方法 9.1.AutoEncoder自动编码器 Deep Learning最简单的一种方法是利用人工神经网络的特点,人工神经网络(ANN)本身就是具有层次结 ...

  6. js判断浏览器的类型和获得浏览器的版本

    <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">      ...

  7. 002-mybatis主配置文件

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC & ...

  8. python webdriver 测试框架-数据驱动xml驱动方式

    数据驱动xml驱动的方式 存数据的xml文件:TestData.xml: <?xml version="1.0" encoding="utf-8"?> ...

  9. Mindoc搭建流程

    1,安装MySql 2,官网下载:https://github.com/lifei6671/mindoc/releases mindoc_windows_amd64.7z 3,如果下载的压缩包中不存在 ...

  10. stm32 Flash读写[库函数]

    stm32 的内置可编程Flash在许多场合具有十分重要的意义.如其支持ICP特性使得开发人员对stm32可以警醒调试开发,可以通过JTAG和SWD接口对stm32进行程序烧写:支持IAP特性使得开发 ...