title: How to save records after importing Excel Sheet into ASP.NET Core Razor pages
body: <p>Im trying tofind a way on how I can save the uploaded excell sheet to my database Ms SQL server.</p>
  
  <p>I have the following:</p>
  
  <p>Model </p>
  
  <pre><code>public class ImportDocs
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public string LastName { get; set; }
      public string UserName { get; set; }
  }
  </code></pre>
  
  <p>HTML and JavaScript for viewing the records</p>
  
  <pre><code>&lt;form method="post" enctype="multipart/form-data"&gt;
  &lt;div class="row"&gt;
      &lt;div class="col-md-4"&gt;
          &lt;input type="file" id="fUpload" name="files" class="form-control" /&gt;
      &lt;/div&gt;
      &lt;div class="col-md-8"&gt;
          &lt;input type="button" id="btnUpload" value="Upload" /&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;br /&gt;
  &lt;div id="dvData"&gt;&lt;/div&gt;
  &lt;br /&gt;
  &lt;div class="col-md-8"&gt;
      &lt;input type="button" id="btnSave" value="Save To Database" /&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>JavaScript</p>
  
  <pre><code>@Html.AntiForgeryToken()
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"&gt; 
  &lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  
  $(document).ready(function () {
      $('#btnUpload').on('click', function () {
          var fileExtension = ['xls', 'xlsx'];
          var filename = $('#fUpload').val();
          if (filename.length == 0) {
              alert("Please select a file.");
              return false;
          }
          else {
              var extension = filename.replace(/^.*\./, '');
              if ($.inArray(extension, fileExtension) == -1) {
                  alert("Please select only excel files.");
                  return false;
              }
          }
          var fdata = new FormData();
          var fileUpload = $("#fUpload").get(0);
          var files = fileUpload.files;
          fdata.append(files[0].name, files[0]);
          $.ajax({
              type: "POST",
              url: "/ImportExcelFiles/Index?handler=Import",
              beforeSend: function (xhr) {
                  xhr.setRequestHeader("XSRF-TOKEN",
                      $('input:hidden[name="__RequestVerificationToken"]').val());
              },
              data: fdata,
              contentType: false,
              processData: false,
              success: function (response) {
                  if (response.length == 0)
                      alert('Some error occured while uploading');
                  else {
                      $('#dvData').html(response);
                  }
              },
              error: function (e) {
                  $('#dvData').html(e.responseText);
              }
          });
      })
  });
  </code></pre>
  
  <p></p>
  
  <p>C# Code </p>
  
  <pre><code>  private IHostingEnvironment _hostingEnvironment;
      public IndexModel(IHostingEnvironment hostingEnvironment)
      {
          _hostingEnvironment = hostingEnvironment;
      }
      public ActionResult OnPostImport()
      {
          IFormFile file = Request.Form.Files[0];
          string folderName = "Upload";
          string webRootPath = _hostingEnvironment.WebRootPath;
          string newPath = Path.Combine(webRootPath, folderName);
          StringBuilder sb = new StringBuilder();
          if (!Directory.Exists(newPath))
          {
              Directory.CreateDirectory(newPath);
          }
          if (file.Length &gt; 0)
          {
              string sFileExtension = Path.GetExtension(file.FileName).ToLower();
              ISheet sheet;
              string fullPath = Path.Combine(newPath, file.FileName);
              using (var stream = new FileStream(fullPath, FileMode.Create))
              {
                  file.CopyTo(stream);
                  stream.Position = 0;
                  if (sFileExtension == ".xls")
                  {
                      HSSFWorkbook hssfwb = new HSSFWorkbook(stream); //This will read the Excel 97-2000 formats  
                      sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook  
                  }
                  else
                  {
                      XSSFWorkbook hssfwb = new XSSFWorkbook(stream); //This will read 2007 Excel format  
                      sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook   
                  }
                  IRow headerRow = sheet.GetRow(0); //Get Header Row
                  int cellCount = headerRow.LastCellNum;
                  sb.Append("&lt;table class='table'&gt;&lt;tr&gt;");
                  for (int j = 0; j &lt; cellCount; j++)
                  {
                      NPOI.SS.UserModel.ICell cell = headerRow.GetCell(j);
                      if (cell == null || string.IsNullOrWhiteSpace(cell.ToString())) continue;
                      sb.Append("&lt;th&gt;" + cell.ToString() + "&lt;/th&gt;");
                  }
                  sb.Append("&lt;/tr&gt;");
                  sb.AppendLine("&lt;tr&gt;");
                  for (int i = (sheet.FirstRowNum + 1); i &lt;= sheet.LastRowNum; i++) //Read Excel File
                  {
                      IRow row = sheet.GetRow(i);
                      if (row == null) continue;
                      if (row.Cells.All(d =&gt; d.CellType == CellType.Blank)) continue;
                      for (int j = row.FirstCellNum; j &lt; cellCount; j++)
                      {
                          if (row.GetCell(j) != null)
                              sb.Append("&lt;td&gt;" + row.GetCell(j).ToString() + "&lt;/td&gt;");
                      }
                      sb.AppendLine("&lt;/tr&gt;");
                  }
                  sb.Append("&lt;/table&gt;");
              }
          }
          return this.Content(sb.ToString());
      }
  }
  </code></pre>
  
  <p>How can I archieve this </p>