Search code examples
c#excelimportexcel-interopinteropservices

Cannot open xls file with C# service


I have to create a service to import data from an excel file into a SQL Server table; the whole system belongs to one of my company's customer so I must use Visual Studio 2010. Some open may have this file opened so I decided to copy it to a temporary directory and then parse this copy. I thought I made it, since it worked correctly in debug mode: the file is opened, all the lines are read and in the end the values are written in the DB. I installed it as a service but I keep on getting the same error (translated into English):

System.Exception: System.Runtime.InteropServices.COMException (0x800A03EC): impossible access file "C:\[temp dir]\[file.xls]". Possible reasons are:

• file name or path file do not exists.
• file in use by another program.
• the name of the folder/worksheet you are trying to save corresponds to the name of an open folder/worksheet.
at Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)

The file exists, I can find it in the folder, and nobody is using it at the moment the service tries to open it (I just copied it). I am not trying to save it, I open it in read only mode so I can exclude the 3rd point also. The machine where I am developing the service is the same machine where the service runs, a Windows 7 Ultimate, Service Pack 1.

I thought it could be a problem of the user of the service, so I tried to launch the service in Visual Studio in debug mode logging to the system with the same user of the service (admin level) and it worked perfectly fine. Then I thought it could be a problem of timing, maybe the service tried to open the file "too soon" while the system had not released it, so I added a

System.Threading.Thread.Sleep(5000);

before the

xlApp = new Microsoft.Office.Interop.Excel.Application(); 

line but nothing changed. Of course, everytime I modify the service I stop it, copy the new files to the system and then restart it.

The code is:

string fileName = "[file name].xlsm";
string sourcePath = @"[origin]";
string targetPath = @"[destination]";
string fileDest = "[destination file name].xls";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string targetFile = System.IO.Path.Combine(targetPath, fileDest);

try
{
    System.IO.File.Copy(sourceFile, targetFile, true);
}
catch (Exception myEx)
{
    throw new Exception(myEx.ToString());
}

xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.DisplayAlerts = false;
xlApp.Visible = false;
try
{
    wbk = xlApp.Workbooks.Open(targetFile, 0, true, Type.Missing, "[pwd]", "[pwd]", true, Type.Missing, Type.Missing, false, false, Type.Missing, true, Type.Missing, Type.Missing); [<- this is the problem, the exception is raised with these instruction]
    sheet = new Worksheet();
    range = null;
    sheet = wbk.Worksheets.get_Item(4);
    range = sheet.UsedRange;
    numColonne = range.Columns.Count;
    numRighe = range.Rows.Count;
}
catch (Exception myEx)
{
    throw new Exception(myEx.ToString());
}

What can I do to solve this problem? Thanks for your help.


Solution

  • I added these 2 folders, C:\Windows\SysWOW64\config\systemprofile\Desktop (64bit) and C:\Windows\System32\config\systemprofile\Desktop (32bit). I found them in an another forum and I decided to try; now it works. Don't ask me why...