35

I just created a simple application and hosted in IIS6.0. In code I'm just instantiating excel objects.

using excel = Microsoft.Office.Interop.Excel.Application;

namespace TestHosting
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            excel excelObj=new Microsoft.Office.Interop.Excel.Application();
        }
    }
}

Its giving me following error

 "Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied." (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

System Configuration:

Windows server 2008,enterprise edition with 64 bit. service pack 2

I tried with many possible solutions found on internet,but none of them were worked for me.

Some solution I tried are below

1) creating Desktop application under the path "C:\Windows\SysWOW64\config\systemprofile" 2)setting full permission/control for Microsfot.EXcelApplicaiton in DCOMCNFG 3)Killing all excel instances in Task Manager

Please help me in this regard,this is just sample app but in my original app is mostly about read and writing data from/to excel.

bot
  • 4,841
  • 3
  • 42
  • 67
Ram
  • 913
  • 5
  • 17
  • 35
  • what about aps.net authentication? is it enabled? – Juliusz Jul 22 '13 at 10:52
  • Do you have Excel installed on the server? The interop interfaces you're using are used to _automate_ Excel which requires that the Excel application is installed there. Each page request cycle will potentially launch a separate instance of excel.exe. I would strongly advise against doing this as part of a web application. Why do you want to do this? If you are wanting to generate Excel documents, there are _much_ better ways to do this such as OpenXML. – Richard Cook Aug 07 '13 at 00:35
  • 2
    there was problem with permissions. I fixed it..thanks everyone – Ram Apr 14 '14 at 09:35
  • 1
    @Ram Im facing the same issue. Can you explain which permissions you fixed? – Tk1993 Mar 01 '17 at 11:31

14 Answers14

55
  1. Make sure that you have Office runtime installed on the server.
  2. If you are using Windows Server 2008 then using office interops is a lenghty configuration and here are the steps.

Better is to move to Open XML or you can configure as below

  • Install MS Office Pro Latest (I used 2010 Pro)
  • Create User ExcelUser. Assign WordUser with Admin Group
  • Go to Computer -> Manage
  • Add User with below options
  • User Options Password Never Expires
  • Password Cannot Be Change

Com+ Configuration

  • Go to Control Panel - > Administrator -> Component Services -> DCOM Config
  • Open Microsoft Word 97 - 2003 Properties
  • General -> Authentication Level : None
  • Security -> Customize all 3 permissions to allow everyone
  • Identity -> This User -> Use ExcelUser /password
  • Launch the Excel App to make sure everything is fine

3.Change the security settings of Microsoft Excel Application in DCOM Config.

Controlpanel --> Administrative tools-->Component Services -->computers --> myComputer -->DCOM Config --> Microsoft Excel Application.

Right click to get properties dialog. Go to Security tab and customize permissions

See the posts here: Error while creating Excel object , Excel manipulations in WCF using COM

bot
  • 4,841
  • 3
  • 42
  • 67
42

Sometimes, you create new application pool and cann't solve it via DCOMCNFG. Then there is another method to do:

Set the Identity (Model Name) of application pool to LocalSystem.

I don't know the root cause, but it solve my problem one time.

