首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

.net组件COM Interop注册的有关问题:Excel中无法调用自定义函数

2012-01-11 
.net组件COM Interop注册的问题:Excel中无法调用自定义函数我用C#制作了一个Excel的自定义函数插件,用来从

.net组件COM Interop注册的问题:Excel中无法调用自定义函数
我用C#制作了一个Excel的自定义函数插件,用来从数据库中取数.程序在开发的机子上没有什么问题,但到客户的机器上就有问题了,不能加载.

自定义函数插件项目的生成属性上选择了 "为Com   Interop注册 ",所以在开发的机器上能正常运行,但在客户机上我用如下命令进行注册,却不能运行:
Regasm   ngxs.dll   /tlb   /codebase
类型库和类型都注册了,在Excel的工具菜单下选择 "加载宏 "-> 自动化,
能找到我的组件,选择后,提示 "找不到加载宏 "mscoree.dll ",从列表中删除吗? ",选否,不删除,但在Excel的公式列表中找不到函数,也没有数据库登陆对话框出现,说明组件没有正常加载.而在开发的机子上,这是可以正常取数运行的.

所以我认为生成下面的 "为Com   Interop注册 "和使用Regasm命令进行注册是有差异的,
那我该怎么进行注册呢,请各位务必指定一二!谢谢各位了.

以下是Excel函数的制作方法:

在VS2005中新建C#类库项目,选择生成属性为 "为Com   Interop注册 ",
然后所有要向Excel暴露的函数类写成如下形式:
using   System;
using   System.Collections.Generic;
using   System.Text;
using   System.Runtime.InteropServices;
using   Microsoft.Win32;
using   System.Windows.Forms;
using   System.Data;
using   System.Data.SqlClient;
namespace   NG新中大
{
       
        [ClassInterface(ClassInterfaceType.AutoDual),   ComVisible(true)]
        public   class   提货单
        {
                private   static   string   m_OrderNo;
                private   DataTable   m_Hts;


                #region   注册
                [ComRegisterFunctionAttribute]
                public   static   void   RegisterFunction(Type   type)
                {
                        Registry.ClassesRoot.CreateSubKey(GetSubKeyName(type));
                }

                [ComUnregisterFunctionAttribute]
                public   static   void   UnregisterFunction(Type   type)
                {
                        Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(type),   false);
                }

                private   static   string   GetSubKeyName(Type   type)
                {
                        string   s   =   @ "CLSID\{ "   +   type.GUID.ToString().ToUpper()   +   @ "}\Programmable ";
                        return   s;
                }
                #endregion   注册


                public   提货单()
                {
                        if   (Conn.conn   ==   null   &&   !Conn.b_notlink)
                        {
                                Conn   temp   =   new   Conn();


                                temp.连接();
                                Conn.b_notlink   =   true;
                        }
                }
                public   string   T日期(string   提货单号)
                {
                        try
                        {
                                if   (Conn.conn   ==   null   ||   Conn.conn.State   !=   ConnectionState.Open)
                                        return   "无联接 ";
                                SqlCommand   comm   =   new   SqlCommand( "Select   rq   From   x_fh   Where   pjh=@pjh ",   Conn.conn);
                                comm.Parameters.Add( "@pjh ",   SqlDbType.VarChar,   30);
                                comm.Parameters[ "@pjh "].Value   =提货单号;
                                object   o   =   comm.ExecuteScalar();
                                if   (o   ==   DBNull.Value   ||   o   ==   null)
                                        return   null;
                                else
                                        return   ((DateTime)o).ToShortDateString();
                        }
                        catch   (Exception   err)
                        {
                                return   "异常 ";
                        }
                }
}

以上是从网上找到的做法,各位高手看看注册上存在什么问题?

[解决办法]
JF UP
[解决办法]
客户机装了.net 框架没啊
[解决办法]


路过,不太懂,只能帮顶一下
[解决办法]
excel 插件只懂些皮毛,没有深入了解过
考虑两点
1、office 的.net支持安装没
2、安全性问题

热点排行