使用GET参数从URL打开Excel(XLS)文件

我正在尝试使用GET参数从HTTP链接打开XLS文件。 如果你只是复制并粘贴到你的网页浏览器的链接,你会看到它的作品。 如果我省略了GET参数,我可以用workbooks.open打开工作簿,但是它会打开错误的工作簿,因为您需要GET参数来精确地拉取我想要的内容。

Dim myURL As String Dim winHttpReq As Object Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1") myURL = "http://www.otcmarkets.com/common/ViewStockScreener.xls?otcMarketTier=&otcMarketTierDesc=&otcMarketTierGroup=&otcMarketTierId=&otcMarketTierGroupDesc=&allTierGroups=true&securityType=CORP&securityTypeDesc=Corporate%20Bond&countryId=&locale=&countryDesc=&localeDesc=&allLocales=true&sicIndustryIdentifier=" winHttpReq.Open "GET", myURL, False winHttpReq.Send MsgBox Len(winHttpReq.responseBody) result = winHttpReq.responseBody Dim x As Workbooks Set x = result x(1).Open 

感谢你的协助!

我相信,除非是SharePoint网站,否则无法直接从Excel中的URL打开文件。

我认为你使用WinHttpRequest是正确的,但是在打开文件之前,你需要将结果保存到本地磁盘上的一个文件中。

 Dim myURL As String Dim winHttpReq As Object Dim responseData() As Byte Dim fname As String Dim fno As Integer Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1") myURL = "http://www.otcmarkets.com/common/ViewStockScreener.xls?" & _ "otcMarketTier=&otcMarketTierDesc=&otcMarketTierGroup=&" & _ "otcMarketTierId=&otcMarketTierGroupDesc=&allTierGroups=true&" & _ "securityType=CORP&securityTypeDesc=Corporate%20Bond&countryId=&" & _ "locale=&countryDesc=&localeDesc=&allLocales=true&sicIndustryIdentifier=" winHttpReq.Open "GET", myURL, False winHttpReq.Send responseData = winHttpReq.responseBody fname = CurDur & "\ViewStockScreener.xls" fno = FreeFile Open fname For Binary As #fno Put #fno, 1, responseData Close #fno Workbooks.Open fname 

responseData之后的()将其声明为可变长度字节数组。 有必要首先将responseBody复制到原始字节数组中,因为直接将responseBody写入二进制文件会破坏数据(可能是某些字符编码问题)。

如果ViewstockScreener.xls名称由于GET请求而变得随机化,该怎么办

将响应数据写入文件时,您可以select所需的任何文件名。 如果保留服务器发回的文件名很重要,那实际上很难。 您必须查看响应头的Content-Disposition字段,然后从中parsing出文件名。