Luis Sérgio
  • 629
  • 6
  • 8
  • 5
    This allows the application to be run with higher privileges and in most cases will fix the issue easily. +1 for the answer on that one. However, remember that running an application pool under an account that has high-level user rights is a **serious security risk**. More info on this [on MSDN](https://technet.microsoft.com/nl-nl/library/cc771170(v=ws.10).aspx). So, I would advise only changing the identity if you're comfortable with the risks and know what the impact could be. – Juliën May 08 '15 at 15:12
  • 3
    i would have really committed suicide, if a genius (in form of you) wasn't born... salute you man... you saved me... – Abhishek Singh May 18 '16 at 00:54
  • I tried every other solutions around including all of the DCOMCnfg -32, and have no luck until I apply this one. – RAY Feb 13 '19 at 03:14
  • This worked for me. For changing identity of application pool, Open the IIS Management Console (INETMGR.MSC). Open the Application Pools node underneath the machine node. Select the application pool you want to change to run under **Local System** identity. Right click the application pool and select Advanced Settings Select the Identity list item and click the ellipsis (the button with the three dots).[msdn](https://learn.microsoft.com/en-us/iis/manage/configuring-security/application-pool-identities) – Ali Shan Jul 20 '19 at 05:41
  • +1000 points - I was looking for 3 days now and it didn't made any sense - THANKS – Edi Sep 07 '20 at 11:29
  • Still the problem exists in my case . I'm really about to commit suicide – Sachintha Nayanajith Oct 16 '20 at 10:40
  • This worked for me in addition of https://stackoverflow.com/questions/30965150/microsoft-office-excel-cannot-access-the-file-on-iis => Create the C:\Windows\SysWOW64\config\systemprofile\Desktop folder – Flou Oct 14 '22 at 16:22
12

Too late to respond. But, if this helps someone who is still facing the issue. I got this fixed by:

→ Set site on dedicated pool instead of shared one.

→ Enable 32 bit application support.

→ Set identity of the application pool to LocalSystem.

Hardik V.
  • 374
  • 2
  • 9
  • Thank you. For me, in my local test environment, all I needed to do was step 3: Set identity of the application pool to LocalSystem. – Tyler Forsythe Jan 15 '20 at 22:06
  • To LocalSystem, then it essentially runs with kernel permissions. That is killing a musquito with a bazooka. Please don't do that and follow the DCOM suggestion above. – Sebazzz Sep 01 '21 at 11:54
5
  1. give the read / write permission to the IIS user or group users

  2. Start -> run -> inetmgr

    enable the ASP.NET authentication for your default website

3. For 64-bit (x64), create this folder: C:\Windows\SysWOW64\config\systemprofile\Desktop

For 32-bit (x86), create this folder: C:\Windows\System32\config\systemprofile\Desktop

The windows service, if running under the systemprofile, needs the Desktop folder. This folder was automatically created on XP and older Windows Server versions, but not for Vista and Windows 2008 Server.

  • 1
    thanks for reply,2 and 3 steps I tried, but regarding 1 step,where should I give permissions for IIS users or group users? – Ram Jul 22 '13 at 12:59
  • On Windows Server 2012 R2 64-bit and Excel with Office 365 64-bit, I created the 64-bit folder and still had the issue. After 2 days of troubleshooting I tried creating the 32-bit folder as well and it immediately worked. – Koops128 Mar 27 '23 at 21:43
5

Personally, I ran these exact steps:

* Install Interop Assemblies: you can install from Microsoft's website https://www.microsoft.com/en-us/download/details.aspx?id=3508&tduid=(09cd06700e5e2553aa540650ec905f71)(256380)(2459594)(TnL5HPStwNw-yuTjfb1FeDiXvvZxhh.R.Q)()

* Check assemblies version: check the version of the assemblies on development and production machines. The assemblies will be in the GAC, in widows 7 this folder is %windir%\assembly.

* Create a Desktop folder: the service uses the desktop folder under systemprofile so you will need to create this folder if not there, here is the location of the folder: For 64 bit applications : C:\Windows\SysWOW64\config\systemprofile\Desktop For 32 bit applications : C:\Windows\System32\config\systemprofile\Desktop

* Add DCOM user permissions:
---start the run window and type 'dcomcnfg'.
---Expand Component Services –> Computers –> My Computer –> DCOM Config.
---Look for Microsoft Excel Application. Right click on it and select properties, then select the Security tab.
---Select the Customize radio button under 'Launch and Activation Permissions' and 'Access Permission' and click the Edit button for both to add users as follows.
---------Click the Add button and users 'IIS_IUSRS' and 'NETWORK SERVICE' and give them full privileges.
---Go to the Identity tab and select "The interactive user" option.
---Click Apply and OK.

IsaacBok
  • 424
  • 5
  • 18
3

solution that worked for me is to change user under which Application Pool is started (ApplicationPoolIdentity obviously has not enough rights to access COM object).

Good luck!

Milos
  • 31
  • 1
  • 1
    I figured out that this worked as well, but then I thought perhaps a better solution would be to set the permissions on the DCOM object(s) themselves, [kind of like this link](http://tanveeronline.blogspot.com/2012/05/component-with-clsid-xxx-failed-due-to.html) – Adam Nofsinger Oct 22 '13 at 16:21
3

try to do the following:

  1. Make sure Office interop assemblies are installed.
  2. Check the version of the assemblies on development and production.
  3. Create Desktop folder under systemprofile.
  4. set the DCOM security explicitly for the service user.

you can find more details here

user3140982
  • 135
  • 1
  • 3
  • 8
2

I just created a simple application

What is the target processor of the application? I am guessing it is x86 - so either set it to x64 or anycpu.

I think the issue you are having is due to a 64bit process trying to access a 32bit dll.

Also, if you can't change the target processor you could try to enable 32-bit Applications from the Application Pools settings in IIS.

Fraser
  • 15,275
  • 8
  • 53
  • 104
2

Do you have Excel installed on the server? The interop interfaces you're using are used to automate Excel which requires that the Excel application is installed there. Each page request cycle will potentially launch a separate instance of excel.exe. I would strongly advise against doing this as part of a web application.

Why do you want to do this? If you are wanting to generate Excel documents, there are much better ways to do this, including OpenXML as mentioned elsewhere on this thread. Please do not run Excel on the server!

Link to OpenXML SDK download: http://www.microsoft.com/en-us/download/details.aspx?id=5124

Richard Cook
  • 32,523
  • 5
  • 46
  • 71
1

This looks like a permission problem. I suggest running processMonitor to determine what permissions are needed.

Mike
  • 3,462
  • 22
  • 25
1

use NPOI to read and write excel..it's free and open source

for exapmle

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

//.....

private void button1_Click(object sender, EventArgs e)
{
    HSSFWorkbook hssfwb;
    using (FileStream file = new FileStream(@"c:\test.xls", FileMode.Open, FileAccess.Read))
    {
        hssfwb= new HSSFWorkbook(file);
    }

    ISheet sheet = hssfwb.GetSheet("Arkusz1");
    for (int row = 0; row <= sheet.LastRowNum; row++)
    {
        if (sheet.GetRow(row) != null) //null is when the row only contains empty cells 
        {
            MessageBox.Show(string.Format("Row {0} = {1}", row, sheet.GetRow(row).GetCell(0).StringCellValue));
        }
    }
}  

see this Stackoverflow question

Community
  • 1
  • 1
sangram parmar
  • 8,462
  • 2
  • 23
  • 47
1

Came across this issue two days back, spent whole complete two days, So I found that I need to give the access to IUSR user group at DCOMCNFG --> My Computer Properties --> Com Security --> Launch and Activation Permissions --> Edit defaults and give all rights to IUSR.

hope it will help someone....

harshal H
  • 11
  • 2
0

Use this

create a user in admin group

add this user details in web config like below

<system.web>
   <identity impersonate="true"
    userName="User Name"
    password="Password" />
    `enter code here`</system.web>

restart the IIS and check again

-tony-

Tony
  • 21
  • 4
0

If you are trying to configure this on 64bit, you have to do your DCOMconfig configuration (see other answers above) in:

C:\WINDOWS\SysWOW64>mmc comexp.msc /32

according to Setting Process-Wide Security Using DCOMCNFG.

After this, I was able to configure this for IIS_IUSRS without administrator privileges.

Greenonline
  • 1,330
  • 8
  • 23
  • 31
Jan
  • 11
  • 1