vba 将数据从 Excel 发送到网站

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/954087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:28:19  来源:igfitidea点击:

Sending Data from Excel to a Website

excel-vbavbaexcel

提问by

I have been asked to make a Macro which sends the Excel Data to a Website. There should not be any database involved. I have been trying to use HTTP Post after reading examples on this website. I have made a ASP.NET webpage which runs on localserver. While debugging the macro, the control does reach the webpage Page_load event but I am unable to see the data there.

我被要求制作一个将 Excel 数据发送到网站的宏。不应该涉及任何数据库。在阅读了本网站上的示例后,我一直在尝试使用 HTTP Post。我制作了一个在本地服务器上运行的 ASP.NET 网页。在调试宏时,控件确实到达了网页 Page_load 事件,但我无法在那里看到数据。

Can anybody help me find my mistake?

有人能帮我找出我的错误吗?

Excel Code:

excel代码:

Sub SendData()
'
' SendData Macro 
' 
Dim sdata As String 
sdata = "Abhh" 
' 
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP") 
URL = "localhost:2782/Default.aspx?"; + sdata 
objHTTP.Open "POST", URL, False objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" 
objHTTP.send ("") 
End Sub

C# Code

C# 代码

 using System; 
 using System.Data; 
 using System.Configuration; 
 using System.Collections; 
 using System.Web; 
 using System.Web.Security; 
 using System.Web.UI; 
 using System.Web.UI.WebControls; 
 using System.Web.UI.WebControls.WebParts; 
 using System.Web.UI.HtmlControls; 

 namespace Fetch 
 { 
      public partial class _Default : System.Web.UI.Page 
      { 
           protected void Page_Load(object sender, EventArgs e) 
           { 
                Label1.Text = Request.Url.Query;
                // I dont know where the data is in the Request object.(If it is there at all..) 
           } 
      } 
 }

Thanks

谢谢

Abi

阿比

回答by mundeep

Does the 'website' have to be a standard webpage? Can you not send it to a 'website' running an ASP.NET WebService instead?

“网站”必须是标准网页吗?您不能将其发送到运行 ASP.NET WebService 的“网站”吗?

See here& hereon how to write a webservice, and then read here& hereon how to call the WebService via VBA.

请参阅此处此处了解如何编写 Web 服务,然后阅读此处此处了解如何通过 VBA 调用 WebService。

回答by Marius Kjeldahl

Although I can't give you an exact example in Excel, I'm pretty certain Excel has a function or two to lookup values from any website using a GET request. The difference between a GET and POST request is that in the former all data passed is part of the URL string. Assuming the HTTP support in Excel is basic, I suspect you will have better luck using a GET request, carefully building your URL with the data with simple string manipulation. Although I do not know the exact syntax, imagine a formula in a cell like =lookuphtml("http://some.url.com/send?var1="&B2&"&var2="&B3), where cells B2 and B3 would contain your variable values (the values you want to pass to the script). This assumes the receiving end is capable of receiving data through GET requests (not only POST requests). Most decent server-side libraries allow data passed through both GET and POST request, although YMMV.

虽然我不能在 Excel 中给你一个确切的例子,但我很确定 Excel 有一两个函数可以使用 GET 请求从任何网站查找值。GET 和 POST 请求之间的区别在于,前者传递的所有数据都是 URL 字符串的一部分。假设 Excel 中的 HTTP 支持是基本的,我怀疑您使用 GET 请求会有更好的运气,使用简单的字符串操作小心地使用数据构建您的 URL。虽然我不知道确切的语法,但可以想象一个单元格中的公式,例如 =lookuphtml(" http://some.url.com/send?var1="&B2&"&var2="&B3),其中单元格 B2 和 B3 将包含您的变量值(您要传递给脚本的值)。这假设接收端能够通过 GET 请求(不仅是 POST 请求)接收数据. 大多数体面的服务器端库允许通过 GET 和 POST 请求传递数据,尽管 YMMV。

回答by barrowc

I'm not a .NET guy but it looks very much like URI.Querywould only show the GET data because POST data isn't part of the URI.

我不是 .NET 人,但它看起来很像URI.Query只会显示 GET 数据,因为 POST 数据不是 URI 的一部分。

You might want to look at using the standard form syntax of name1=value1&name2=value2&...in your sDatastring and then on the server use Request.Formas per the examples here

您可能希望name1=value1&name2=value2&...sData字符串中使用标准形式的语法,然后在服务器上Request.Form按照此处的示例使用