大家好,我在使用 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.
信用:这里
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"
信用:这里
Go to your IIS Manager. → ApplicationPools → AdvancedSettings → Enable32BitApplication → True.
信用:这里
现在奇怪的是,我刚刚做了一个普通的 winform 并且具有与我的 ASP.net MVC 项目中相同的代码,并且一旦它到达那个MSOIE.Application xlsApp = new MSOIE.Application();它就可以正常工作。一行代码,所以我真的无法弄清楚这两个之间的区别是什么会导致一个工作和另一个(使用相同的代码)不是......
任何帮助都会很棒!谢谢!