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.Merge multiple DataTables and export to Excel in ASP.Net<p>Here i have created sample,In this I have merged two Datatable with primary key also exported to excel.</p> <p><span style="text-decoration: underline;">C#</span></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataColumn col; DataTable table1 = new DataTable(); table1.PrimaryKey = new DataColumn[] { col = table1.Columns.Add(&#34;patient_id&#34;) }; col.DataType = typeof(int); col.Unique = true; col = table1.Columns.Add(&#34;appointment_time&#34;); col = table1.Columns.Add(&#34;patient_name&#34;); col = table1.Columns.Add(&#34;patient_doctor&#34;); table1.Rows.Add(1, &#34;0900&#34;, &#34;George Michael&#34;, &#34;Dr. Jhon&#34;); table1.Rows.Add(2, &#34;1000&#34;, &#34;David Morkel&#34;, &#34;Dr. Peter&#34;); table1.Rows.Add(3, &#34;1100&#34;, &#34;Jack Michael&#34;, &#34;Dr. Smith&#34;); table1.Rows.Add(4, &#34;1200&#34;, &#34;Kevin Morkel&#34;, &#34;Dr. Andrea&#34;); DataTable table2 = new DataTable(); table2.PrimaryKey = new DataColumn[] { col = table2.Columns.Add(&#34;patient_id&#34;) }; col.DataType = typeof(int); col.Unique = true; col = table2.Columns.Add(&#34;appointment_time&#34;); table2.Rows.Add(1, &#34;0900&#34;); table2.Rows.Add(2, &#34;1000&#34;); table2.Rows.Add(3, &#34;1100&#34;); table2.Rows.Add(4, &#34;1200&#34;); DataTable merged = new DataTable(); merged.Merge(table1); merged.Merge(table2); ExportToExcel(merged); } } private void ExportToExcel(DataTable dt) { Response.Clear(); Response.Buffer = true; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=RepeaterExport.xls&#34;); Response.Charset = &#34;&#34;; Response.ContentType = &#34;application/vnd.ms-excel&#34;; string tab = &#34;&#34;; foreach (DataColumn dc in dt.Columns) { Response.Write(tab + dc.ColumnName); tab = &#34;\t&#34;; } Response.Write(&#34;\n&#34;); int i; foreach (DataRow dr in dt.Rows) { tab = &#34;&#34;; for (i = 0; i &lt; dt.Columns.Count; i++) { Response.Write(tab + dr[i].ToString()); tab = &#34;\t&#34;; } Response.Write(&#34;\n&#34;); } Response.Flush(); Response.End(); }</pre> <p><span style="text-decoration: underline;">Screenshot</span></p> <p><img src="https://i.imgur.com/x79FLWW.png" alt="" width="446" height="210" /></p> <p>I hope this will help you out.</p>https://www.aspforums.net:443/Threads/117491/Merge-multiple-DataTables-and-export-to-Excel-in-ASPNet/https://www.aspforums.net:443/Threads/117491/Merge-multiple-DataTables-and-export-to-Excel-in-ASPNet/Fri, 16 Oct 2015 09:09:49 GMT