In this article I will explain with an example, how to export Repeater control to Excel file in ASP.Net using C# and VB.Net.
 
 

Database

I have made use of the following table Customers with the schema as follows.
Export ASP.Net Repeater Control to Excel File
 
I have already inserted few records in the table.
Export ASP.Net Repeater Control to Excel File
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

HTML Markup

The following HTML Markup consists of:
Repeater – For displaying data.
The Repeater consists of following templates.
HeaderTemplate – The content of this template will not be repeated and will be placed in the top most position i.e. head section of the Repeater control.
ItemTemplate – The content of this template will be repeated for each record present in its DataSource.
ItemTemplate consist of Label controls for displaying records.
FooterTemplate – The content of this template will not be repeated and will be placed in the bottom most position i.e. footer section of the Repeater control.
 
Button – For exporting Repeater data to Excel.
The Button has been assigned with an OnClick event handler.
<asp:Repeater ID="rptCustomers" runat="server">
    <HeaderTemplate>
        <table cellspacing="0" rules="all" border="1">
            <tr>
                <th>Customer Id</th>
                <th>Name</th>
                <th>Country</th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td><asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("CustomerId") %>'/></td>
            <td><asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'/></td>
            <td><asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'/></td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>
<br />
<asp:Button ID="btnExport" runat="server" Text="Export to Excel" OnClick="ExportToExcel" />
 
 

Namespaces

You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
using System.Configuration;
using System.Data.SqlClient; 
 
VB.Net
Imports System.IO
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient 
 
 

Populating the Repeater control

Inside the Page Load event handler, the Repeater is populated with dynamic DataTable.
Note: For more details on populate Repeater with DataTable, please refer my article Populate (Bind) Repeater control from Code Behind using DataTable in ASP.Net.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string sql = "SELECT CustomerId, Name, Country FROM Customers";
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        rptCustomers.DataSource = dt;
                        rptCustomers.DataBind();
                    }
                }
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim sql As String = "SELECT CustomerId, Name, Country FROM Customers"
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As SqlConnection = New SqlConnection(constr)
            Using cmd As SqlCommand = New SqlCommand(sql, con)
                Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                    Using dt As DataTable = New DataTable()
                        sda.Fill(dt)
                        rptCustomers.DataSource = dt
                        rptCustomers.DataBind()
                    End Using
                End Using
            End Using
        End Using
    End If
End Sub
 
 

Exporting Repeater to Excel in ASP.Net

When the Export Button is clicked, the Response class properties are set.
1. Content-Disposition – It is a response header indicating, the download file is an attachment and allows setting the file name.
Note: For more details on Content-Disposition header, please refer What is Content Disposition Header in ASP.Net.
 
2. ContentType – It informs the Browser about the file type. In this case it is Excel file.
Then, the StringWriter and HtmlTextWriter class objects are created and StringWriter object is passed as parameter to the HtmlTextWriter object.
Next, using the RenderControl method of Repeater, the Repeater is rendered as an HTML string.
Finally, StringWriter object is written to the Response which initiates the File download operation.
C#
protected void ExportToExcel(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=RepeaterExport.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
            rptCustomers.RenderControl(hw);
            Response.Output.Write(sw.ToString());
        }
    }
    Response.Flush();
    Response.End();
}
 
VB.Net
Protected Sub ExportToExcel(ByVal sender As Object, ByVal e As EventArgs)
    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=RepeaterExport.xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"
    Using sw As StringWriter = New StringWriter()
        Using hw As HtmlTextWriter = New HtmlTextWriter(sw)
            rptCustomers.RenderControl(hw)
            Response.Output.Write(sw.ToString())
        End Using
    End Using
    Response.Flush()
    Response.End()
End Sub
 
 

Screenshot

The Form

Export ASP.Net Repeater Control to Excel File
 
 

Exported Excel file

Export ASP.Net Repeater Control to Excel File
 
 

Demo

 
 

Downloads