读取远程服务器中托管的Excel文件

时间:2019-09-16 07:53:43

标签: excel vba server

我将一个Excel文件上传到服务器,该文件是一个小数据库。 我想使用vba通过另一个excele文件读取和写入文件。 有可能吗?

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码打开并关闭所需的工作簿。只要它处于打开状态,您就可以定期使用它(例如复制和粘贴数据)。

仅使用例如:

Workbooks("New Data.xlsx").Worksheets("Sheet").Range("A1:A2").Copy

主要代码:

Sub OpenWorkbook()
'Open a workbook

  'Open method requires full file path to be referenced.
  Workbooks.Open "C:\Users\username\Documents\New Data.xlsx"

End Sub


Sub CloseWorkbook()
'Close a workbook

  Workbooks("New Data.xlsx").Close SaveChanges:=True

End Sub

对于与FTP服务器的连接,此代码应至少可以帮助您或使您有所了解。再一次可能需要修补。

Function FTP_Upload_File(QuellDatei As String, ZielDatei As String, FTPServer As String, FTPBenutzer As String, FTPKennwort As String) As Long
    Dim FTP             As Inet
    Dim ExecString      As String

    On Local Error GoTo FuncExit

    Set FTP = New Inet
    With FTP
        .Protocol = icFTP
        .RemoteHost = FTPServer
        .Username = FTPBenutzer
        .Password = FTPKennwort

        ExecString = "put " & Chr(34) & QuellDatei & Chr(34) & " " & Chr(34) & ZielDatei & Chr(34)
        .Execute .URL, ExecString
        Do While .StillExecuting
            DoEvents
        Loop
    End With

FuncExit:
    FTP_Upload_File = FTP.ResponseCode
    Set FTP = Nothing
End Function