<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="367px" CellPadding="4">
<Columns>
<asp:TemplateField HeaderText="编号">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="标题">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="内容">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("content") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="创建时间">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("createTime") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="类别编号">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("caId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="导出Excel" Width="109px"
onclick="Button1_Click" />
&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button2" runat="server" Text="导出Word" Width="86px"
onclick="Button2_Click" /> </form>
</body>
</html>

使用Gridview绑定数据库,绑定数据源。

后台:

首先引用命名空间

using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;

static string ConStr = ConfigurationManager.AppSettings["Constr"];
SqlConnection conn = new SqlConnection(ConStr);
protected void Page_Load(object sender, EventArgs e)
{
BindNews();
}
// Page.VerifyRenderingInServerForm 方法 就是验证服务器控件是否需要在<form runat=server></form>的标记之中,如果不在这个标记之中,将会引发下面的异常
//如果该页当前不处于页处理中的呈现阶段,且位于 <form runat=server> 标记内,则该方法将引发异常。需要位于服务器窗体内的控件可以在呈现期间调用该方法,以便在它们被放置到外面时显示明确的错误信息。发送回或依赖于注册的脚本块的控件应该在 Control.Render 方法的重写中调用该方法。呈现服务器窗体元素的方式不同的页可以重写该方法以在不同的条件下引发异常
//就必须重载VerifyRenderingInServerForm方法
public override void VerifyRenderingInServerForm(Control control)
{ }
void BindNews()
{
string sqlstr="select * from news";
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, conn);
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds.Tables[].Rows.Count > )
{ GridView1.DataSource = ds;
GridView1.DataBind();
} }
/// <summary>
/// 导出Excel(以流的方式进行编写所要得到的内容)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
//文档名称
string myfilename = "LLF" + DateTime.Now.ToString("yyyy-mm-dd-hh-mm-ss"); //申明一个hc 的类
HttpContext hc = HttpContext.Current;
//清空缓存
hc.Response.Clear();
//开启缓存
hc.Response.Buffer = true;
//设置编码格式(获取或设置输出流的字符集)Encoding(表示字符的编码)
hc.Response.ContentEncoding = Encoding.Default;
//设置头文件((AddHeader)将http的标头添加到输出流)
hc.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(myfilename, Encoding.Default) + ".xls");
//导出为Excel文件类型
hc.Response.ContentType = "application/ms-excel"; //申明文件写入流
StringWriter sw = new StringWriter();
//将服务器控件内的信息转义成HTML文件格式内容并存入到文件写入流
HtmlTextWriter htw = new HtmlTextWriter(sw);
//将数据库内的信息显示到GridView1上,同时在将将服务器控件内的信息转义成HTML文件格式
this.GridView1.RenderControl(htw);
//将文件写入流的信息输出到excel中
hc.Response.Write(sw.ToString());
//结束
hc.Response.End();
}
/// <summary>
/// 导出word
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
//文档名称
string myfilename = "fahui" + DateTime.Now.ToString("yyyyMMdd_hhmmss");
//申明一个hc的类
HttpContext hc = HttpContext.Current;
//清空缓存
hc.Response.Clear();
//开启缓存
hc.Response.Buffer = true;
//设置编码格式
hc.Response.ContentEncoding = Encoding.Default;
//设置头文件
hc.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(myfilename, Encoding.Default) + ".doc");
//导出为Excel文件类型
hc.Response.ContentType = "application/ms-word"; //申明文件写入流
StringWriter sw = new StringWriter();
//将服务器控件内的信息转义成HTML文件格式内容并存入到文件写入流
HtmlTextWriter htw = new HtmlTextWriter(sw);
//将数据库内的信息显示到GridView1上,同时在将将服务器控件内的信息转义成HTML文件格式
this.GridView1.RenderControl(htw);
//将文件写入流的信息输出到excel中
hc.Response.Write(sw.ToString());
//结束
hc.Response.End(); }

