简体   繁体   English

使用Interop在C#中打开Excel文件时出现异常

[英]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. 我有一个程序在服务器上运行,每天从网站下载.xls文件,打开它然后将其转换为.csv,以便能够正确解析它并添加到数据库。

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: 当我在我的本地PC上运行时,一切正常,它大部分时间都在服务器上工作,但也许每周一次或两次我得到这个例外:

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) 发生异常:System.Runtime.InteropServices.COMException(0x800A03EC):来自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. Excel安装在服务器上,尝试打开文件时发生异常。

Run> dcomcnfg 运行> 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 这将打开组件服务并导航到控制台根\\组件服务\\计算机\\我的电脑\\ DCOM配置\\ Microsoft Excel应用程序右键单击Microsoft Excel应用程序,然后选择属性

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(); 它特别与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. 如果可能的话,如果这是唯一需要excel的应用程序,请在Excel上执行Process.Kill() This helps if Excel.exe does not close properly. 如果Excel.exe未正确关闭,这会有所帮助。

Final, as stated in the comments above, Excel automation on the server is not supported. 最后,如上面的注释中所述,不支持服务器上的Excel自动化。 I would recommend looking in to SSIS for automation of excel. 我建议查看SSIS以获得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. 您可能需要将Microsoft Excel的DCOM标识设置为作为特定用户帐户运行,而不是交互式用户。

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 组件服务并导航到控制台根\\组件服务\\计算机\\我的计算机\\ DCOM配置\\ Microsoft Excel应用程序

Right click Microsoft Excel Application and select Properties 右键单击Microsoft Excel Application,然后选择“ 属性”

Under the Security tab, set to Customize on all and give access to Everyone (for testing) 在Security选项卡下,设置为allize on all并授予Everyone访问权限(用于测试)

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. 您可能正在尝试将文件保存到某个位置,而.NET没有写入该位置的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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