Skip to content

Common Issues When Using Microsoft.Office.Interop.Excel in .NET Projects

TLDR

  • dotnet publish does not support COM component compilation; it is recommended to separate the conversion service into an independent .NET Framework Web API.
  • If a 0x80070005 error occurs at runtime, you must grant DCOM access permissions to the IIS Application Pool account via dcomcnfg.
  • If a "file not found" error occurs, you must manually create the Desktop folder in the system directory for the Office component to access.
  • This solution is limited to Windows environments, and Microsoft Office must be installed on the server.

In .NET projects, if you need to convert Excel files to ODS format, a common approach is to use Microsoft.Office.Interop.Excel to call the Office software on the host to perform a "Save As" operation. While this works fine in a development environment, you will often encounter compilation and permission issues when deploying to a server.

dotnet publish Compilation Exceptions

When this issue occurs: When the project is published using the dotnet publish command and references COM components.

Since dotnet publish uses the MSBuild built into the .NET SDK, it does not support the ResolveComReference task, leading to compilation failure with error MSB4803.

Recommendation: As there is currently no direct solution for this, it is recommended to extract the ODS conversion functionality and create an independent .NET Framework Web API service. When the system needs to convert formats, call this API to process the request, thereby bypassing the limitations of the .NET Core/5+ environment regarding COM components.

Runtime Error Handling Workflow

1. Insufficient DCOM Component Permissions

When this issue occurs: When an IIS application attempts to launch an Excel COM object, but the Application Pool account lacks sufficient DCOM permissions, a 0x80070005 (E_ACCESSDENIED) error is thrown.

Steps to resolve:

  1. Press Win + R and enter dcomcnfg to open Component Services.
  2. Expand in order: Component Services > Computers > My Computer > DCOM Config.
  3. Find Microsoft Excel Application, right-click it, and select "Properties".
  4. Switch to the "Security" tab and click "Customize" in the "Access Permissions" section.
  5. Add the corresponding IIS APPPOOL\{Application Pool Name} to the user list.
  6. Check the "Local Access" and "Remote Access" permissions.

2. Excel File Access Issues

When this issue occurs: The Excel COM component attempts to access the user's Desktop folder during execution, but this path does not exist by default for Windows Server system service accounts.

Steps to resolve: You must manually create the corresponding Desktop folder based on the installed Office bit version:

  • 32-bit Office: C:\Windows\SysWOW64\config\systemprofile\Desktop
  • 64-bit Office: C:\Windows\System32\config\systemprofile\Desktop

Important Notes

  • This configuration is only applicable to Windows environments.
  • Microsoft Office must be installed on the server side.
  • It is recommended to re-check the aforementioned permissions and directory settings after every Windows update.

Change Log

  • 2024-10-21 Initial version created.