Click here to Skip to main content
15,888,112 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi I read the excel data and I want to throw the exceli datatable I read.
For example I want to have excel in a folder and throw it into the datatable in the same way as the values in the excel row, column.
I tried to do as follows, but I am taking errors. One of my mistakes is as follows.

This line:

Application app=new Excel.Application(); //this error is:Application is an ambiguous reference between Microsoft.Office.Interop.Excel.Application and System.Windows.Application.

What I have tried:

using(var conn=new OleDbDataAdapter("Select DISTINCT [File Number] FROM [Sheet1$]" ,conn);
var dt=new System.Data.DataTable();
da.Fill(dt);

Application app=new Application();   //this line error and this error 
Workbook book=null;
Worksheet sheet=null;
Range range=null;

app.Visible=false;
app.ScreenUpdating=false;
app.DisplayAlerts=false;

string Path=Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);

book=app.Workbooks.Open(Path + @" ", MissingValue,MissingValue,MissingValue);

range=sheet.get_Range("A1",Missing.Value);
range=range.get_End(XlDirection.xlToRight);
range=range.get_End(xlDirection.xlDown);

string downAddress=range.get_Address(false,false,xlReferenceStyle.xlA1,Type.Missing,Type.Missing);
range=sheet.get_Rane("A1",downAddress);
object[,] values=(object[,])range.Value2;

for(int j=1; j<=values.GetLenght(1); j++)
{
Console.Write("{0}",j);
}

for(int j=1; j<=values.GetLenght(0); j++)
{
Console.Write("{0}",j++);
}

Console.WriteLine();
Posted
Updated 26-May-19 1:53am
v2

The error you are getting says you have 2 namespaces having the same name of Application class in it so you need to choose the correct one for your use here.

You can either use the full name of the class with namespace when declaring your Application instance or create an alias to resolve this issue.



C#
using ExcelNS = Microsoft.Office.Interop.Excel; // create alias for Excel namespace

Application app=new ExcelNS.Application();


Or

C#
Application app=new Microsoft.Office.Interop.Excel.Application();
 
Share this answer
 
In the right-hand part of your code

Application app=new Excel.Application();


you use Excel.Application so the compiler knows it is the Application class in the Excel namespace. However on the left you only have "Application" so it doesn't know if it is the Application class in Excel or in System.Windows. So update the left-hand side to remove the ambiguity.

Excel.Application app=new Excel.Application();
 
Share this answer
 
Comments
[no name] 26-May-19 8:04am    
I searched this error and I done Excel.Application app=new Excel.Application(); .But this line still giving error. microsoft.office.interop.excel reference cannot be found.
F-ES Sitecore 26-May-19 9:02am    
Make sure you have the reference properly added to the project.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900