1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <title></title> 8 </head> 9 <body> 10 <form id="form1" runat="server"> 11 12 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 13 Width="367px" CellPadding="4"> 14 <Columns> 15 <asp:TemplateField HeaderText="编号"> 16 <ItemTemplate> 17 <asp:Label ID="Label1" runat="server" Text=\'<%# Eval("id") %>\'></asp:Label> 18 </ItemTemplate> 19 </asp:TemplateField> 20 <asp:TemplateField HeaderText="标题"> 21 <ItemTemplate> 22 <asp:Label ID="Label2" runat="server" Text=\'<%# Eval("title") %>\'></asp:Label> 23 </ItemTemplate> 24 </asp:TemplateField> 25 <asp:TemplateField HeaderText="内容"> 26 <ItemTemplate> 27 <asp:Label ID="Label3" runat="server" Text=\'<%# Eval("content") %>\'></asp:Label> 28 </ItemTemplate> 29 </asp:TemplateField> 30 <asp:TemplateField HeaderText="创建时间"> 31 <ItemTemplate> 32 <asp:Label ID="Label4" runat="server" Text=\'<%# Eval("createTime") %>\'></asp:Label> 33 </ItemTemplate> 34 </asp:TemplateField> 35 <asp:TemplateField HeaderText="类别编号"> 36 <ItemTemplate> 37 <asp:Label ID="Label5" runat="server" Text=\'<%# Eval("caId") %>\'></asp:Label> 38 </ItemTemplate> 39 </asp:TemplateField> 40 </Columns> 41 </asp:GridView> 42 <br /> 43 <br /> 44 <asp:Button ID="Button1" runat="server" Text="导出Excel" Width="109px" 45 onclick="Button1_Click" /> 46 47 <asp:Button ID="Button2" runat="server" Text="导出Word" Width="86px" 48 onclick="Button2_Click" /> 49 50 </form> 51 </body> 52 </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[0].Rows.Count > 0) { 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(); }