使用GetObject()方法从Excel VBA调用WCF Web服务[复制]

时间:2021-09-07 13:03:19

Possible Duplicate:
How do I call WCF client from Excel 2003 VBA?

可能重复:如何从Excel 2003 VBA调用WCF客户端?

I want to call a web service developed using WCF from the Excel using VBA code. How can we do this ? I have tried the GetObject() method but I'm getting syntax error while using this. I want to display the data that I'm getting from the web service in Excel. Please help. The DataContract is as follows: `

我想使用VBA代码调用使用WCF从Excel开发的Web服务。我们应该怎么做 ?我尝试过GetObject()方法,但是在使用它时遇到语法错误。我想显示我从Excel中的Web服务获取的数据。请帮忙。 DataContract如下:`

[DataContract] 
public class Data
{
    [DataMember]
    public int Id;
    [DataMember]
    public DateTime LockTime;
    [DataMember]
    public DateTime LoginTime;
    [DataMember]
    public DateTime LastDefenitionDate;
    [DataMember]
    public string NTLogin;
    [DataMember]
    public string SystemName;
}

ServiceContract is as follows:

ServiceContract如下:

`

`

[ServiceContract]

interface IDataService
{
    [OperationContract]
    List<Data> GetData();
    [OperationContract]
    void SubmitData(Data data);
}

`

`

DataService for accessing database is as follows:

用于访问数据库的DataService如下:

`

`

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

public class DataService : IDataService { public static SQLConnection SQLDBConnection = new SQLConnection();

public class DataService:IDataService {public static SQLConnection SQLDBConnection = new SQLConnection();

    #region IDataService Members

    public List<Data> GetData()
    {
        List<Data> datas = new List<Data>();
        try
        {
            if (SQLDBConnection.con.State == ConnectionState.Closed) SQLDBConnection.con.Open();
            datas.Clear();
            SqlCommand sqlcommand = new SqlCommand();
            sqlcommand.Connection = SQLDBConnection.con;
            sqlcommand.CommandText = "select * from tblData";
            sqlcommand.CommandType = CommandType.Text;
            SqlDataAdapter sqladapter = new SqlDataAdapter(sqlcommand);
            DataTable dt = new DataTable();
            sqladapter.Fill(dt);

            Data data = null;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                data = new Data();
                data.Id = Convert.ToInt32(dt.Rows[i]["id"]);
                data.NTLogin = dt.Rows[i]["NTLogin"].ToString();
                data.SystemName = dt.Rows[i]["SystemName"].ToString();
                data.LockTime = Convert.ToDateTime(dt.Rows[i]["LockTime"]);
                data.LoginTime = Convert.ToDateTime(dt.Rows[i]["LoginTime"]);
                data.LastDefenitionDate = Convert.ToDateTime(dt.Rows[i]["LastDefenitionDate"]);
                datas.Add(data);
            }
        }
        catch (Exception ex)
        {  } 
        return datas;
    }

    public void SubmitData(Data data)
    {
        if (SQLDBConnection.con.State == ConnectionState.Closed) SQLDBConnection.con.Open();
        SqlCommand sqlcommand = new SqlCommand();
        sqlcommand.Connection = SQLDBConnection.con;
        sqlcommand.CommandText = "Insert into dbo.tblData(NTLogin, SystemName, LockTime, LoginTime, LastDefenitionDate) values ('" + data.NTLogin + "','" + data.SystemName + "','" + data.LockTime + "' , '" + data.LoginTime + "', '" + data.LastDefenitionDate + "')";
        sqlcommand.CommandType = CommandType.Text;
        int RowsAffected = sqlcommand.ExecuteNonQuery();
    }

    #endregion
}

`

`

EDIT:

编辑:

The possible duplicate suggestion's answer didn't worked out for me. My entire code is given here. I have even checked that post, before posting my question. Please check my code.

可能的重复建议的答案对我来说没有成功。我的完整代码在这里给出。在发布我的问题之前,我甚至检查过那篇文章。请检查我的代码。

1 个解决方案

#1


2  

Read following article, it describes in details how to call WCF service from VBA code using GetObject:

阅读以下文章,它详细描述了如何使用GetObject从VBA代码调用WCF服务:

Calling WCF Services from Excel VBA clients using the WCF Service Moniker
http://damianblog.com/2009/07/05/excel-wcf/

使用WCF Service Moniker从Excel VBA客户端调用WCF服务http://damianblog.com/2009/07/05/excel-wcf/

But it works only for simple WCF service contracts. For more complex things you need to use VSTO

但它仅适用于简单的WCF服务合同。对于更复杂的事情,您需要使用VSTO

UPD: In such case, when using Moniker with MEX contracts, you should use only primitive types, and arrays of primitive types. When you need to use complex type, either e.g. try to pack them into string or use more advanced techniques like WCF Moniker with COM Clients ( http://msdn.microsoft.com/en-us/library/ms752245.aspx) or VSTO.

UPD:在这种情况下,当使用带有MEX契约的Moniker时,应该只使用原始类型和基本类型数组。当您需要使用复杂类型时,例如尝试将它们打包成字符串或使用更高级的技术,如带有COM客户端的WCF Moniker(http://msdn.microsoft.com/en-us/library/ms752245.aspx)或VSTO。

#1


2  

Read following article, it describes in details how to call WCF service from VBA code using GetObject:

阅读以下文章,它详细描述了如何使用GetObject从VBA代码调用WCF服务:

Calling WCF Services from Excel VBA clients using the WCF Service Moniker
http://damianblog.com/2009/07/05/excel-wcf/

使用WCF Service Moniker从Excel VBA客户端调用WCF服务http://damianblog.com/2009/07/05/excel-wcf/

But it works only for simple WCF service contracts. For more complex things you need to use VSTO

但它仅适用于简单的WCF服务合同。对于更复杂的事情,您需要使用VSTO

UPD: In such case, when using Moniker with MEX contracts, you should use only primitive types, and arrays of primitive types. When you need to use complex type, either e.g. try to pack them into string or use more advanced techniques like WCF Moniker with COM Clients ( http://msdn.microsoft.com/en-us/library/ms752245.aspx) or VSTO.

UPD:在这种情况下,当使用带有MEX契约的Moniker时,应该只使用原始类型和基本类型数组。当您需要使用复杂类型时,例如尝试将它们打包成字符串或使用更高级的技术,如带有COM客户端的WCF Moniker(http://msdn.microsoft.com/en-us/library/ms752245.aspx)或VSTO。