无法将类型为Microsoft.Office.Interop.Excel.ApplicationClass的COM对象转换为接口类型Microsoft.Office.Interop.Excel._Application

时间:2018-08-06 10:13:53

标签: c# excel asp.net-mvc

我正在研究一个Asp.net MVC项目,该项目具有一个功能,允许用户将excel文件上传到服务器,然后服务器将excel中的数据处理并保存到数据库中。标题,我遇到了以下错误:  无法将类型为“ Microsoft.Office.Interop.Excel.ApplicationClass”的COM对象转换为接口类型为“ Microsoft.Office.Interop.Excel._Application”。该操作失败是因为在COM组件上对具有IID'{000208D5-0000-0000-C000-000000000046}'的接口的QueryInterface调用由于以下错误而失败:RPC服务器不可用。 (来自HRESULT的异常:0x800706BA)。

到目前为止,我尝试了其他网站上所有的波纹管解决方案以及许多其他解决方案:     + 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。  而且所有这些都不起作用。

波纹管是我的功能:

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

0 个答案:

没有答案