由于80070005访问,检索组件的COM类工厂失败在ASP.net MVC应用程序中被拒绝

时间:2017-09-22 14:51:28

标签: c# com office-interop excel-2016 clsid

嘿,我遇到了一个问题,即获得COM Office Excel调用以解决问题。

这是我的代码:

public async Task<bool> updateExcelSheets(string excelName, DateTime newDate, string excelPath)
{
   //Loop through all the data columns and update them
   int intX = 1;
   bool skipParseCol = true;
   Dictionary<string, string> resultsBack = new Dictionary<string, string>();
   Dictionary<string, string> _tmpParms = new Dictionary<string, string>();
   HELPer SQLHelper = new HELPer();
   DataSet allData = new DataSet();

   try
   {
       MSOIE.Application xlsApp = new MSOIE.Application();
       //                                          [FileName]
       //                                          |        [UpdateLinks]
       //                                          |        |    [ReadOnly]
       //                                          |        |    |    [Format]
       //                                          |        |    |    |     [Blanks]
       //                                          |        |    |    |    / \    [Ignore Read Only Recommended]
       //                                          |        |    |    |   |   |   |
       //                                          ▼        ▼    ▼    ▼   ▼   ▼   ▼
       MSOIE.Workbook wb = xlsApp.Workbooks.Open(excelPath, 0, false, 5, "", "", true);
       MSOIE.Sheets sheets = wb.Worksheets;
       MSOIE.Worksheet ws = (MSOIE.Worksheet)sheets.get_Item(2); //Selecting DDValues tab

       xlsApp.Visible = true;
       //More code here........
   }
   catch (Exception e)
   {
       Console.WriteLine("{0} Exception caught.", e.Message);
   }

   return Task.FromResult(true).IsCompleted;
}

代码到达 MSOIE.Application xlsApp = new MSOIE.Application(); 部分后,它会转到异常,e.Message会说:

  

由于以下错误,检索CLSID为{00024500-0000-0000-C000-000000000046}的组件的COM类工厂失败:80070005拒绝访问。 (HRESULT异常:0x80070005(E_ACCESSDENIED))。

我经常搜索其他修补程序,但似乎并不适用于我。我试过了:

1.In DCOMCNFG, right click on the My Computer and select properties.     
2.Choose the COM Securities tab.    
3.In Access Permissions, click Edit Defaults and add Network Service to it and give it Allow local access permission. Do the same for < Machine_name >\Users.    
4.In Launch and Activation Permissions, click Edit Defaults and add Network Service to it and give it Local launch and Local Activation permission. Do the same for < Machine_name >\Users.

信用:HERE

1.From the Start menu, click Run and type Dcomcnfg.exe.
2.In Component Services, click Console root, expand Component Services, expand Computers, expand My computer,expand DCOMConfig.
3.Search for Microsoft EXCEL 15.0 Object Library. Click on it.
4.Right click and select Properties.
5.On security tab, select Customize in "Launch and Activation" section.
6.Click edit and add identity of app pool under which you application is running. 
7.Repeat above step for "Access Permission"

信用:HERE

Go to your IIS Manager. → ApplicationPools → AdvancedSettings → Enable32BitApplication → True.

信用:HERE

现在奇怪的是,我只是做了一个正常的winform并且拥有与我的ASP.net MVC项目中的相同的代码,并且一旦到达 MSOIE就可以正常工作。应用程序xlsApp =新的MSOIE.Application(); 代码行,所以我真的无法弄清楚2会导致一个工作与另一个(具有相同的代码)之间的区别....

任何帮助都会很棒!谢谢!

0 个答案:

没有答案