NET Repeater控件使用

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Repeater.aspx.cs" Inherits="OprateWordWebSite.Repeater" %>

<!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">
    <div>
    <div>
        <asp:Button ID="btnExport" runat="server" Text="导出数据"
            onclick="btnExport_Click" /></div>
    <asp:Repeater id="rptEmp" runat="server">
  <HeaderTemplate>
  <div  style="text-decoration: underline; font-family: 宋体, Arial, Helvetica, sans-serif; font-size: 18px; position: relative; text-align: center;">Repeater Header</div>
  
   <div style="text-align: right">2010年05月&nbsp;&nbsp;&nbsp;&nbsp</div>
    <table width="100%" border="1" cellspacing="0" cellpadding="0">   
    <tr align="center" bgcolor="#3399FF">
   <td>员工编号</td>
   <td>员工姓名</td>
   <td>所属职位</td>
   <td>上级编号</td>
   <td>工资水平</td>
   </tr>
  </HeaderTemplate>
  <ItemTemplate>
   <tr align="center">
   <td><%# DataBinder.Eval(Container.DataItem, "EMPNO") %></td>
   <td><%# DataBinder.Eval(Container.DataItem, "ENAME") %></td>
   <td><%# DataBinder.Eval(Container.DataItem, "JOB") %></td>
   <td><%# DataBinder.Eval(Container.DataItem, "MGR") %></td>
   <td><%# DataBinder.Eval(Container.DataItem, "SAL") %></td>
   </tr>
  </ItemTemplate>
  <FooterTemplate>
   </table>
  </FooterTemplate>
</asp:Repeater>

    </div>
    </form>
</body>
</html>

---------------------后台----------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using System.IO;

namespace OprateWordWebSite
{
    public partial class Repeater : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (OracleConnection conn = new OracleConnection("Data Source=orcl;user=scott;password=wang131421"))
                {
                    OracleCommand cmd = new OracleCommand("select * from emp", conn);

                    OracleDataAdapter da = new OracleDataAdapter(cmd);

                    DataSet ds = new DataSet();

                    da.Fill(ds, "emp");

                    rptEmp.DataSource = ds.Tables[0];
                    rptEmp.DataBind();
                }
            }
        }

        public override void VerifyRenderingInServerForm(Control control)
        {

        }


        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            Response.ClearContent();

            Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");

            Response.ContentType = "application/excel";

            StringWriter sw = new StringWriter();

            HtmlTextWriter htw = new HtmlTextWriter(sw);

            rptEmp.RenderControl(htw);

            Response.Write(sw.ToString());

            Response.End();
 

        }
    }
}

原文地址:https://www.cnblogs.com/kingwangzhen/p/1709684.html