ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Export ASP.Net ListView to Excel using C# and VB.Net<p>Hi <a class="username" rel="nauna"> nauna</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:ListView ID=&#34;ListView1&#34; runat=&#34;server&#34;&gt; &lt;ItemTemplate&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:Label ID=&#34;Label1&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Id&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;lblprofile&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Name&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:Label ID=&#34;lblresume&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Country&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/ItemTemplate&gt; &lt;/asp:ListView&gt; &lt;br /&gt; &lt;asp:Button ID=&#34;btnExport&#34; runat=&#34;server&#34; Text=&#34;Export To Excel&#34; OnClick=&#34;ExportToExcel&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.IO; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.IO</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { BindGrid(); } } private void BindGrid() { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;Id&#34;, typeof(int)), new DataColumn(&#34;Name&#34;, typeof(string)), new DataColumn(&#34;Country&#34;, typeof(string)) }); dt.Rows.Add(1, &#34;John Hammond&#34;, &#34;United States&#34;); dt.Rows.Add(2, &#34;Mudassar Khan&#34;, &#34;India&#34;); dt.Rows.Add(3, &#34;Suzanne Mathews&#34;, &#34;France&#34;); dt.Rows.Add(4, &#34;Robert Schidner&#34;, &#34;Russia&#34;); ListView1.DataSource = dt; ListView1.DataBind(); } protected void ExportToExcel(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=GridViewExport.xls&#34;); Response.Charset = &#34;&#34;; Response.ContentType = &#34;application/vnd.ms-excel&#34;; using (StringWriter sw = new StringWriter()) { HtmlTextWriter hw = new HtmlTextWriter(sw); this.BindGrid(); ListView1.RenderControl(hw); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } } public override void VerifyRenderingInServerForm(Control control) { /* Verifies that the control is rendered */ }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then BindGrid() End If End Sub Private Sub BindGrid() Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn() {New DataColumn(&#34;Id&#34;, GetType(Integer)), New DataColumn(&#34;Name&#34;, GetType(String)), New DataColumn(&#34;Country&#34;, GetType(String))}) dt.Rows.Add(1, &#34;John Hammond&#34;, &#34;United States&#34;) dt.Rows.Add(2, &#34;Mudassar Khan&#34;, &#34;India&#34;) dt.Rows.Add(3, &#34;Suzanne Mathews&#34;, &#34;France&#34;) dt.Rows.Add(4, &#34;Robert Schidner&#34;, &#34;Russia&#34;) ListView1.DataSource = dt ListView1.DataBind() End Sub Protected Sub ExportToExcel(ByVal sender As Object, ByVal e As EventArgs) Response.Clear() Response.Buffer = True Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=GridViewExport.xls&#34;) Response.Charset = &#34;&#34; Response.ContentType = &#34;application/vnd.ms-excel&#34; Using sw As StringWriter = New StringWriter() Dim hw As HtmlTextWriter = New HtmlTextWriter(sw) Me.BindGrid() ListView1.RenderControl(hw) Response.Output.Write(sw.ToString()) Response.Flush() Response.End() End Using End Sub Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control) End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/sBFCO2K.gif" alt="" width="409" height="492" /></p>https://www.aspforums.net:443/Threads/226021/Export-ASPNet-ListView-to-Excel-using-C-and-VBNet/https://www.aspforums.net:443/Threads/226021/Export-ASPNet-ListView-to-Excel-using-C-and-VBNet/Thu, 18 Jul 2019 00:29:11 GMT