无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型

.Net技术 码拜 9年前 (2015-04-02) 4311次浏览 0个评论
 

无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Excel.Application”。此操作失败的原因是对 IID 为“{000208D5-0000-0000-C000-000000000046}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 加载类型库/DLL 时出错。 (异常来自 HRESULT:0x80029C4A (TYPE_E_CANTLOADLIBRARY))。

无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型
 protected bool IsExistExecl()
        {
            try
            {
                xlApp = new Excel.Application();
                if (xlApp == null)
                {
                    returnMessage = “无法创建Excel对象,可能您的计算机未安装Excel!”;
                    return false;
                }
            }
            catch (Exception ex)
            {
                returnMessage = “请正确安装Excel!”;
                //throw ex;
                return false;
            }

            return true;
        }

无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型
在这个地方出现错误 xlApp = new Excel.Application();
无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型
using Excel = Microsoft.Office.Interop.Excel;
无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型
Excel.Application xlApp = new Excel.Application(); 
当然应该是这样用,xlApp要赋值成Excel.Application的实例,它自己必须是个Excel.Application类型的对象
你不能先把他声明成COM对象啊
无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型
        private Excel.Application xlApp;
无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型
using用法
要么写在类外面,最前面
using system.IO;

要么写成
using()
{
}
的形式

无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Diagnostics;
using System.Data.OleDb;
using System.Windows.Forms;

namespace QRCode
{
    public class ExcelIO : IDisposable
    {
        #region Constructors
        private ExcelIO()
        {
            status = IsExistExecl() ? 0 : -1;
        }

        public static ExcelIO GetInstance()
        {
            //if(instance == null)
            //{
            //    lock (syncRoot)
            //    {
            //         if(instance == null)
            //         {
            //            instance = new ExcelIO();
            //         }
            //    }
            //}
            //return instance;
            return new ExcelIO();
        }
        #endregion

        #region Fields
        private static ExcelIO instance;
        private static readonly object syncRoot = new object();
        private string returnMessage;
        private Excel.Application xlApp;
        private Excel.Workbooks workbooks = null;
        private Excel.Workbook workbook = null;
        private Excel.Worksheet worksheet = null;
        private Excel.Range range = null;
        private int status = -1;
        private bool disposed = false;//是否已经释放资源的标记
        #endregion

        #region Properties
        /// <summary>
        /// 返回信息
        /// </summary>
        public string ReturnMessage
        {
            get { return returnMessage; }
        }

        /// <summary>
        /// 状态:0-正常,-1-失败 1-成功
        /// </summary>
        public int Status
        {
            get { return status; }
        }
        #endregion

