从通过网络下载的ZIP文件中提取CSV,然后格式化并导入该CSV以访问

时间:2017-07-20 19:10:55

标签: excel excel-vba ms-access web-scraping access-vba vba

http://portal.flmmis.com/FLPublic/Provider_ManagedCare/Provider_ManagedCare_Registration/tabId/77/Default.aspx?linkid=pml处有一个ZIP数据文件,其中包含我需要在Access数据库中使用的CSV文件。

此文件每天更新​​,这就是流程需要自动化的原因。将ZIP文件中的CSV解压缩为Excel文件后,我需要用空格替换文件中的所有逗号。

之后,我需要将该文件导入标题为Network_DB的数据库,以便我可以根据该数据集和其他数据集生成报告。它以逗号分隔。

我不知道从哪里开始。我已经看到了对Python,PowerShell,Excel宏的建议......整整九码。我会说我在工作时无法访问Python,而且我对PowerShell不熟悉。

2 个答案:

答案 0 :(得分:0)

如果您使用的是MS Access,则VBA是解决此问题的最简单方法。

要从网上下载文件,您可以使用以下代码段:

Sub downloadUrl(url As String, destinationFile As String)
    Dim htp As New MSXML2.XMLHTTP60
    Dim stream As New ADODB.Stream

    'Request file
    With htp
        .Open "GET", url, false
        .send
    End With

    'Save to file
    With stream
        .Open
        .Type = adTypeBinary
        .write htp.responseBody
        .SaveToFile destinationFile
        .Close
    End With

End Sub

然后像这样调用这个函数

downloadUrl "http://portal.flmmis.com/FLPublic/Provider_ManagedCare/Provider_ManagedCare_Registration/tabId/77/Default.aspx?linkid=pml", "C:\Users\Public\test.csv"

要将CSV导入MS Access,您可以使用其中一个导入向导来帮助您。

Ps。:我试图访问提供的链接,但它似乎对我来说是离线的

答案 1 :(得分:0)

您可以在此处使用 DownLoadFile 功能:

Show pictures directly from URLs in Access forms and reports

下载文件。然后使用如下函数解压缩它:

Public Function UnzipFile( _
    ByVal ZipFile As String, _
    Optional ByRef DestinationFolder As String) _
    As Boolean

    Const LocalAppData  As String = "LOCALAPPDATA"
    Const OverWriteAll  As Long = &H10&

    Dim ShellApp        As Object
    Dim FileName        As String
    Dim Result          As Boolean

    ' Any error will cause a return value of False.
    On Error Resume Next

    FileName = Dir(ZipFile, vbNormal)
    If InStr(StrReverse(ZipFile), StrReverse(FileName)) = 1 Then
        ' ZipFile exists.
        If DestinationFolder = "" Then
            ' Find and use user's LocalAppData folder, and return constructed folder name.
            DestinationFolder = Environ(LocalAppData) & "\" & Left(FileName, InStr(FileName, ".")) & "Files"
        End If
        If Dir(DestinationFolder, vbDirectory) = "" Then
            ' Create new destination folder.
            MkDir DestinationFolder
        End If
        If InStr(StrReverse(DestinationFolder), StrReverse(Dir(DestinationFolder, vbDirectory))) = 1 Then
            ' Destination folder existed or has been created successfully.
            Set ShellApp = CreateObject("Shell.Application")
            ' Unzip files to destination folder.
            ShellApp.Namespace(CVar(DestinationFolder)).CopyHere ShellApp.Namespace(CVar(ZipFile)).Items, OverWriteAll
            If Err.Number = 0 Then
                Result = True
            End If
        End If
    End If

    UnzipFile = Result

    Set ShellApp = Nothing

End Function