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.Import dynamically generated Excel to Database using C# in ASP.Net MVC<p>Hi <a class="username" rel="userNK"> userNK</a>,</p> <p>Refer below code.</p> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <pre class="brush: csharp">DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;f1&#34;, typeof(string)), new DataColumn(&#34;f2&#34;, typeof(string)), new DataColumn(&#34;f3&#34;, typeof(string)), new DataColumn(&#34;01/07/2019&#34;, typeof(string)), new DataColumn(&#34;01/14/2019&#34;, typeof(string)), new DataColumn(&#34;01/21/2018&#34;, typeof(string)), new DataColumn(&#34;02/28/2019&#34;, typeof(string)), new DataColumn(&#34;02/04/2019&#34;, typeof(string)), new DataColumn(&#34;02/11/2019&#34;, typeof(string)), new DataColumn(&#34;02/18/2019&#34;, typeof(string)), new DataColumn(&#34;02/25/2019&#34;, typeof(string)), new DataColumn(&#34;03/04/2019&#34;, typeof(string)) }); dt.Rows.Add(&#34;Material Code&#34;, &#34;MaterialName&#34;, &#34;Planned Quantity&#34;, &#34;&#34;, &#34;&#34;, &#34;&#34;, &#34;&#34;, &#34;&#34;, &#34;&#34;, &#34;&#34;, &#34;&#34;, &#34;&#34;); dt.Rows.Add(&#34;OHM010&#34;, &#34;25 KV Double&#34;, &#34;10000&#34;, &#34;&#34;, &#34;200&#34;, &#34;&#34;, &#34;400&#34;, &#34;500&#34;, &#34;250&#34;, &#34;&#34;, &#34;&#34;, &#34;&#34;); dt.Rows.Add(&#34;OHM011&#34;, &#34;25KV Double&#34;, &#34;20000&#34;, &#34;100&#34;, &#34;200&#34;, &#34;&#34;, &#34;&#34;, &#34;350&#34;, &#34;&#34;, &#34;&#34;, &#34;50&#34;, &#34;&#34;); // Get Above Data from excel. DataTable dtnew = new DataTable(); dtnew.Columns.Add(&#34;MaterialID&#34;); dtnew.Columns.Add(&#34;PlanedQty&#34;); dtnew.Columns.Add(&#34;ImportedDate&#34;); dtnew.Columns.Add(&#34;Month&#34;); dtnew.Columns.Add(&#34;YearAddedOn&#34;); for (int i = 1; i &lt; dt.Rows.Count; i++) { for (int j = 3; j &lt; dt.Columns.Count; j++) { dtnew.Rows.Add(dt.Rows[i][&#34;f1&#34;].ToString(), dt.Rows[i][j].ToString(), dt.Columns[j].ColumnName, Convert.ToDateTime(dt.Columns[j].ColumnName).Month, DateTime.Today.Year + &#34; &#34; + DateTime.Today.ToString(&#34;MM/dd/yyyy&#34;)); } } for (int i = 0; i &lt; dtnew.Rows.Count; i++) { var db = new RVNL2Entities(); int materialId = db.tblMaterialMasters.Where(o =&gt; o.MaterialCode == dtnew.Rows[i][&#34;MaterialID&#34;].ToString()).SingleOrDefault().MaterialDetailsID; if (materialId != 0) { tblPlanedQty objAddQty = new tblPlanedQty(); objAddQty.MaterialID = materialId; objAddQty.AddedBy = 1; objAddQty.AddedOn = DateTime.Now; objAddQty.ImportedDate = dtnew.Rows[i][&#34;ImportedDate&#34;].ToString(); objAddQty.Month = dtnew.Rows[i][&#34;Month&#34;].ToString(); objAddQty.Year = dtnew.Rows[i][&#34;YearAddedOn&#34;].ToString(); objAddQty.PlanedQty = dtnew.Rows[i][&#34;PlanedQty&#34;].ToString(); objAddQty.ProjectID = 4; db.tblPlanedQties.Add(objAddQty); db.SaveChanges(); } }</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/707517/Import-dynamically-generated-Excel-to-Database-using-C-in-ASPNet-MVC/https://www.aspforums.net:443/Threads/707517/Import-dynamically-generated-Excel-to-Database-using-C-in-ASPNet-MVC/Wed, 28 Aug 2019 08:26:56 GMT