0

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?

4
  • Where do you have data.xls saved? Commented Oct 5, 2010 at 16:12
  • @Toktic, You're probably receiving compilation errors because of the backslashes \. Try putting the @ sign in front of the string and it should be Ok. e.g. @"c:\whatever\data.xls" Commented Oct 5, 2010 at 16:17
  • Grant is right. Show us the code where you try absolute path. Commented Oct 5, 2010 at 16:19
  • makes a case for decent error management :) Commented Oct 5, 2010 at 18:02

5 Answers 5

2

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");
Sign up to request clarification or add additional context in comments.

6 Comments

Good answer.. but probably not what OP asked!!?! =)
@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.
True, but it seems he wants relative path now. =)
Yes, I saw that comment after I posted :)
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 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.
|
1

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.

1 Comment

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

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.

3 Comments

absolute path worked.. But it is not applicable. I need relative.
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.
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.
0

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.

1 Comment

I have set working directory of project to bin/release... And nothing.
0

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.

1 Comment

I have copyed data.xls to all directories which have project.. Nothing worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.