title: MVC - Create a database table using displayed table in html
body: <p>I'm trying to save a html table which is created from excel reader in my MVC project.
  This is code of uploading excel and display it in HTML table in the browser:</p>
  
  <p>Upload function:</p>
  
  <pre><code>    public ActionResult Upload()
      {
          return View();
      }
      [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Upload(HttpPostedFileBase upload)
      {
          if (ModelState.IsValid)
          {
  
              if (upload != null &amp;&amp; upload.ContentLength &gt; 0)
              {
                  // ExcelDataReader works with the binary Excel file, so it needs a FileStream
                  // to get started. This is how we avoid dependencies on ACE or Interop:
                  Stream stream = upload.InputStream;
  
                  // We return the interface, so that
                  IExcelDataReader reader = null;
  
  
                  if (upload.FileName.EndsWith(".xls"))
                  {
                      reader = ExcelReaderFactory.CreateBinaryReader(stream);
                  }
                  else if (upload.FileName.EndsWith(".xlsx"))
                  {
                      reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                  }
                  else
                  {
                      ModelState.AddModelError("File", "This file format is not supported");
                      return View();
                  }
  
                  reader.IsFirstRowAsColumnNames = true;
  
                  DataSet result = reader.AsDataSet();
                  reader.Close();
  
                  return View(result.Tables[0]);
              }
              else
              {
                  ModelState.AddModelError("File", "Please Upload Your file");
              }
          }
          return View();
      }
  </code></pre>
  
  <p>And this is my View Code:</p>
  
  <pre><code>@model System.Data.DataTable
  @using System.Data;
  
  &lt;h2&gt;Upload File&lt;/h2&gt;
  
  @using (Html.BeginForm("Upload", "ImportData", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
  {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary()
  
      &lt;div class="form-group"&gt;
          &lt;input type="file" id="dataFile" name="upload" /&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group"&gt;
          &lt;input type="submit" value="Upload" class="btn btn-default" /&gt;
      &lt;/div&gt;
  
      if (Model != null)
      {
          &lt;table&gt;
              &lt;thead&gt;
                  &lt;tr&gt;
                      @foreach (DataColumn col in Model.Columns)
                      {
                          &lt;th&gt;@col.ColumnName&lt;/th&gt;
                      }
                  &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
                  @foreach (DataRow row in Model.Rows)
                  {
                      &lt;tr&gt;
                          @foreach (DataColumn col in Model.Columns)
                          {
                              &lt;td&gt;@row[col.ColumnName]&lt;/td&gt;
                          }
                      &lt;/tr&gt;
                  }
              &lt;/tbody&gt;
          &lt;/table&gt;
      }
  }
  </code></pre>
  
  <h2>After using the previous code the html will browsed in the view like the below image :</h2>
  
  <h2> <a href="https://i.stack.imgur.com/Xx88j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Xx88j.png" alt="enter image description here"></a></h2>
  
  <p>Finally, I need to save this HTML table as a new table:</p>
  
  <pre><code>CREATE TABLE ExcelSheet_name(
     ID varchar(50),
     FirstName varchar(50),
     LastName varchar(50),
     .....
     XColumn varchar(50));
  </code></pre>
  
  <p>the table name depending on the excel sheet name and the column will be depending on the displayed column in the HTML table</p>