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?

Tigran Tokmajyan
  • 1,937
  • 7
  • 25
  • 36

5 Answers5

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");
Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127
  • Good answer.. but probably not what OP asked!!?! =) – Nayan Oct 05 '10 at 17:04
  • @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. – Jeff Ogata Oct 05 '10 at 17:14
  • True, but it seems he wants relative path now. =) – Nayan Oct 05 '10 at 17:16
  • Yes, I saw that comment after I posted :) – Jeff Ogata Oct 05 '10 at 17:18
  • 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. – Jeff Ogata Oct 05 '10 at 17:22
  • I agree. Let's see what he does. – Nayan Oct 06 '10 at 01:10
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.

Chris Jaynes
  • 2,868
  • 30
  • 29
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.

Nayan
  • 3,092
  • 25
  • 34
  • If you want relative path, then now you know how to create relative path knowing what the default folder is. – Nayan Oct 05 '10 at 17:00
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.

Community
  • 1
  • 1
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.

vlad
  • 4,748
  • 2
  • 30
  • 36