tags:

views:

110

answers:

5

I have code in C# like this.

xlWorkBook = xlApp.Workbooks.Open("data.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

I have data.xls file where .exe are located.

When I compile and then run .exe, I'm receiving error that data.xls could not be found.

What I do wrong?

A: 

It depends on how exactly you run your application. What makes you think that application is being ran in the same directory where executable file is located? Most probably you just forgot to set the working directory right. How to do it? See this Q&A.

Vlad Lazarenko
I have set working directory of project to bin/release... And nothing.
Toktik
It's irrelevant.
Nayan
+1  A: 

Unless you've changed your project settings, when your C# app gets built, it is being built in a bin/debug (or bin/release) folder under your project. When you run from the IDE, that's the current working directory for your app.

Try using an absolute path, or moving the data.xls file into your application's bin/debug folder.

When you specify the absolute path, make sure to prefix the string with an @ sign to escape out the slashes. string path = @"c:\data\excel\data.xls";

UPDATE: If you need to use a relative path, I would get the absolute path based on the relative patht this way:

FileInfo fileInfo = new FileInfo("data.xls");
String path = fileInfo.FullName;

This might be preferable to getting the full path based on the .exe location, because it will work even if the CWD is not the same as the .exe location.

Chris Jaynes
absolute path worked.. But it is not applicable. I need relative.
Toktik
You could try using "./data.xls", but if you need to find it in the the CWD, I would try it using adrift's answer.
Chris Jaynes
This updated answer might work better for you than adrift's answer, because it would work from the CWD, wherever it is, not just the exe path.
Chris Jaynes
Relative will not work with this answer.
Nayan
@Nayan - What do you mean? I just tried it and it works fine on my box...
Chris Jaynes
A: 

i think this is a problem with the location of the Excel file. the application's working directory is not where the .exe file is located, but probably in the bin/debug folder.

vlad
I have copyed data.xls to all directories which have project.. Nothing worked.
Toktik
+2  A: 

If your xls will always be in the same location as your .exe, you can use this to get a path that won't be hardcoded to the build directory:

string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string path = Path.Combine(directory, "data.xls");
adrift
Good answer.. but probably not what OP asked!!?! =)
Nayan
@Nayan, I figured his original problem was not using @, and that the error specified in the post (file doesn't exist) was because he wasn't using an absolute path. Others had answered already recommending he use an absolute path, and I just wanted to provide this so he would be able to run his app from different folders without having the xls path hardcoded.
adrift
True, but it seems he wants relative path now. =)
Nayan
Yes, I saw that comment after I posted :)
adrift
If he wants to use a relative path, then he should put the xls in the My Documents folder (as per your answer), or use the answer to [this post](http://stackoverflow.com/questions/1766748/how-do-i-get-a-relative-path-from-one-path-to-another-in-c) if he wants to put it in a different location (probably too much work). But if just wants to have the xls in the exe's folder, he can use an absolute path.
adrift
I agree. Let's see what he does.
Nayan
+1  A: 

By default, Excel assumes that the folder of the file specified is user's "My Documents" directory. If the file is not there, any attempt to open it will fail.

By specifying an absolute path to the file, you can ensure that correct file is being picked up. Make sure the file exists.

Eg-

//file is in D:\TestFolder, and its called abc.xlsx
xlApp.Workbooks.Open( @"D:\TestFolder\abc.xlsx", ....

Hope it helps.

Other answers show you how to use the absolute path to the file which is kept at certain location.

Nayan
If you want relative path, then now you know how to create relative path knowing what the default folder is.
Nayan