使用 SQL 将 150 个文本文件导入 Access as Tables

Import 150 text files to Access as Tables using SQL

我有 150 个 csv 格式的文本文件使用管道 |作为分隔符。这些文件位于网络上的不同文件夹中。文件名是特定于日期的,并有一个包含日期的后缀。我创建了一个包含 3 个字段的 table:文件位置;不包括后缀的文件名;后缀(格式为 yymmdd)。

我想创建一个 SQL 脚本,它将我创建的 table 中命名的 150 个文件中的每一个导入到 150 个单独的 Access 2007 table 中,命名为不包括后缀的文件名。我尝试使用 VBA 来执行此操作,但文件名包含句点并且 VBA 不喜欢这样。

每个文件都有不同的列结构,但第一行始终是 headers。我只能使用 Access 2007 的原生功能,因为我工作的组织不允许第三方插件或应用程序。我没有可用的 SQL 服务器。

就 Access 而言,我是一个完全的新手,并且正在努力实现接近此的任何目标。你能帮忙吗?

我努力在 Access 中实现我想要的,所以转向 Excel。下面的代码将每个文本文件创建为单个 Excel 工作簿中的 sheet,主 table 将文件名/路径等保存为 "Master" Sheet 在工作簿中。它会在重新创建 "Master" 之前删除除 sheet 之外的所有 sheet,如果找不到文件,它将留下空白 sheet.

Sub ImportFiles()

'This script looks at a Master list of files for import and imports each to their own tab
'The files are pipe (|) delimited and can be in any addressable location
'If any files are not found, the import of that file is skipped, leaving a blank worksheet

'Close the Report File before starting the import
    Application.DisplayAlerts = False
    On Error Resume Next
    Windows("Report.xlsx").Activate
    Windows("Report.xlsx").Close SaveChanges:=True
    On Error GoTo 0
    Application.DisplayAlerts = True

'Start by looking at the sheet which contains the Master list of files
    Sheets("Master").Activate

'declare three arrays of unknown length
    Dim FileName() As String
    Dim FilePath() As String
    Dim FullName() As String

'initially there are no files
    Dim NumberFiles As Integer
    NumberFiles = 0

'loop over all of the file cells
'The master file needs to be structured FileName, FilePath and FullName in that order
'Change C2 to the cell containing the first FileName in the Master List
    Dim FileCell As Range
    Dim TopCell As Range
    Dim FileRange As Range

    Set TopCell = Range("C2")
    Set FileRange = Range(TopCell, TopCell.End(xlDown))
    For Each FileCell In FileRange

'we've found another file!
    NumberFiles = NumberFiles + 1

'for each file found, extend all arrays
    ReDim Preserve FileName(NumberFiles - 1)
    ReDim Preserve FilePath(NumberFiles - 1)
    ReDim Preserve FullName(NumberFiles - 1)

'now store the filename, path and fullname of the new file
    FileName(NumberFiles - 1) = FileCell.Value
    FilePath(NumberFiles - 1) = FileCell.Offset(0, 1).Value
    FullName(NumberFiles - 1) = FileCell.Offset(0, 2).Value

Next FileCell

'delete any existing sheets except Master and create new blank sheets
For Each Sheet In Application.Worksheets
    Application.DisplayAlerts = False
    If Sheet.Name <> "Master" Then
    Sheet.Delete
    End If
    Next Sheet
    Sheets("Master").Activate
    For i = 1 To (NumberFiles)
    Worksheets.Add(After:=Sheets(Sheets.Count)).Name = "Sheet" & i
    Next i
    ThisWorkbook.Sheets("Sheet1").Select

'Start the process of import

'create a workbook object for the workbook where the current macro is running
    Dim ws As Worksheet

'import each file into its own sheet
    For i = 0 To (NumberFiles - 1)

    Set ws = ThisWorkbook.Sheets("Sheet" & i + 1) 'the current sheet

'Ignore any missing files and carry on processing
    Application.DisplayAlerts = False
    On Error Resume Next

'This imports the delimited files
    With ws.QueryTables.Add(Connection:="TEXT;" & FullName(i),     Destination:=ws.Range("$A"))
        .Name = "a" & i
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileOtherDelimiter = "|"
        .TextFileColumnDataTypes = Array(1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False

    End With

'Rename tabs to name on master list
    ws.Name = "" & FileName(i)

    Next i

'Reopen the Report File
    Workbooks.Open FileName:=ThisWorkbook.Path & "\Report.xlsx"

End Sub