导出Excel And 导出word
<%@ 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" />
<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的更多相关文章
- 使用Apache POI导出Excel小结--导出XLS格式文档
使用Apache POI导出Excel小结 关于使用Apache POI导出Excel我大概会分三篇文章去写 使用Apache POI导出Excel小结--导出XLS格式文档 使用Apache POI ...
- NPOI控件的使用导出excel文件和word文件
public HttpResponseMessage GetReportRateOutput(DateTime? begin_time = null, DateTime? end_time = nul ...
- java导出Excel定义导出模板
在很多系统功能中都会有Excel导入导出功能,小编采用JXLS工具,简单.灵活. JXLS是基于 Jakarta POI API 的Excel报表生成工具,它采用标签的方式,类似于jsp页面的EL表达 ...
- 导出Excel(导出一个模版)
有时,客户需要一个标准的模板来填东西,然后在导入 这时可以弄好excel模板,供导出 /** * 导出excel模板文件 * @param request * @param response * @r ...
- C#导出EXCEL(DataTable导出EXCEL)
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.I ...
- Vue框架下实现导入导出Excel、导出PDF
项目需求:开发一套基于Vue框架的工程档案管理系统,用于工程项目资料的填写.编辑和归档,经调研需支持如下功能: Excel报表的导入.导出 PDF文件的导出 打印表格 经过技术选型,项目组一致决定通过 ...
- 利用GridView控件导出其他文件(导出Excel,导出Word文件)
原文发布时间为:2008-10-16 -- 来源于本人的百度文章 [由搬家工具导入] // 注意,在Visual Studio2005平台下,如果使用GridView导出文件, //就必须重 ...
- Spring Boot 系列教程12-EasyPoi导出Excel下载
Java操作excel框架 Java Excel俗称jxl,可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件,现在基本没有更新了 http://jxl.sourcef ...
- spring boot + easypoi两行代码excel导入导出
easypoi封装了poi让我们能够非常简单的实现Excel导出,Excel模板导出,Excel导入,Word模板导出等,具体可见官网:http://www.afterturn.cn/. 我这边实现了 ...
随机推荐
- 【Unity3d游戏开发】Unity3D中常用的物理学公式
马三最近在一直负责Unity中的物理引擎这一块,众所周知,Unity内置了NVIDIA公司PhysX物理引擎.然而,马三一直觉得只会使用引擎而不去了解原理的程序猿不是一位老司机.所以对一些常用的物理学 ...
- jQuery插件 -- 动态事件绑定插件jquery.livequery.js
http://blog.csdn.net/zzq58157383/article/details/7721974 动态事件绑定插件livequery, 可以利用它给相应的DOM元素注册事件或者触发回调 ...
- CentOS6.7安装RabbitMQ3.6.5
1.安装所有依赖包yum install -y gcc ncurses ncurses-base ncurses-devel ncurses-libs ncurses-static ncurses-t ...
- erlang mac os 10.9 卸载脚本
#!/bin/bash if [ "$(id -u)" != "0" ]; then echo "Insufficient permissions. ...
- msql数据迁移,myisam及innoDB
直接迁移数据库文件. 一.MySQL数据库文件介绍 MySQL的每个数据库都对应存放在一个与数据库同名的文件夹中,MySQL数据库文件包括MySQL所建数据库文件和MySQL所用存储引擎创建的数据库文 ...
- MySQL黑科技用法总结(持续更新)
1.利用set插入数值 insert [into] 表名 set 列=值. 2.利用select对字段进行测试 ) ,并且有2条记录 ',num1+1的计算结果 tips:相等返回1,否则返回0 f ...
- 我的ES6学习之路(一)
强烈推荐 阮一峰写的<ECMAScript6入门> let和const命令 let命令: let用于声明变量,用法和var相似,但是不完全相同,有以下几点区别 ① let命令只在当前作用 ...
- 1.Powershell认识
Windows PowerShell 是一种命令行外壳程序和脚本环境,自Windows Server 2008开始就有内置于系统当中,有取代CMD之势.管理员使用Powershell完成一些日常重复的 ...
- Java学习记录-Jdk包简单介绍
java.applet Java语言编写的一些小应用程序 java.awt AWT 是Abstract Window ToolKit (抽象窗口工具包)的缩写,这个工具包提供了一套与本地图形界面进行交 ...
- Java中PreparedStatement与Statement的总结
概要: PreparedStatement 接口继承自 Statement 接口,PreparedStatement 比普通Statement 对象使用起来更加灵活,更有效率. 一.PreparedS ...