vba - 使用 VBA 将 Excel xlsm 文件上传到 php 脚本

标签 vba excel winhttp winhttprequest

我想从 VBA 将 Excel xlsm 文件上传到 php 脚本。我找到了以下代码:

Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

Dim strURL As String
Dim StrFileName As String
Dim FormFields As String
Dim path As String
Dim name As String

   StrFileName = "c:\temp\ctc1output.xls"
   strURL = "http://www.tri-simulation.com/P3/"


   WinHttpReq.Open "POST", strURL, False


   ' Set the header
  WinHttpReq.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"


   FormFields = """fileulp=" & StrFileName & """"
   FormFields = FormFields + "&"
   FormFields = FormFields + """sfpath=P3"""

   WinHttpReq.Send FormFields


   ' Display the status code and response headers.
   MsgBox WinHttpReq.GetAllResponseHeaders
   MsgBox WinHttpReq.ResponseText
  • 我应该将文件作为二进制文件还是其他类型的文件来处理?
  • 我可以在文件仍然打开时上传文件吗(我想上传运行 VBA 的文件)?

  • 我不确定我是否走在正确的轨道上。
    我也不确定标题和表单字段应该是什么。

    感谢您的帮助。

    最佳答案

    您不需要对任何内容进行 base64 编码。关注 sample code you have found但在准备文件之前(在'---准备正文评论之前)只需像这样添加其他文本(表单条目)

    sPostData = sPostData & "--" & STR_BOUNDARY & vbCrLf & _
         "Content-Disposition: form-data; name=""" & Name & """"
    sPostData = sPostData & vbCrLf & vbCrLf & _
         pvToUtf8(Value) & vbCrLf
    

    ...在哪里 NameValue是您想要包含在服务请求中的设计名称和实际文本。对于函数pvToUtf8实现看看this Google Cloud Print service connector .上面的片段取自 pvRestAddParam同一个连接器的功能。

    关于vba - 使用 VBA 将 Excel xlsm 文件上传到 php 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15404460/

    相关文章:

    c# - .NET - Excel ListObject 在数据绑定(bind)时自动调整大小

    ssl - vb6 winhhtp:安全通道支持中发生错误

    database - 用于登录用户名和密码的 DLookup 代码

    vba - Excel VBA - 为什么包含数字的分割字符串的算术比较有效?

    c# - 使用 C# 去除宏的 Excel 文件

    ssl - 经典 asp 中的 WinHttp TLS 连接

    vba - WinHTTP VBA 后续请求不能使用之前的登录凭据?

    excel - 如何循环遍历范围并将值分配给参数

    excel - 使用对象调用方法时未定义 VBA 变量

    sql - 需要 vba-excel 和 Sql 查询帮助