0

Checked out the link bellow and tried to use the method proposed there with no results. My problem relates with importing from an Excel file using the OLEDB provider, for some reasons that I don't understand, when the file is open in Excel the import shows some columns (e.g date) as text, it doesn't happen when the file is not in use. My problem can be solved temporally by checking if the xls file is open/in use, so I can post a message asking the user to close the file.

Thanks, Mihail

https://www.google.com/search?rlz=1C1RNHN_enUS465US465&ix=hea&sourceid=chrome&ie=UTF-8&q=stackoverflow.

Kolky
  • 2,917
  • 1
  • 21
  • 42
Eu Lupu
  • 361
  • 1
  • 4
  • 10
  • 1
    That's a link to a Google search for "stackoverflow". All of us here have already found it. I think you included the wrong link. – Cody Gray - on strike Jan 12 '12 at 15:09
  • Not a duplicate as question, but Emmanuel link is the correct one, the code there is not working for me when the file is open by Excel. – Eu Lupu Jan 12 '12 at 16:01

1 Answers1

5

One way to do that would be opening the file in exclusive mode. This will fail if the file is open in any other process, and it will also prevent other processes from opening it until you close the handle. For example:

File.Open("worksheet.xls", FileMode.Open, FileAccess.Read, FileShare.None); 
Jon
  • 428,835
  • 81
  • 738
  • 806
  • That is exactly what I am doing, opening in exclusive mode. The file is open in Excel when running my program but I am able to open it, the resulting stream is not null, hence my problem. Is this particular to Excel itself? – Eu Lupu Jan 12 '12 at 17:32
  • I have been testing this more, not exactly sure what causes, sometime the opened Excel file to be open exclusively as the code above shows, so I will take the answer is correct and see if can figure out the Excel specifics. Thanks for your help! – Eu Lupu Jan 12 '12 at 18:37