导出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/. 我这边实现了 ...
随机推荐
- maven ClassNotFoundException: org.springframework.web.context.ContextLoaderL
信息: Starting Servlet Engine: Apache Tomcat/6.0.32 2012-3-31 9:39:40 org.apache.catalina.core.Standar ...
- HDU 1556 线段树或树状数组,插段求点
1.HDU 1556 Color the ball 区间更新,单点查询 2.题意:n个气球,每次给(a,b)区间的气球涂一次色,问最后每个气球各涂了几次. (1)树状数组 总结:树状数组是一个查 ...
- C++11 auto and decltype
1.auto关键字 C++新标准引入auto关键词,此auto与之前C语言的auto意义已经不一样了. 这里的auto是修饰未知变量的类型,编译器会通过此变量的初始化自动推导变量的类型. 例如:aut ...
- springmvc+spring+hibernate
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...
- CSS3过渡、变形和动画
1.CSS3过渡 所谓CSS3过渡,就是使用CSS3让元素从一种状态慢慢转换到另一种状态.如鼠标的悬停状态就是一种过渡.如下例子: #content a{ text-decoration: n ...
- HDU 5965 枚举模拟 + dp(?)
ccpc合肥站的重现...一看就觉得是dp 然后强行搞出来一个转移方程 即 根据第i-1列的需求和i-1 i-2列的枚举摆放 可以得出i列摆放的种类..加了n多if语句...最后感觉怎么都能过了..然 ...
- 张小龙在2017微信公开课PRO版讲了什么(附演讲实录和2016微信数据报告)
今天2017微信公开课PRO版在广州亚运城综合体育馆举行,这次2017微信公开课大会以“下一站”为主题,而此次的微信公开课的看点大家可能就集中在腾讯公司高级副总裁.微信之父——张小龙的演讲上了!今天中 ...
- MVC5 视图 不显示 Styles.Render Scripts.Render 问题解决
第一步:安装 WebGrease 使用 nuget 安装 WebGrease 安装依赖 第二步:修改配置文件 <configSections> <!-- For more infor ...
- 利用xtrabackup备份mysql数据库
利用xtrabackup备份mysql数据库 一.安装1.直接下载二进制文件wget http://www.percona.com/downloads/XtraBackup/XtraBackup-2. ...
- js中网页高度与宽度那些事
网页可见区域宽:document.body.clientWidth; 网页可见区域高:document.body.clientHeight; 网页可见区域宽:document.body.offsetW ...