导出Excel And 导出word的更多相关文章

  1. 使用Apache POI导出Excel小结--导出XLS格式文档

    使用Apache POI导出Excel小结 关于使用Apache POI导出Excel我大概会分三篇文章去写 使用Apache POI导出Excel小结--导出XLS格式文档 使用Apache POI ...

  2. NPOI控件的使用导出excel文件和word文件

    public HttpResponseMessage GetReportRateOutput(DateTime? begin_time = null, DateTime? end_time = nul ...

  3. java导出Excel定义导出模板

    在很多系统功能中都会有Excel导入导出功能,小编采用JXLS工具,简单.灵活. JXLS是基于 Jakarta POI API 的Excel报表生成工具,它采用标签的方式,类似于jsp页面的EL表达 ...

  4. 导出Excel(导出一个模版)

    有时,客户需要一个标准的模板来填东西,然后在导入 这时可以弄好excel模板,供导出 /** * 导出excel模板文件 * @param request * @param response * @r ...

  5. C#导出EXCEL(DataTable导出EXCEL)

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.I ...

  6. Vue框架下实现导入导出Excel、导出PDF

    项目需求:开发一套基于Vue框架的工程档案管理系统,用于工程项目资料的填写.编辑和归档,经调研需支持如下功能: Excel报表的导入.导出 PDF文件的导出 打印表格 经过技术选型,项目组一致决定通过 ...

  7. 利用GridView控件导出其他文件(导出Excel,导出Word文件)

    原文发布时间为:2008-10-16 -- 来源于本人的百度文章 [由搬家工具导入] // 注意,在Visual Studio2005平台下,如果使用GridView导出文件,      //就必须重 ...

  8. Spring Boot 系列教程12-EasyPoi导出Excel下载

    Java操作excel框架 Java Excel俗称jxl,可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件,现在基本没有更新了 http://jxl.sourcef ...

  9. spring boot + easypoi两行代码excel导入导出

    easypoi封装了poi让我们能够非常简单的实现Excel导出,Excel模板导出,Excel导入,Word模板导出等,具体可见官网:http://www.afterturn.cn/. 我这边实现了 ...

随机推荐

  1. 【原】iOS学习47之第三方-FMDB

    将 CocoaPods 安装后,按照 CocoaPods 的使用说明就可以将 FMDB 第三方集成到工程中,具体请看博客iOS学习46之第三方CocoaPods的安装和使用(通用方法) 1. FMDB ...

  2. 【Unity3d游戏开发】UGUI插件入门之游戏菜单

    ugui是unity4.6开始加入的一个新的ui系统,非常强大,下面我们将通过一系列博客的方式一起来学习一下ugui的使用.本篇博客会介绍如何使用ugui制作一个游戏菜单,并且了解如何让物体与ugui ...

  3. 玩转AR,联想将在2017年推出第二款Tango AR手机

    今年6月份,联想与谷歌合作推出了全球首款消费级AR手机Phab2 Pro,并获得很大的关注.作为谷歌Project Tango的一部分,这款手机的最大亮点是它搭载了三颗后置摄像头和多个传感器,机身背面 ...

  4. ACM:UESTC - 649 括号配对问题 - stack

      UESTC - 649  括号配对问题 Time Limit: 1000MS   Memory Limit: 65535KB   64bit IO Format: %lld & %llu ...

  5. [python学习] 语言基础—排序函数(sort()、sorted()、argsort()函数)

    python的内建排序函数有 sort.sorted两个. 1.基础的序列升序排序直接调用sorted()方法即可 ls = list([5, 2, 3, 1, 4]) new_ls = sorted ...

  6. ZeroMQ接口函数之 :zmq_ctx_set - 设置环境上下文属性

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_ctx_set zmq_ctx_set(3) ØMQ Manual - ØMQ/3.2.5 Name zmq_ct ...

  7. 从DataGridView导出Excel

    从DataGridView导出Excel的两种情况,不多说,直接记录代码(新建类,直接引用传入参数). using System; using System.Collections.Generic; ...

  8. tachyon of zybo cluster

    把Tachyon层加入spark和hadoop之间,以加速集群 官网:http://tachyon-project.org/ github:https://github.com/amplab/tach ...

  9. ThinkPHP 3.2.3 使用 PHPExcel 处理 Excel 表格

    下载 PHPExcel(https://github.com/PHPOffice/PHPExcel) 把下载的 zip 包解压至 ./ThinkPHP/Library/Vendor 下 一.导入 Ex ...

  10. 简单实用JSTL标签库

    1. JSTL标签库是一个什么东东? 简单来说,有了它你就可以在类似HTML的JSP页面里面运用一些高级语法,实现迭代.条件判断.XML文档操作.国际化标签.SQL标签. 2. 如何使用 ? 参考网址 ...