2

I am doing a project in C# and I have downloaded ExcelDataReader from codeplex and added it to my project reference. There is an error in namespace 'using Excel;' though I have added Excel, Excel.4.5, Microsoft.Office.Interop.Excel to my project references. I'm getting error where I have written

 IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

The errors are quoted below:

The type or namespace name 'ExcelDataReader' could not be found (are you missing a using directive or an assembly reference?) The name 'ExcelReaderFactory does not exist in the current context

How to deal with these errors?
Thank you in advance.

PS:- I have added 'using Excel = Microsoft.Office.Interop.Excel' directive as well.

shA.t
  • 16,580
  • 5
  • 54
  • 111
Rituja Nashikkar
  • 71
  • 1
  • 1
  • 7
  • Try changing 'using Excel = Microsoft.Office.Interop.Excel' to 'using Microsoft.Office.Interop.Excel' – Souvik Ghosh Jul 28 '17 at 09:17
  • Try to install the excel reference via nugget. Nugget will do the job for you and then you should be able to resolve the dependency. – JBO Jul 28 '17 at 09:19
  • @SouvikGhosh i made changes as per ur suggestion and now I'm getting a new error "'DataTable' is an ambiguous reference between 'System.Data.DataTable' and 'Microsoft.Office.Interop.Excel.DataTable'." – Rituja Nashikkar Jul 28 '17 at 09:24
  • @JBO I am new to .NET and C#...I don't know what nuggets are and how to use them – Rituja Nashikkar Jul 28 '17 at 09:25
  • @RitujaNashikkar Ok, in your visual studio, you go to Tools > "Nuget Package Manager" > "Manage Nuget Packages For Solution". It will open a window where you will be able to look for the reference you need. – JBO Jul 28 '17 at 09:31
  • @JBO There is no such option in Tools...What to do now? – Rituja Nashikkar Jul 28 '17 at 09:40
  • Look the answer posted by @Michal Turcyzn, it says the thing as me but more detailed. – JBO Jul 28 '17 at 09:41

3 Answers3

2

Go to Project -> Manage NuGet Packages..., in opened window select Browse tab and type Excel Data Reader, install it, at the top of your class add using Excel. Then you will be able to use interface IExcelDataReader.

Here is the source:

https://forums.asp.net/t/1981566.aspx?The+type+or+namespace+name+IExcelDataReader+could+not+be+found

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • Sir, there is no option as 'Manage NuGet Packages' in Project tab. Not under any other tab in visual studio. Seems like I'm missing it in my machine. – Rituja Nashikkar Jul 28 '17 at 09:46
1

Go to your project-> right click on References and click on Manage NuGet Packages a new tab would opened in your IDE if you are using VS 2015 or higher otherwise a pop window would open. enter package name and install.

tally2512
  • 61
  • 4
1

In older versions of ExcelDataReader - like 2.1.2.3 - it uses an Excel namespace for itself, I mean that namespace is not related to Microsoft Office Excel, that I can suggest you to use alias for them:

using XlReader = Excel;
using Xl = Microsoft.Office.Interop.Excel;

...

var excelReader = XlReader.ExcelReaderFactory.CreateOpenXmlReader(stream);

But in newer version - like 3.1.0 - its namespace changed to ExcelDataReader;
You can install the package of ExcelDataReader 2.1.2.3 that will remove your compile exception.

shA.t
  • 16,580
  • 5
  • 54
  • 111