1

I'm working on a Asp.net MVC project, which have a function that allow user to upload an excel file to server,server then process data from excel and save to database, It's working perffectly until to day,as the title, I met the follwing error: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA).

So far, I tried all the bellow solutions and many many other solutions from other site: +unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'". +Unable to cast COM object (EXCEL). +Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. +Unable to cast COM object of type 'Microsoft.Office.Interop.PowerPoint.ApplicationClass'. +: 'Unable to cast COM object of type 'System.__ComObject' to interface type. And all of them are not working.

bellow is my function :

[HttpPost]
    public ActionResult Index(HttpPostedFileBase avatarFile, string userId, int? delete_old_data)
    {
        string message = "0";
        if (avatarFile != null && avatarFile.ContentLength > 0)
        {
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook = null;
            try
            {
                if (delete_old_data == 1)
                {
                    int result = DatabaseController.Instant().deleteAllAccountExceptAdmin();
                }
                string path = Path.Combine(Server.MapPath("~/Images"),
                                           userId + Path.GetFileName(avatarFile.FileName));
                KillSpecificExcelFileProcess(Path.GetFileName(avatarFile.FileName));
                avatarFile.SaveAs(path);
                bool thereIsError = false;
                try
                {
                    excelWorkbook = excelApp.Workbooks.Open(path);
                }
                catch (Exception ex)
                {
                    thereIsError = true;
                    message = "1";
                }
                if (null != excelWorkbook)
                {
                    foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in excelWorkbook.Worksheets)
                    {
                        Microsoft.Office.Interop.Excel.Range range = sheet.UsedRange;
                        object[,] values = (object[,])range.Value2;
                        if (null != values)
                        {
                            for (int i = 15; i <= values.GetLength(0); i++)
                            {
                                user us = new user();
                                us.user_type = 1;
                                us.user_meeting_type = 0;
                                us.age = 0;
                                us.avatar = "adminavatar.png";
                                us.email = string.Empty;
                                us.password = "12345";
                                for (int j = 2; j <= values.GetLength(1); j++)
                                {
                                    try
                                    {
                                        if (null != values[i, j])
                                        {
                                            string s = values[i, j].ToString();
                                            Console.WriteLine("s = " + s + "---j = " + j);
                                            switch (j)
                                            {
                                                case 2:
                                                    us.user_login_name = s;
                                                    break;
                                                case 3:
                                                    us.office = s;
                                                    us.office_address = s;
                                                    us.full_name = s;
                                                    break;
                                                case 4:
                                                    try
                                                    {
                                                        us.status = Convert.ToInt32(s);
                                                    }
                                                    catch
                                                    {

                                                    }
                                                    break;
                                                case 5:
                                                    us.position = s;
                                                    break;
                                                default:
                                                    break;
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        message = "2";
                                        thereIsError = true;
                                    }
                                }
                                if (null != us)
                                {
                                    try
                                    {
                                        DatabaseController.Instant().addUser(us);
                                    }
                                    catch (Exception ex)
                                    {

                                    }
                                }
                            }
                        }
                        else
                        {
                            thereIsError = true;
                        }
                    }
                    try
                    {
                        excelWorkbook.Close();
                        excelApp.Workbooks.Close();
                        excelApp.Quit();
                        Marshal.ReleaseComObject(excelWorkbook);
                        Marshal.ReleaseComObject(excelApp.Workbooks);
                        Marshal.ReleaseComObject(excelApp);

                    }
                    catch
                    {

                    }
                }
                if (thereIsError)
                {

                }

            }
            catch (Exception ex)
            {
                if (null != excelWorkbook)
                {
                    try
                    {
                        excelWorkbook.Close();
                        excelApp.Workbooks.Close();
                        excelApp.Quit();
                        Marshal.ReleaseComObject(excelWorkbook);
                        Marshal.ReleaseComObject(excelApp.Workbooks);
                        Marshal.ReleaseComObject(excelApp);
                    }
                    catch
                    {

                    }
                }
                message = "3";
            }
        }
        return Redirect(Url.Action("AddUserView", "UserList", new { message }));
    }
user2905416
  • 404
  • 7
  • 21
  • 1
    Ok, there are some options you could check: 1) is the COM dll correctly registered? 2) Does the IIS user (assuming you use IIS) allowed to access and use the excel COM dll. 3) try this library: https://github.com/JanKallman/EPPlus – Stefan Aug 06 '18 at 10:26
  • 1
    I had similar problems when using Interop as a way of working with Excel. Everytime there was Windows Update my application would broke. At that time I had mix of different Office components. Main Office package was 2010 and I have used Visio version 2013. As Stefan suggested correct way would be to use epplus or Office Open Xml libraries. In that scenario you do not need Office on server. Btw, Microsoft strongly discourages such usage of Office. – Vladimir.RL Aug 06 '18 at 13:16
  • Thank you Stefan,Vladimir.RL for your reply,I'm using APPlus and it's working now. – user2905416 Aug 07 '18 at 03:58
  • Possible duplicate of [unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'"](https://stackoverflow.com/questions/28066719/unable-to-cast-com-object-of-type-microsoft-office-interop-excel-applicationcla) – HackSlash Aug 27 '19 at 17:57

0 Answers0