Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hallo!
I am trying to open an excel file using the following code which is written in a class library that I created in my project.

string mySheet = @"‪C:\Users\Tal\Desktop\DataBase.xlsx";

           var excelApp = new Excel.Application();
       
           excelApp.Visible = true;

          
           Workbook wb = excelApp.Workbooks.Open(mySheet);
           Worksheet ws = (Worksheet)wb.Worksheets[1];
           Excel.Range xlRange = ws.UsedRange;


When I try to run the program it stops when it trys to execute the following command line:
" Workbook wb = excelApp.Workbooks.Open(mySheet);"

It says it can not find the excel file and asks me to check if the location of the file that I inserted is write properly. I know for sure that the file location is right because when I'm trying to run the same program on the console application it all works just fine.

If anyone knows how to make this program to work on the windows form too, I would be very happy if he could help me to do so.

Thanks, Tal.


Hi again,
I have some updates related to my problem that I wanted to fill you in about.

Apparently the problem I told you about is even bigger than I thought. I found out that the program doesn't work properly but only on my computer. Now, it's not even working on the console application as it was before. In both platforms(console application and windows form) the program stops, and the same error message appears.
However, when I try to run the program on a different computer it work's exactly as it should and the Excel file is displayed. I should of say though, that there are three basic differences between the two computers which I think might couse this problem.
Firt, on my computer (the one where the code doesn't compile) windows 8 is used as the operating system and on the other computer windows 7 is installed.
The second difference between the two computers is that on my computer I have the Excel 2010 and on the other one the 2007 version.
In order to work with Excel, I need to add the Microsoft Excel Object Libary refrence. On my computer it is the 14.0 version, while on the other computer it is the 12.0 version.
Please let me know if you think that the problem is related to one of the differences I mentioned above, and if not, I would be very happy to hear for any other suggestions you might have, that could help me solve this problem.

Thanks again, Tal.
Posted
Updated 10-Dec-13 12:42pm
v8
Comments
Adam Zgagacz 9-Dec-13 11:48am    
Regardless if this is console or Windows form it should behave same way. But since it doesn’t, here is suggestion for further debugging:

Instead of hardcoding path, have openFileDialog in your program to pick the file and from dialog navigate to location you want to pick it. Does it give you same error?
Have you tried putting the file in some other directory?
Talhm 10-Dec-13 17:46pm    
yes unfortunately it didn't work out

Have a look here: Workbooks.Open method[^].

There are several input parameters which are obligatory. You can pass Missing Field[^] instaed of real values ;)
 
Share this answer
 
C#
object objOpt = Type.Missing;
this.excelApp = new Excel.Application();
this.wbclass = excelApp.Workbooks.Open(
                                                filePath,
                                                objOpt,
                                                true,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt);
 
Share this answer
 
v2
this.openFileDialog1.FileName = "*.xls";

DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
//string mySheet = @"‪C:\Users\Tal\Desktop\DataBase.xlsx";
string mySheet = openFileDialog1.FileName;

var excelApp = new Microsoft.Office.Interop.Excel.Application();

excelApp.Visible = true;


Workbook wb = excelApp.Workbooks.Open(mySheet);
Worksheet ws = (Worksheet)wb.Worksheets[1];
Microsoft.Office.Interop.Excel.Range xlRange = ws.UsedRange;
}
 
Share this answer
 
this.openFileDialog1.FileName = "*.xls";

DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
//string mySheet = @"‪C:\Users\Tal\Desktop\DataBase.xlsx";
string mySheet = openFileDialog1.FileName;

var excelApp = new Microsoft.Office.Interop.Excel.Application();

excelApp.Visible = true;


Workbook wb = excelApp.Workbooks.Open(mySheet);
Worksheet ws = (Worksheet)wb.Worksheets[1];
Microsoft.Office.Interop.Excel.Range xlRange = ws.UsedRange;
}
 
Share this answer
 
Comments
TheRealSteveJudge 1-Dec-14 3:31am    
Clone of Solution 5!
Free-Rider?

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