In this article I will explain with an example, how to upload, read and display Excel file data using OLEDB in ASP.Net MVCRazor.
The uploaded Excel file data will be read using OLEDB library and the read data will be displayed in View in HTML Table (Grid) format.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
Connection Strings
The Excel files of version 97-2003 and 2007 and above make use different OLEDB providers and hence two different connection strings have been saved in the Web.Config file.
The DataSource property has been assigned a Placeholder {0}, which will be replaced by actual path of the File.
    <add name="Excel03ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'"/>
    <add name="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'"/>
You will need to import the following namespaces.
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
Following is a Model class named CustomerModel with three properties i.e. CustomerId, Name and Country.
public class CustomerModel
    /// Gets or sets CustomerId.
    public int CustomerId { get; set; }
    /// Gets or sets Name.
    public string Name { get; set; }
    /// Gets or sets Country.
    public string Country { get; set; }
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, an empty Generic List collections of the CustomerModel class object is returned.
Action method for handling POST operation for uploading and reading Excel file
This Action method gets called when the Excel File is selected and the Import Button is clicked, and it gets the uploaded file in the HttpPostedFileBase parameter.
Note: In case the HttpPostedFileBase parameter is appearing NULL, then please refer the article, ASP.Net MVC: HttpPostedFileBase always returns NULL.
The uploaded Excel file is saved to a folder named Uploads and then based on its extension whether XLS (97-2003) or XLSX (2007 and above), appropriate connection string is read from the Web.Config file and Placeholder is replaced by the path of the Excel file.
Then a connection is established with the Excel file using OLEDB classes and the name of the first sheet is determined and the first sheet data is read into a DataTable.
Finally a loop is executed over the DataTable and the records are copied into the Generic List collection of CustomerModel class objects which is then sent to the View.
public class HomeController : Controller
    // GET: Home
    public ActionResult Index()
        return View(new List<CustomerModel>());
    public ActionResult Index(HttpPostedFileBase postedFile)
        List<CustomerModel> customers = new List<CustomerModel>();
        string filePath = string.Empty;
        if (postedFile != null)
            string path = Server.MapPath("~/Uploads/");
            if (!Directory.Exists(path))
            filePath = path + Path.GetFileName(postedFile.FileName);
            string extension = Path.GetExtension(postedFile.FileName);
            string conString = string.Empty;
            switch (extension)
                case".xls": //Excel 97-03.
                    conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                case".xlsx": //Excel 07 and above.
                    conString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
            conString = string.Format(conString, filePath);
            using (OleDbConnection connExcel = new OleDbConnection(conString))
                using (OleDbCommand cmdExcel = new OleDbCommand())
                    using (OleDbDataAdapter odaExcel = new OleDbDataAdapter())
                        DataTable dt = new DataTable();
                        cmdExcel.Connection = connExcel;
                        //Get the name of First Sheet.
                        DataTable dtExcelSchema;
                        dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                        //Read Data from First Sheet.
                        cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
                        odaExcel.SelectCommand = cmdExcel;
                        foreach (DataRow row in dt.Rows)
                            customers.Add(new CustomerModel
                                CustomerId = Convert.ToInt32(row["Id"]),
                                Name = row["Name"].ToString(),
                                Country = row["Country"].ToString()
        return View(customers);
Inside the View, the CustomerModel class is declared as IEnumerable which specifies that it will be available as a Collection.
The View consists of an HTML FileUpload element and a Submit Button enclosed in a Form element.
The HTML Form has been created using the Html.BeginForm method which accepts the following parameters.
ActionNameName of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
HtmlAttributes – This array allows to specify the additional Form Attributes. Here we need to specify enctype = “multipart/form-data” which is necessary for uploading Files.
For displaying the records, an HTML Table is used. A loop will be executed over the Model which will generate the HTML Table rows with the Excel file data.
@using Read_Excel_OLEDB_MVC.Models
@model IEnumerable<CustomerModel>
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width"/>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
        <input type="file" name="postedFile"/>
        <input type="submit" value="Import"/>
    @if (Model.Count() > 0)
        <table cellpadding="0" cellspacing="0">
            @foreach (CustomerModel customer in Model)
The Excel File
Upload, Read and Display Excel file data using OLEDB in ASP.Net MVC
HTML Grid displaying Excel data
Upload, Read and Display Excel file data using OLEDB in ASP.Net MVC