繁体   English   中英

将 CSV 文件从 SharePoint Online 文档库导入 Access 数据库

[英]Import a CSV file into Access database from SharePoint Online Document Library

我在 SharePoint Online 文档库中上传了一个 CSV 文件,该文件每天更新​​。 我使用这个 CSV 文件来创建一些带有 Access 数据库的报告。 我想要实现的是将 CSV 文件自动导入 Access,而无需在本地下载和保存 CSV 文件。 我使用的代码如下:

Sub ImportCSV()

   DoCmd.TransferText TransferType:=acLinkDelim, TableName:="Daily_Report_Table", FileName:="https://mycompany.sharepoint.com/teams/Daily%20Reporting.csv", HasFieldNames:=True

End Sub

但是,它要么要求我登录,然后说我无权访问该文件,要么不执行说它无法加载 HTML 页面...

不要被链接的标题所迷惑。

您不能直接通过 HTTP 导入/链接文件。 代码为您下载文件,准备链接:

Sub ImportCSV()

    Dim Result As Long
    Dim Url As String
    Dim Filename As String

    Url = "https://mycompany.sharepoint.com/teams/Daily%20Reporting.csv"
    Filename = "C:\Imports\DailyReporting.csv"

    Result = DownloadFile(Url, Filename)
    If Result = 0 Then
        DoCmd.TransferText TransferType:=acLinkDelim, TableName:="Daily_Report_Table", FileName:=Filename, HasFieldNames:=True 
    End If

End Sub

配套代码模块:

Option Compare Database
Option Explicit

Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
    ByVal pCaller As Long, _
    ByVal szURL As String, _
    ByVal szFileName As String, _
    ByVal dwReserved As Long, _
    ByVal lpfnCB As Long) _
    As Long

Public Function DownloadFile( _
    ByVal strURL As String, _
    ByVal strLocalFilename As String) _
    As Long

' Download file or page with public access from the web.
' 2004-12-17. Cactus Data ApS, CPH.

' Usage, download a file:
' lngRet = DownloadFile("http://www.databaseadvisors.com/Graphics/conf2002/2002ConferencePicsbySmolin/images/dba02smolin27.jpg", "c:\happybassett.jpg")
'
' Usage, download a page:
' lngRet = DownloadFile("http://www.databaseadvisors.com/conf2002/conf200202.asp", "c:\dbaconference.htm")

' Returns 0 if success, error code if not.
' Error codes:
' -2146697210 "file not found".
' -2146697211 "domain not found".

' Limitation.
' Does not check if local file was created successfully.

    Dim lngRetVal As Long

    lngRetVal = URLDownloadToFile(0, strURL & vbNullChar, strLocalFilename & vbNullChar, 0, 0)

    DownloadFile = lngRetVal

End Function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM