<%@ 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. JS里面的两种运动函数

    最新学了一个新的运动函数,与最初学习的有所不同,第一个运动是根据运动速度完成运动 ,第二个则是根据运动的时间来完成运动,而且把之前的函数都进行了一些兼容处理,在这里列出了看一下: 第一种animate ...

  2. DOTA 2 Match History WebAPI(翻译)

    关于DOTA 2 Match History WebAPI 的 源网页地址: http://dev.dota2.com/showthread.php?t=47115 由于源网页全英文,这边做下翻译方便 ...

  3. iscrolljs 看API 回顾以前开发中失误

    今天有空 细致的看看iscrolljs api 发现自己以前的几个失误是没看api造成的 失误1 页面a操作 影响了页面b的滚动条 api 解释: options.bindToWrapper The ...

  4. Python数据库迁移脚本(终极版)

    上次的那几个脚本这次全部整合到了一起,而且后面发现了一个可以使用的ODBC包,于是这次采用的方式就很简单了,直接通过ODBC将InterBase数据库中的数据全部取出来之后通过Python的sqlal ...

  5. jquery 练习笔记

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Spring注入方式及注解配置

    一:基于xml的DI(Dependency Injection) 注入类型: 定义学生Student实体类和小汽车Car实体类:进行封装和生成ToString(),并自定义属性Car Student ...

  7. Learn ZYNQ (8)

    在zed的PS端运行spark(已成功): (1)设置uboot为sd卡启动rootfs: "sdboot=if mmcinfo; then " \                 ...

  8. php http头设置相关信息

    HTTP 状态码 状态码用来告诉HTTP客户端,HTTP服务器是否产生了预期的Response. HTTP/1.1中定义了5类状态码, 状态码由三位数字组成,第一个数字定义了响应的类别 1XX 提示信 ...

  9. 关于datagridview里使用combox的总结

    最近写的程序中需要在DataGridView中使用下拉选择的功能,首选方案是列的ColumnType属性 使用EditingControlShowing事件, if (e.Control is Com ...

  10. 【java基础学习】反射

    1. 什么是反射 Class.Method.Field.Constructor,它们是反射对象.它们是类.方法.成员变量.构造器,在内存中的形式. 也就是万物皆对象!类是类型.方法是类型.成员变量是类 ...