C# error when read Excel file, this is a strange error

2

When I read (*.xls) file in C# I get an error.

I see lots of topic about "DCOM Access Denied Problem" and configuration of related problem in SO, but non of them solved my problem. all permission and administrator account was checked. this is image of error:

Update: this is logged error by app :

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)

This is Event Windows logs:

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>

enter image description here

and this is list of installed program:

enter image description here enter image description here

    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();
            }
        }

this problem is appears when I reinstall windows and office. installation office is 2013 (both office and win are 64 bit)

Update:

c#
.net
excel
winforms
office-interop
asked on Stack Overflow May 28, 2019 by Akj • edited May 30, 2019 by Akj

2 Answers

4

Avoid Interop unless you really need it!

For Excel you can use EPPlus which is a great C# Library for read/write Excel-Files.

It works perfertly and does not require Excel to be installed on the PC.

EDIT: As changing from Interop to EPPlus is not an option for the author and with the additionally provided error information I'd like to add the folowwing to my answer.

The error is "Access Denied". So I can think of two causes:

  1. Insufficient rights for the user to open that workbook or to start an excel process.

  2. The workbook is in use by another process (Excel, ...)

For 1: check if the executing user can access the workbook and is allowed to start excel. Note that for an IIS-Webapplication the User is "IIS Application pool User" and not Admin.

For 2: This is what Hans mentioned before. Make sure no other instance of Excel is running.

answered on Stack Overflow May 28, 2019 by Vanice • edited May 28, 2019 by Vanice
2

After lot's of hints, works and tips, I Uninstall the Foxit Reader software and now the xls file is work perfectly. why Microsoft couldn't detect this error.

answered on Stack Overflow Jun 1, 2019 by Akj

User contributions licensed under CC BY-SA 3.0