Step‑by‑Step Guide: Import and Display Excel Data in ASP.NET Core MVC with C#

How to upload the and display the uploaded excel file data in table format in asp.net core mvc 10 using c#.net and vb. net without using oldb.
In today's article I will show you how to upload an excel file and display the excel data preview in tabular format in asp.net .net core 10 mvc using c#.net and vb.net.  Here we will upload the file and read the data and make the excel data to display in table. Now for this article first we will create a new asp.net core 10 mvc application in visual studio 2026. After this we will create a sample Excel file with few data.

Excel data

So, first we will install a the NuGet package named as "ExcelDataReader".  To install the package, you need to right click on Dependency folder and select "Manage NuGet Package". In this you need to search the "ExcelDataReader".  If we use these ExcelDataReader package to read the excel file, we don't need to install Office on server to read the excel file. 

ExcelDataReader NuGet Package

After getting this we need to install both the packages shown above. Now add a model class file and add the below code. 

public class EmployeeModel
{
    public List<Employee> Employees { get; set; } = new List<Employee>();
}
public class Employee
{
    public double EmployeeID { get; set; }
    public string employeeName { get; set; } = string.Empty;
    public string Project { get; set; } = string.Empty;
} 

Now let's go to controller file and add HttpGet method as shown below.

[HttpGet]
public IActionResult Index()
{
    EmployeeModel employeeModel = new EmployeeModel();
    employeeModel.Employees = new List<Employee>();
    return View(employeeModel);
}  

In above HttpGet action method. In this i have passed the object of EmployeeModel class. Now crate a view and add the below code.

@model EmployeeModel
@{
    ViewData["Title"] = "Home Page";
}
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
    <div class="text-center">
        <input type="file" name="file" class="form-control" />
        <br />
        <input type="submit" value="Upload" />
        <br />
        <hr />
        <table width="100%" class="d-table table-bordered border-1">
            <thead>
                <tr>
                    <th>Emp. Id</th>
                    <th>Emp. Name</th>
                    <th>Project</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var emp in Model.Employees)
                {
                    <tr>
                        <td>@emp.EmployeeID</td>
                        <td>@emp.EmployeeName</td>
                        <td>@emp.Project</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
} 
In above code I have added the model class reference. Here I have added form tag. Without this we will not be able the perform post action Here @enctype = "multipart/form-data" is very important. In you don't add this in form tag you will not be able to get the uploaded file at controller end. At the end I have used for each loop to display the excel data in view.  Now let's add the HttpPost action method code to get the uploaded file and read the file and make collection to display the file data in view. 
C#.Net

using ExampleWebProject.Models;
using ExcelDataReader;
using Microsoft.AspNetCore.Mvc;
using System.Data;

namespace ExampleWebProject.Controllers
{
    public class HomeController : Controller
    {
        [HttpGet]
        public IActionResult Index()
        {
            EmployeeModel employeeModel = new EmployeeModel();
            employeeModel.Employees = new List<Employee>();
            return View(employeeModel);
        } 
        [HttpPost]
        public IActionResult Index(IFormFile file)
        {
            EmployeeModel employeeModel = new EmployeeModel();
            employeeModel.Employees = new List<Employee>();
            if (file != null)
            {
                try
                {
                    //Upload file to server
                    string parentPath = @"wwwroot\Demofiles\";
                    string uploadfilepath = Path.Combine(Directory.GetCurrentDirectory(), parentPath, file.FileName);
                    var filestream = new FileStream(uploadfilepath, FileMode.Create);
                    file.CopyToAsync(filestream);
                    string dbSaveUploadPath = string.Format("{0}{1}", @"\Demofiles\", file.FileName);
                    filestream.Close();
                    filestream.Dispose();
                    filestream = null;

                    //Read excel file data
                    DataSet dataSet = new DataSet();
                    System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
                    using var stream = new FileStream(dbSaveUploadPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    using IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream);
                    dataSet = reader.AsDataSet(new ExcelDataSetConfiguration()
                    {
                        UseColumnDataType = false,
                        ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
                        {
                            UseHeaderRow = true
                        }
                    });

                    //convert dataset to model list
                    employeeModel.Employees = dataSet.Tables[0].AsEnumerable()
                    .Select(dataRow => new Employee
                    {
                        EmployeeID = dataRow.Field<double>("Id"),
                        EmployeeName = dataRow.Field<string>("Name"),
                        Project = dataRow.Field<string>("Project")
                    }).ToList();
                    ViewBag.Message = "File uploaded successfully.";
                }
                catch (Exception ex)
                {
                    ViewBag.Message = ex.Message;
                }
            }
            else
            {
                ViewBag.Message = "Please select file to upload.";
            }
            return View(employeeModel);
        }
    }
} 
In above code I have added HttpGet method which I already explained. Now let's check the HttpPost method. Her first I have uploaded the user selected excel file in wwwroot folder and read the excel file data in Dataset. After reading the data in Dataset I have convert the dataset value to list on Employee. At the end I have passed the object to view. Now let's run the code to check the output.

Excel file selection

Now click on upload button. Ones the break point hit we will be able to get the excel value in dataset. Here i the preview of dataset. 
Excel to Dataset

Now at last, I have converted the dataset to list of employees. Now press F5 to check the output.

Upload and preview the excel data

upload-excel-file-display-preview-in-asp-net-core-mvc-using-c-net-vb-net.zip 14KiB
Note: Here in download i have just added sample excel, model, view and controller files. You must create a new .net core mvc project add this file and folders.

Post a Comment