title: Using Epplus to import data from an Excel file to SQL Server database table
body: <p>I've tried implementing this<a href="https://www.paragon-inc.com/resources/blogs-posts/easy_excel_interaction_pt6" rel="nofollow">https://www.paragon-inc.com/resources/blogs-posts/easy_excel_interaction_pt6</a> on an ASP.NET MVC 5 Application.</p>
  
  <p>//SEE CODE BELOW</p>
  
  <pre><code>   [HttpPost]
      public ActionResult Upload(HttpPostedFileBase file)
      {
  
          var regPIN = DB.AspNetUsers.Where(i =&gt; i.Id == user.Id).Select(i =&gt; i.registrationPIN).FirstOrDefault();
          if (file != null &amp;&amp; file.ContentLength &gt; 0)
          {
              var extension = Path.GetExtension(file.FileName);
              var excelFile = Path.Combine(Server.MapPath("~/App_Data/BulkImports"),regPIN + extension);
              if (System.IO.File.Exists(excelFile))
              {
                  System.IO.File.Delete(excelFile);
              }
              else if (file.ContentType == "application/vnd.ms-excel" || file.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
              {
                  file.SaveAs(excelFile);//WORKS FINE
                  //BEGINING OF IMPORT
                  FileInfo eFile = new FileInfo(excelFile);
                  using (var excelPackage = new ExcelPackage(eFile))
                  {
                      if (!eFile.Name.EndsWith("xlsx"))//Return ModelState.AddModelError()
                      { ModelState.AddModelError("", "Incompartible Excel Document. Please use MSExcel 2007 and Above!"); }
                      else
                      {
                          var worksheet = excelPackage.Workbook.Worksheets[1];
                          if (worksheet == null) { ModelState.AddModelError("", "Wrong Excel Format!"); }// return ImportResults.WrongFormat;
  
                          else
                          {
                              var lastRow = worksheet.Dimension.End.Row;
                              while (lastRow &gt;= 1)
                              {
                                  var range = worksheet.Cells[lastRow, 1, lastRow, 3];
                                  if (range.Any(c =&gt; c.Value != null))
                                  { break; }
                                  lastRow--;
                              }
                              using (var db = new BlackBox_FinaleEntities())// var db = new BlackBox_FinaleEntities())
                              {
                                  for (var row = 2; row &lt;= lastRow; row++)
                                  {
                                      var newPerson = new personalDetails
                                      {
                                          identificationType = worksheet.Cells[row, 1].Value.ToString(),
                                          idNumber = worksheet.Cells[row, 2].Value.ToString(),
                                          idSerial = worksheet.Cells[row, 3].Value.ToString(),
                                          fullName = worksheet.Cells[row, 4].Value.ToString(),
                                          dob = DateTime.Parse(worksheet.Cells[row, 5].Value.ToString()),
                                          gender = worksheet.Cells[row, 6].Value.ToString()
                                      };
                                      DB.personalDetails.Add(newPerson);
                                      try { db.SaveChanges(); }
                                      catch (Exception) { }
                                  }
                              }
                          }
  
                      }
  
                  }//END OF IMPORT
                  ViewBag.Message = "Your file was successfully uploaded.";
                  return RedirectToAction("Index");
              }
  
              ViewBag.Message = "Error: Your file was not uploaded. Ensure you upload an excel workbook file.";
              return View();
          }
          else
          {
              ViewBag.Message = "Error: Your file was not uploaded. Ensure you upload an excel workbook file.";
              return View();
          }
      }
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/qBtNN.png" rel="nofollow">See Picture Error</a>
  Any help would be greatly appreciated mates.</p>