954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

error opening excel sheet in c#

i'm trying to open an existing excel worksheet. my code is:

Microsoft.Office.Interop.Excel.Application excelApp;
Microsoft.Office.Interop.Excel.Workbook excelWorkbook;
Microsoft.Office.Interop.Excel.Worksheet ExlWrkSheet;
private object Opt = Type.Missing

excelApp = new ApplicationClass();

excelWorkbook = excelApp.Workbooks.Open("..//..//Data.xls", Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt);


on the last line i get an exception that he cannot find the file.
i placed the file in the project directory.
i tried also "Data.exl" path when the file is placed in the debug directory, it didn't helped.

what am i doing wrong ?

emilio
Junior Poster
163 posts since Nov 2007
Reputation Points: 20
Solved Threads: 0
 

Why not use a pathname like @"C:\....\Test.xls" ?

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Are you sure about the path wheather it is right or not

Jugortha
Junior Poster
172 posts since Oct 2007
Reputation Points: 11
Solved Threads: 16
 

i tried it with an absolute path like "c:\Data.xls" and it works fine.
the problem is with relative path.

emilio
Junior Poster
163 posts since Nov 2007
Reputation Points: 20
Solved Threads: 0
 

Read this http://weblogs.asp.net/pwelter34/archive/2006/02/08/create-a-relative-path-code-snippet.aspx
it might help.
It is also possible that Open only accepts absolute paths.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Open does except relative paths, however, you have to be sure of where you are vs where your file is which is often unlikely.

So, if you have your app, and you are sure you are in your exe's directory, then opening "Data/file" will work - but, for example, excel may not be in the apps directory but its own, you can tell by using the filemonitor from microsoft (seeing as they bought the company out) which will show you where it was trying to find the file.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You