        #region Methods
        /// <summary>
        /// 判断是否安装Excel
        /// </summary>
        /// <returns></returns>
        protected bool IsExistExecl()
        {
            try
            {
                xlApp = new Excel.Application();
                if (xlApp == null)
                {
                    returnMessage = “无法创建Excel对象,可能您的计算机未安装Excel!”;
                    return false;
                }
            }
            catch (Exception ex)
            {
                returnMessage = “请正确安装Excel!”;
                //throw ex;
                return false;
            }

            return true;
        }

无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型
using Excel = Microsoft.Office.Interop.Excel;
没见过这种写法,不报错?
改成
using Microsoft.Office.Interop.Excel;
无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型
10分
COM错误:无法加载dll
如果确认已安装了excel,看看它的版本和你在自己机器上引用的Excel组件版本一致
其实你可以在catch块里多一道动态创建的操作

try
            {
                xlApp = new Excel.Application();
                if (xlApp == null)
                {
                    returnMessage = "无法创建Excel对象,可能您的计算机未安装Excel!";
                    return false;
                }
            } 
            catch
            {
                      try
                      {
                                var typeCLSID = Guid.Parse("000208D5-0000-0000-C000-000000000046");
                                type = System.Type.GetTypeFromCLSID(typeCLSID);
                                object excelComObj = Activator.CreateInstance(type); 
                      }
                      catch (Exception ex)
                       {
                             returnMessage = "请正确安装Excel!";
                             //throw ex;
                              return false;
                        }
            }

只给你思路,上面的GUID和progID可以通过 控制面板 > 管理工具 > 组件服务 -> DCOM 中找到(找不到则说明没有安装成功)
object excelComObj = Activator.CreateInstance(type); 
//或者你也可以通过ProID,也就是COM组件名的方式创建
object excelComObj = Marshal.GetActiveObject(“Microsoft Excel Application”);  //VB中也可以用GetObject()
object也可以用Net4.0之后的动态类型
否则你会由于C#编译时的类型检查,使用组件里的函数时非常麻烦(不停地反射),当然用VB.Net也会很方便

无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型
30分
引用 楼主 qq_18870477 的回复:

无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Excel.Application”。此操作失败的原因是对 IID 为“{000208D5-0000-0000-C000-000000000046}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 加载类型库/DLL 时出错。 (异常来自 HRESULT:0x80029C4A (TYPE_E_CANTLOADLIBRARY))。

不看代码,先查环境
先看看office安装时excel的.net编程支持支持是否安装,
如果是精简版或者绿色版的office往往出问题;
其次,查下程序引用的版本和本地的版本是否一致,
不一致,重新添加引用。

无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型
标题: Microsoft SQL Server Management Studio
——————————

无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型“Microsoft.VisualStudio.OLE.Interop.IServiceProvider”。此操作失败的原因是对 IID 为“{6D5140C1-7436-11CE-8034-00AA006009FA}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 不支持此接口 (异常来自 HRESULT:0x80004002 (E_NOINTERFACE))。 (Microsoft.VisualStudio.OLE.Interop)

程序位置:

   在 Microsoft.VisualStudio.OLE.Interop.IServiceProvider.QueryService(Guid& guidService, Guid& riid, IntPtr& ppvObject)
   在 Microsoft.VisualStudio.Shell.ServiceProvider.GetService(Guid guid, Type serviceType)
   在 Microsoft.VisualStudio.Shell.ServiceProvider.GetService(Type serviceType)
   在 Microsoft.VisualStudio.Shell.WindowPane.GetService(Type serviceType)
   在 Microsoft.SqlServer.Management.SqlStudio.Explorer.ObjectExplorerToolWindow.GetService(Type serviceType)
   在 Microsoft.VisualStudio.Shell.WindowPane.System.IServiceProvider.GetService(Type serviceType)
   在 System.ComponentModel.Design.ServiceContainer.GetService(Type serviceType)
   在 Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.ObjectExplorerControl.GetService(Type serviceType)
   在 Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.ObjectExplorerControl.System.IServiceProvider.GetService(Type serviceType)
   在 System.ComponentModel.Design.ServiceContainer.GetService(Type serviceType)
   在 Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.NavigableItem.GetService(Type serviceType)
   在 Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.RootNavigableItem.GetService(Type serviceType)
   在 Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.NodeContext.GetService(Type serviceType)
   在 Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.NavigableItemBuilder.<GetColumnsFromNavigationService>d__f.MoveNext()
   在 Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.NavigableItemBuilder.AddFields(List`1 list, IEnumerable`1 fields, IDictionary`2 allValidFields, AddFieldsFlags flags)
   在 Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.NavigableItemBuilder.GetFields(INodeInformation source, Dictionary`2& allValidColumns)
   在 Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.NavigableItemBuilder.BuildDynamicItemWithQuery(IList`1 nodes, INodeInformation source, INavigableItem sourceItem, String urnQuery, Boolean registerBuilder, Boolean registerBuiltItems)
   在 Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.NavigableItemBuilder.BuildDynamicItem(IList`1 nodes, INodeInformation source, INavigableItem sourceItem, IFilterProvider filter)
   在 Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.NavigableItemBuilder.Build(IList`1 nodes, INodeInformation source, INavigableItem sourceItem, IFilterProvider filter)
   在 Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.NavigableItem.RequestChildren(IGetChildrenRequest request)
   在 Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.ExplorerHierarchyNode.BuildChildren(WaitHandle quitEvent)


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!