2013-03-14 79 views
2

我想從VBA上載Excel xlsm文件到php腳本。我發現下面的代碼:使用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 
  1. 我應該處理的文件作爲二進制文件或其他類型的文件?
  2. 我可以在文件仍然打開時上傳文件嗎(我想上傳VBA運行的文件)?

我不知道我是否在正確的軌道上。 我也不確定標題和表單字段應該是什麼。

Thx尋求幫助。

+1

嗨,從您的代碼中,您只是將文件名發送到服務器。你需要以某種方式將文件轉換爲二進制文件並將它們包含在POST消息中。 – Larry 2013-03-14 09:33:18

+0

嗨,我可以看到,該文件必須作爲二進制文件上傳。我使用[這裏]的代碼(http://wqweto.wordpress.com/2011/07/12/vb6-using-wininet-to-post-binary-file/)。它也似乎我不需要複製。我現在的問題是,我需要發送一些文本的帖子。這是一個單獨的請求,或者我可以用第一個包含它嗎?對不起,如果我看起來多餘,但這是我第一次與這個話題。 – Albertus 2013-03-14 10:53:39

+2

你必須模擬一個瀏覽器組裝一個POST請求和base64編碼你的文件,這可以完成,但有點乏味。另一種選擇是運行[curl](http://curl.haxx.se/),它只需要文件名和上傳表單參數(curl可執行文件可以從vba運行)。 – collapsar 2013-03-15 19:37:05

回答

0

你不需要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功能。

相關問題