当我在C#中读取(* .xls)文件时,出现错误。
我看到了很多有关“ DCOM访问被拒绝的问题”和SO中相关问题的配置的主题,但没有一个解决了我的问题。检查所有权限和管理员帐户。这是错误的图片:
更新:这是应用记录的错误:
Error : The remote procedure call failed. (Exception from HRESULT: 0x800706BE)
Stack Trace : 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)
at Pardic.WebProcess.frmGetPrice.worker_DoWork(Object sender, DoWorkEventArgs e)
^^-------------------------------------------------------------------^^
Source : Microsoft.Office.Interop.Excel
Method : Quit
Date : 02:21:19 ب.ظ
Time : 07/03/1398
Computer : DESKTOP-2QPBGQ1
Error : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
Stack Trace : at Microsoft.Office.Interop.Excel.ApplicationClass.Quit()
at Pardic.WebProcess.frmGetPrice.worker_DoWork(Object sender, DoWorkEventArgs e)
这是事件Windows日志:
Log Name: Application
Source: Application Error
Date: 5/28/2019 2:16:11 PM
Event ID: 1000
Task Category: (100)
Level: Error
Keywords: Classic
User: N/A
Computer: DESKTOP-2QPBGQ1
Description:
Faulting application name: EXCEL.EXE, version: 15.0.4420.1017, time stamp: 0x506741b5
Faulting module name: ntdll.dll, version: 10.0.14393.2214, time stamp: 0x5ac2f612
Exception code: 0xc0000005
Fault offset: 0x000000000004961c
Faulting process id: 0x1934
Faulting application start time: 0x01d5153a29e7eeb5
Faulting application path: C:\Program Files\Microsoft Office\Office15\EXCEL.EXE
Faulting module path: C:\Windows\SYSTEM32\ntdll.dll
Report Id: 9910317c-cd4c-49b2-8869-76f88b722123
Faulting package full name:
Faulting package-relative application ID:
Event Xml:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="Application Error" />
<EventID Qualifiers="0">1000</EventID>
<Level>2</Level>
<Task>100</Task>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2019-05-28T09:46:11.052916900Z" />
<EventRecordID>20076</EventRecordID>
<Channel>Application</Channel>
<Computer>DESKTOP-2QPBGQ1</Computer>
<Security />
</System>
<EventData>
<Data>EXCEL.EXE</Data>
<Data>15.0.4420.1017</Data>
<Data>506741b5</Data>
<Data>ntdll.dll</Data>
<Data>10.0.14393.2214</Data>
<Data>5ac2f612</Data>
<Data>c0000005</Data>
<Data>000000000004961c</Data>
<Data>1934</Data>
<Data>01d5153a29e7eeb5</Data>
<Data>C:\Program Files\Microsoft Office\Office15\EXCEL.EXE</Data>
<Data>C:\Windows\SYSTEM32\ntdll.dll</Data>
<Data>9910317c-cd4c-49b2-8869-76f88b722123</Data>
<Data>
</Data>
<Data>
</Data>
</EventData>
</Event>
这是已安装程序的列表:
public static System.Collections.Generic.List<Pardic.DataAwareCtrls.Progress.InsuranceDrugInfo> ReadResult(string fileAddress)
{
List<Pardic.DataAwareCtrls.Progress.InsuranceDrugInfo> retList = new List<Pardic.DataAwareCtrls.Progress.InsuranceDrugInfo>();
Microsoft.Office.Interop.Excel.Application oXL;
oXL = new Microsoft.Office.Interop.Excel.Application();
try
{
Microsoft.Office.Interop.Excel.Workbook theWorkbook = oXL.Workbooks.Open(fileAddress);
Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
foreach (Microsoft.Office.Interop.Excel.Worksheet item in sheets)
{
if (item.Name.Contains("Main"))
{
Microsoft.Office.Interop.Excel.Range ShtRange = item.UsedRange;
int CodeColumnNum = 0;
int PriceColumnNum = 0;
int NameColumnNum = 0;
int num = 0;
for (num = 1; num <= ShtRange.Columns.Count; num++)
{
try
{
string temp = string.Empty;
try
{
temp = ShtRange.Cells[1, num].Value2.ToString();
}
catch
{
temp = string.Empty;
}
if (temp.Contains("كد دارو"))
CodeColumnNum = num;
if (temp.Contains("تغيير قيمت"))
PriceColumnNum = num;
if (temp.Contains("نام دارو"))
NameColumnNum = num;
}
catch (Exception ex)
{
if (CodeColumnNum > 0 && PriceColumnNum > 0 && NameColumnNum > 0)
break;
oXL.Quit();
throw ex;
}
}
for (num = 2; num <= ShtRange.Rows.Count; num++)
{
Pardic.DataAwareCtrls.Progress.InsuranceDrugInfo drug = new Pardic.DataAwareCtrls.Progress.InsuranceDrugInfo();
string Codetemp = string.Empty;
try
{
Codetemp = ShtRange.Cells[num, CodeColumnNum].Value2.ToString();
}
catch
{
Codetemp = string.Empty;
}
string Pricetemp = string.Empty;
try
{
Pricetemp = ShtRange.Cells[num, PriceColumnNum].Value2.ToString();
}
catch
{
Pricetemp = string.Empty;
}
drug.Drug_Code = CodeAnalize(Codetemp);
drug.Drug_Price = PriceAnalize(Pricetemp);
drug.Drug_Name = ShtRange.Cells[num, NameColumnNum].Value2;
if (drug.Drug_Price != string.Empty && drug.Drug_Code != string.Empty)
retList.Add(drug);
}
}
}
theWorkbook.Save();
return retList;
}
catch //(Exception ex)
{
return null;
}
finally
{
oXL.Quit();
}
}
当我重新安装Windows和Office时出现此问题。安装办公室是2013年(办公室和Win均为64位)
更新:
答案 0 :(得分:3)
除非真正需要,否则请避免使用Interop!
对于Excel,您可以使用EPPlus,这是一个出色的C#库,用于读取/写入Excel文件。
它可以正常工作,并且不需要在PC上安装Excel。
编辑:由于从Interop到EPPlus的更改不是作者的选择,并且通过附加提供的错误信息,我想在回答中添加以下内容。
错误是“访问被拒绝”。所以我可以想到两个原因:
用户权限不足,无法打开该工作簿或启动Excel进程。
该工作簿正在被另一个进程(Excel,...)使用
对于1:检查执行用户是否可以访问工作簿并被允许启动excel。请注意,对于IIS Web应用程序,用户是“ IIS应用程序池用户”,而不是“管理员”。
对于2:这是汉斯之前提到的。确保没有其他Excel实例在运行。
答案 1 :(得分:0)
经过很多提示,工作和技巧后,我卸载了Foxit Reader软件,现在xls文件可以正常工作了。为什么Microsoft无法检测到此错误。