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 Generic List of DataTable to multiple Excel Sheets (Worksheets) using ClosedXML in C# and VB.Net<p>Hi <a class="username" rel="SUJAYS"> SUJAYS</a>,</p> <p>Check the updated code sample.</p> <p>Refer below article for more details.</p> <h1 class="header"><a title="Export DataSet DataTables to multiple Excel Sheets (Worksheets) in ASP.Net using C# and VB.Net'a" href="https://www.aspsnippets.com/Articles/Export-DataSet-DataTables-to-multiple-Excel-Sheets-Worksheets-in-ASPNet-using-C-and-VBNet.aspx">Export DataSet DataTables to multiple Excel Sheets (Worksheets) in ASP.Net using C# and VB.Net</a></h1> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using ClosedXML.Excel; using System.IO;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO Imports ClosedXML.Excel</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">private void button1_Click(object sender, EventArgs e) { DataTable dt1 = new DataTable(); dt1.Columns.AddRange(new DataColumn[3] { new DataColumn(&#34;Id&#34;, typeof(int)), new DataColumn(&#34;Name&#34;, typeof(string)), new DataColumn(&#34;Country&#34;,typeof(string)) }); dt1.Rows.Add(1, &#34;John Hammond&#34;, &#34;United States&#34;); dt1.Rows.Add(2, &#34;Mudassar Khan&#34;, &#34;India&#34;); DataTable dt2 = dt1.Clone(); dt2.Rows.Add(3, &#34;Suzanne Mathews&#34;, &#34;France&#34;); dt2.Rows.Add(4, &#34;Robert Schidner&#34;, &#34;Russia&#34;); // Get the list as per your code. List&lt;DataTable&gt; dts = new List&lt;DataTable&gt;(); dts.Add(dt1); dts.Add(dt2); string path = &#34;D:\\Excel\\&#34;; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } using (XLWorkbook wb = new XLWorkbook()) { for (int i = 0; i &lt; dts.Count; i++) { wb.Worksheets.Add(dts[i], i.ToString()); } wb.SaveAs(path + &#34;Test.xlsx&#34;); } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click Dim dt1 As DataTable = New DataTable() dt1.Columns.AddRange(New DataColumn(2) {New DataColumn(&#34;Id&#34;, GetType(Integer)), New DataColumn(&#34;Name&#34;, GetType(String)), New DataColumn(&#34;Country&#34;, GetType(String))}) dt1.Rows.Add(1, &#34;John Hammond&#34;, &#34;United States&#34;) dt1.Rows.Add(2, &#34;Mudassar Khan&#34;, &#34;India&#34;) Dim dt2 As DataTable = dt1.Clone() dt2.Rows.Add(3, &#34;Suzanne Mathews&#34;, &#34;France&#34;) dt2.Rows.Add(4, &#34;Robert Schidner&#34;, &#34;Russia&#34;) Dim dts As List(Of DataTable) = New List(Of DataTable)() dts.Add(dt1) dts.Add(dt2) Dim path As String = &#34;D:\Excel\&#34; If Not Directory.Exists(path) Then Directory.CreateDirectory(path) End If Using wb As XLWorkbook = New XLWorkbook() For i As Integer = 0 To dts.Count - 1 wb.Worksheets.Add(dts(i), i.ToString()) Next wb.SaveAs(path &amp; &#34;Test.xlsx&#34;) End Using End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/2as2X4L.gif" alt="" width="282" height="197" /></p>https://www.aspforums.net:443/Threads/440438/Export-Generic-List-of-DataTable-to-multiple-Excel-Sheets-Worksheets-using-ClosedXML-in-C-and-VBNet/https://www.aspforums.net:443/Threads/440438/Export-Generic-List-of-DataTable-to-multiple-Excel-Sheets-Worksheets-using-ClosedXML-in-C-and-VBNet/Fri, 21 Jun 2019 03:47:20 GMT