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:
- How to upload the file in MVC.
- Read excel file content.
How to upload the file in MVC
In your MVC razor view add the following html content.
- @using(Html.BeginForm("Upload", "Home", FormMethod.Post, new {enctype = "multipart/form-data"}))
- {
- <table>
- <tr>
- <td>File:</td>
- <td>
- <input type="file" name="UploadedFile" />
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <input type="submit" name="Submit" value="Submit" />
- </td>
- </tr>
- </table>
- }
Now come to home controller and add action called Upload.
- public ActionResult Upload(FormCollection formCollection)
- {
- if (Request != null)
- {
- HttpPostedFileBase file = Request.Files["UploadedFile"];
- if ((file != null) && (file.ContentLength & gt; 0) && !string.IsNullOrEmpty(file.FileName))
- {
- string fileName = file.FileName;
- string fileContentType = file.ContentType;
- byte[] fileBytes = new byte[file.ContentLength];
- var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
- }
- }
- }
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:
- public class Users
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- }
- public ActionResult Upload(FormCollection formCollection)
- {
- if (Request != null)
- {
- HttpPostedFileBase file = Request.Files["UploadedFile"];
- if ((file != null) && (file.ContentLength & gt; 0) && !string.IsNullOrEmpty(file.FileName))
- {
- string fileName = file.FileName;
- string fileContentType = file.ContentType;
- byte[] fileBytes = new byte[file.ContentLength];
- var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
- var usersList = new List & lt;
- Users & gt;
- ();
- using(var package = new ExcelPackage(file.InputStream))
- {
- var currentSheet = package.Workbook.Worksheets;
- var workSheet = currentSheet.First();
- var noOfCol = workSheet.Dimension.End.Column;
- var noOfRow = workSheet.Dimension.End.Row;
- for (int rowIterator = 2; rowIterator & lt; = noOfRow; rowIterator++)
- {
- var user = new Users();
- user.FirstName = workSheet.Cells[rowIterator, 1].Value.ToString();
- user.LastName = workSheet.Cells[rowIterator, 2].Value.ToString();
- usersList.Add(user);
- }
- }
- }
- }
- return View("Index");
- }

Mehul PrajapatiPosted Nov 20, 2019, 5:12 AM
Thanks. your code make my task easy.
Sarmad YousifPosted Apr 12, 2019, 3:22 PM
What is usersList object? Is there another way to see the results of the uploaded file in the view
amit vermaPosted Apr 24, 2018, 12:43 AM
How to transfer user data to sqlserver table?????????
NazimMert BilgiPosted Dec 29, 2017, 2:30 AM
Very very thank youu.....
Rakesh ShrivastavaPosted Feb 4, 2017, 2:08 AM
Thank you sir..this is what i was looking for.