In this article I will explain with an example, how to export ASP.Net Repeater control to Excel file in ASP.Net using C# and VB.Net.
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net Repeater and an ASP.Net Button control.
The Repeater control consists of an ItemTemplate. The content of this template will be repeated for each record present in its DataSource.
The ASP.Net Button has been assigned with an OnClick event handler.
<asp:Repeater ID="rptCustomers" runat="server">
    <ItemTemplate>
        <table border="1">
            <tr>
                <td style="width: 100px">
                    <%#Eval("CustomerID") %>
                </td>
                <td style="width: 100px">
                    <%#Eval("City") %>
                </td>
                <td style="width: 100px">
                    <%#Eval("PostalCode")%>
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:Repeater>
<br />
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="ExportToExcel" />
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
 
VB
Imports System.IO
Imports System.Data
 
 
Populating the Repeater control
Inside the Page Load event handler, first the contents of XML file are read and converted to DataSet using ReadXml method.
Finally, top 10 records are fetched from the DataSet and the Repeater control is populate using C# and VB.Net.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("~/Customers.xml"));
        rptCustomers.DataSource = ds.Tables[0].Rows.Cast<DataRow>().Take(10).CopyToDataTable();
        rptCustomers.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim ds As New DataSet()
        ds.ReadXml(Server.MapPath("~/Customers.xml"))
        rptCustomers.DataSource = ds.Tables(0).Rows.Cast(Of DataRow)().Take(10).CopyToDataTable()
        rptCustomers.DataBind()
    End If
End Sub
 
 
Export Repeater to Excel in ASP.Net using C# and VB.Net
When Export Button is clicked, the following event handler is executed.
Inside this event handler, the StringWriter and HtmlTextWriter instance is created.
Then, StringWriter object is set to the HtmlTextWriter object.
Next, using the RenderControl method, the Repeater has been rendered as an HTML string.
Finally, HTML string is written to the Response which initiates the File download.
C#
protected void ExportToExcel(object sender, EventArgs e)
{
    using (StringWriter sw = new StringWriter())
    {
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=RepeaterExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        rptCustomers.RenderControl(hw);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
}
 
VB.Net
Protected Sub ExportToExcel(sender As Object, e As EventArgs)
    Using sw As StringWriter = New StringWriter()
        Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=RepeaterExport.xls")
        Response.Charset = ""
        Response.ContentType = "application/vnd.ms-excel"
        rptCustomers.RenderControl(hw)
        Response.Output.Write(sw.ToString())
        Response.Flush()
        Response.End()
    End Using
End Sub
 
 
Screenshot
Export ASP.Net Repeater Control to Excel File
 
 
Demo
 
 
Downloads