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)
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.
The ‘Microsoft.ACE.OLEDB.12.0’ provider is not registered on the local machine.
and probably, you would go for following approach to fix them
1. Installation of Office 2007 Data Connectivity Components or Office 2010 Database Engine on the Server.
2. The application pool that the site is running set “Enable 32-bit applications” to “True”
3. In project, Change Platform target from: Any CPU to x86 and Rebuild it.
2. The application pool that the site is running set “Enable 32-bit applications” to “True”
3. In project, Change Platform target from: Any CPU to x86 and Rebuild it.
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.
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.
Steps:
2. To install ExcelDataReader, run the following command in the Package Manager Console:
Install-Package ExcelDataReader
3. Add New Controller say HomeController and add following action:
public ActionResult Upload() { return View(); } |
4. Add View of Upload action and use following code:
@model System.Data.DataTable @using System.Data; < h2 >Upload File</ h2 > @using (Html.BeginForm("Upload", "Home", null, FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() @Html.ValidationSummary() < div class = "form-group" > < input type = "file" id = "dataFile" name = "upload" /> </ div > < div class = "form-group" > < input type = "submit" value = "Upload" class = "btn btn-default" /> </ div > if (Model != null) { < table > < thead > < tr > @foreach (DataColumn col in Model.Columns) { < th >@col.ColumnName</ th > } </ tr > </ thead > < tbody > @foreach (DataRow row in Model.Rows) { < tr > @foreach (DataColumn col in Model.Columns) { < td >@row[col.ColumnName]</ td > } </ tr > } </ tbody > </ table > } } |
We will read excel, get data in DataTable and show DataTable in View.
5. To read the submitted file:
|
No comments:
Post a Comment