简体   繁体   中英

Exception When Opening Excel File in C# Using Interop

I have a program running on a server which downloads an .xls file from a website every day, opens it then converts it to a .csv to be able to parse it correctly and add to a database.

When I run it on my local PC everything works fine and it works most of the time on the server as well, but maybe once or twice a week I get this exception:

Exception occurred: System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC 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)

Here's a code snippet:

 Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook wbWorkbook = app.Workbooks.Open(_dir + _fileNameTrim + ".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            wbWorkbook.SaveAs(_dir + _fileNameTrim + ".csv", Microsoft.Office.Interop.Excel.XlFileFormat.xlCSVWindows, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            wbWorkbook.Close(false, "", true);

I've read many things online, a lot of solutions mention security settings but I don't think this is the case with me, otherwise it wouldnt work at all. People have also mentioned the file being corrupt. I'm really not sure. Any help would be much appreciated.

UPDATE:

Excel is installed on the server and the exception occurs when trying to open the file.

Run> dcomcnfg

This will open Component Services and navigate to Console Root \\Component Services\\Computers\\My Computer\\DCOM Config\\Microsoft Excel Application Right click Microsoft Excel Application and select Properties

Click on Identity Tab and check selection.

It works for me when select option - The interactive user

Please try, best luck!!

.Workpens.Open(strPath,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing ,Type.Missing,Type.Missing, Microsoft.Office.Interop.Excel.XlCorruptLoad.xlExtractData );

I have the same problem. It is specifically related to new Microsoft.Office.Interop.Excel.Application(); and what I have been doing is to create the file in a temp location where I know I don't have any permission issues. Once I am done creating the document, I then move it to the final destination, deleting the file created in my temp location.

So far I have not had any issues.

I suggest you create a log file and save it by date where it can log the steps it completes throughout your code. This way you can see where it fails.

If possible too, do a Process.Kill() on excel if this is the only application that requires excel. This helps if Excel.exe does not close properly.

Final, as stated in the comments above, Excel automation on the server is not supported. I would recommend looking in to SSIS for automation of excel.

UPDATE:

Make sure you test if the application runs even when no user is logged on. You might need to set the DCOM identity for Microsoft Excel to run as a specific user account and not the interactive user.

Go to:

Control Panel\\All Control Panel Items\\Administrative Tools

Click on

Component Services and navigate to Console Root\\Component Services\\Computers\\My Computer\\DCOM Config\\Microsoft Excel Application

Right click Microsoft Excel Application and select Properties

Under the Security tab, set to Customize on all and give access to Everyone (for testing)

Under the Identity tab, set This User and select the appropriate account (same account as the Task Scheduler is using to run your application)

Good Luck!

Did you check if it's a permission issue? It's possible that you are trying to save the file to a location and .NET doesn't have the permissions to write to that location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM