Sometimes we have a requirement to upload an excel file, read that excel file and save into database or perform operation on uploaded excel file.

This blog demonstrate the following:

  1. How to upload the file in MVC.
  2. Read excel file content.

How to upload the file in MVC

In your MVC razor view add the following html content.

  1. @using(Html.BeginForm("Upload", "Home", FormMethod.Post, new {enctype = "multipart/form-data"}))
  2. {
  3. <table>
  4. <tr>
  5. <td>File:</td>
  6. <td>
  7. <input type="file" name="UploadedFile" />
  8. </td>
  9. </tr>
  10. <tr>
  11. <td colspan="2">
  12. <input type="submit" name="Submit" value="Submit" />
  13. </td>
  14. </tr>
  15. </table>
  16. }
Notice, the action name: Upload, Controller Name: Home.

Now come to home controller and add action called Upload.
  1. public ActionResult Upload(FormCollection formCollection)
  2. {
  3. if (Request != null)
  4. {
  5. HttpPostedFileBase file = Request.Files["UploadedFile"];
  6. if ((file != null) && (file.ContentLength & gt; 0) && !string.IsNullOrEmpty(file.FileName))
  7. {
  8. string fileName = file.FileName;
  9. string fileContentType = file.ContentType;
  10. byte[] fileBytes = new byte[file.ContentLength];
  11. var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
  12. }
  13. }
  14. }
Now try to run the app by putting a breakpoint and see if all is working until now,

Now add dll called EPPLUS from NUGET which is used for reading and writing files.

Let's say I have a file with FirstName & LastName.

Now add a class called users:
  1. public class Users
  2. {
  3. public string FirstName { get; set; }
  4. public string LastName { get; set; }
  5. }
Modify our action method to read the file stream object which we uploaded. Add using OfficeOpenXml; statement.
  1. public ActionResult Upload(FormCollection formCollection)
  2. {
  3. if (Request != null)
  4. {
  5. HttpPostedFileBase file = Request.Files["UploadedFile"];
  6. if ((file != null) && (file.ContentLength & gt; 0) && !string.IsNullOrEmpty(file.FileName))
  7. {
  8. string fileName = file.FileName;
  9. string fileContentType = file.ContentType;
  10. byte[] fileBytes = new byte[file.ContentLength];
  11. var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
  12. var usersList = new List & lt;
  13. Users & gt;
  14. ();
  15. using(var package = new ExcelPackage(file.InputStream))
  16. {
  17. var currentSheet = package.Workbook.Worksheets;
  18. var workSheet = currentSheet.First();
  19. var noOfCol = workSheet.Dimension.End.Column;
  20. var noOfRow = workSheet.Dimension.End.Row;
  21. for (int rowIterator = 2; rowIterator & lt; = noOfRow; rowIterator++)
  22. {
  23. var user = new Users();
  24. user.FirstName = workSheet.Cells[rowIterator, 1].Value.ToString();
  25. user.LastName = workSheet.Cells[rowIterator, 2].Value.ToString();
  26. usersList.Add(user);
  27. }
  28. }
  29. }
  30. }
  31. return View("Index");
  32. }
Finally all the users will be available in usersList object.