Upload and Read Excel File (.xls, .xlsx) in ASP.NET MVC

By Brij Mohan

Are you using traditional way to read .xls and .xlsx files in ASP.NET/C#? If yes, you might be using following providers:Microsoft.Jet.OLEDB.4.0 provider for Excel 97-2003 format (.xls)Microsoft.ACE.OLEDB.12.0 provider for Excel 2007+ format (.xlsx)

and you may get the following common errors:

The ‘Microsoft.Jet.OLEDB.4.0’ provider is not registered on the local machine.The ‘Microsoft.ACE.OLEDB.12.0’ provider is not registered on the local machine.

What would happen if you have shared hosting or Azure Website hosting and You are not allowed to install ACE Database engine on the server?

In this post, we will see how to upload and read both .xls and .xlsx excel files without any server dependencies in ASP.NET MVC. We will use ExcelDataReader, an Open Source project to read excel files.

Output:

Conclusion:

ExcelDataReader has no dependencies on external database engines which may not be available on a remote server. It is helpful to deploy application to Windows Azure Websites, Shared hosting or any other machine where the ACE Database engine is not available. We have seen how easy to read excel sheets in ASP.NET MVC with ExcelDataReader.

Hope It helps. Feel free to share your opinion in comment box.

Share on

13 comments

Thank you for this great example !. But I have a question. Is it possible to after read the excel file that the code is changed in a way that he can also read data from each cell from the spreadsheet and then save this data in a text file or database? If possible how we could do this ?

It’s throwing error ‘IExcelDataReader’ does not contain a definition for ‘IsFirstRowAsColumnNames’ and no extension method ‘IsFirstRowAsColumnNames’ accepting a first argument of type ‘IExcelDataReader’ could be found (are you missing a using directive or an assembly reference?)

If you are using ExcelDataReader 3+ you will find that there isn’t any method for AsDataSet() for your reader object, You need also to install another package for ExcelReader.DataSet, then you can use AsDataSet() method. Also there is not a property for IsFirstRowAsColumnNames instead of that you need to set it inside of ExcelDataSetConfiguration