2009-10-26 103 views
0

我想從讀取網站中的文本文件(如www.site.com/example.txt)中讀取,並相應地更新Excel表格中的選項卡。是否有可能使用Excel VBA?你能提供一些例子嗎?EXCEL VBA - 從網站打開文本文件

你能否建議我需要通讀以便我能達到目標。是否有一些onlien教程或材料可用。請幫助

下面的網站有一個如何在JAVA中的例子。有沒有人有VBA的例子?

http://www.daniweb.com/forums/thread143010.html#

回答

0

是的,你可以使用e.g HTTP客戶端庫來獲取從網頁。

也有工具可用或此類任務 http://www.iopus.com/iMacros/excel.htm?ref=rg8excel1HB&gclid=CLWbrNef2p0CFUtp4wodBnx8sQ

如果你想自己做,你必須參考 「Microsoft Internet Transfer」Active X控件。

例如,你可能會 BUF = HTTPControl.OpenURL(page_you_like_to_fetch,icByteArray)抓取網頁 你再可以只用通常的「嫌疑人」打開它就像打開 或者可以註冊文件作爲ODBC數據源,並獲取它與 SQL類似的語句。

當然你也可以手工做。打開文件,在一個文件之後訪問另一個文件並將其放入適當的Execl單元中。

0

我喜歡弗里德里希的建議,但是如果你想做到這一點你可以使用shell命令的另一種方式從內部VBA來啓動這個進程:

Windows Implementation of the Linux WGet command

這是Linux的一個端口命令,該命令從命令行獲取網站(URL)的內容到文件,然後可以打開已在本地創建的VBA代碼中的文件並相應地處理它。

(還可有此命令的其它實現,我只是鏈接到的第一個,我在谷歌找到)

5

看看在Workbooks.OpenText method。 filename參數將接受一個http URL。該方法允許您在一個步驟中打開並解析它(假設它是一個分隔文件)。

Application.Workbooks.OpenText("http://somewebsite.com/test.txt", _ 
    StartRow:=3, _ 
    DataType:=Excel.XlTextParsingType.xlDelimited, _ 
    TextQualifier:=Excel.XlTextQualifier.xlTextQualifierNone, _ 
    Comma:=True) 
7

下面是如何下載文本文件的示例。

要求微軟WinHTTPServices參考

Sub Test_DownloadTextFile() 
    Dim text As String 
    text = DownloadTextFile("http://stackoverflow.com/") 

    'At this point you can process you file how you wish. 
    Debug.Print text 
End Sub 

'Tool.References... Add a reference to Microsoft WinHTTPServices 
Public Function DownloadTextFile(url As String) As String 
    Dim oHTTP As WinHttp.WinHttpRequest 
    Set oHTTP = New WinHttp.WinHttpRequest 
    oHTTP.Open Method:="GET", url:=url, async:=False 
    oHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" 
    oHTTP.setRequestHeader "Content-Type", "multipart/form-data; " 
    oHTTP.Option(WinHttpRequestOption_EnableRedirects) = True 
    oHTTP.send 

    Dim success As Boolean 
    success = oHTTP.waitForResponse() 
    If Not success Then 
     Debug.Print "DOWNLOAD FAILED!" 
     Exit Function 
    End If 

    Dim responseText As String 
    responseText = oHTTP.responseText 

    Set oHTTP = Nothing 

    DownloadTextFile = responseText 
End Function