最近有个项目需要从Excel中调用Web Service, Google 了一翻果然可以,总结如下:
一、安装开发包。
      要从Excel中通过宏调用Web Service,需要安装Microsoft Office 2003 Web Services Toolkit 2.01,可从这里下载,下载
二、调用Web Service
      安装好开发包以后,调用Web Service就变得很容易了,操作跟在VS 2003中差不多。切换到宏编辑器(Visual Basic Editor),从Tools菜单中点击“Web Service Reference”打开添加Web service引用的对话框。添加好引用后,开发包就会自动为你封装好一些用Soap调用Web Service的相关类。调用这些类中的相关方法就可以调用Web Service方法了。
三、读写SoapHeader内容
有些Web Service需要提供SoapHeader内容,比如通过SoapHeader提供帐号信息以供服务端验证客户身份。下面就是一个例子
服务端:
自定义SoapHeader类:
Call web service from excel    public class ServiceHead : System.Web.Services.Protocols.SoapHeader
    }

Web Service方法:
Call web service from excel[WebMethod(Description = "User name and password are reuired!")]
Call web service from excel[SoapHeader(
"accountHead")]
Call web service from excel
public MonthPriceCol GetPriceInfo(string pSymbol,string pBeginDate,string pEndDate)
}

客户端:
定义一个专门的类用于封装对SoapHeader的操作,类名为clsAccountHeader:
Call web service from excel
Call web service from excel
Option Explicit
Call web service from excel
Implements IHeaderHandler
Call web service from excel
在调用Web Service方法时提供SoapHeader内容:
End Sub

关于读SoapHeader的实现可参考婷篇文章:利用 SOAP 头保持 EJB 状态(http://www.ibm.com/developerworks/cn/webservices/ws-ejbsoap/index.html

相关文章: