简体   繁体   English

如何使用 SQL 与查询合并多个 excel 文件而不使用 SSIS?

[英]How to merge multiple excel files using SQL with a query and not using SSIS?

I have 10 excel files with same data structure.我有 10 个具有相同数据结构的 excel 文件。 Each file has first name and last name in Sheet1.每个文件在 Sheet1 中都有名字和姓氏。 But the file names are different and has no pattern in it.但是文件名不同,并且没有模式。 All the 10 files are present in a same folder.所有 10 个文件都存在于同一个文件夹中。

I want to use SQL to merge all 10 files and get it into one SQL table.我想使用 SQL 合并所有 10 个文件并将其放入一个 SQL 表中。 Is there a way to do that.有没有办法做到这一点。

Files path: C:\User\ferguson\excelfiles

Excel file 1 : name.xlsx
Excel file 2: names.xlsx
Excel file 3: details.xlsx
.
.
.
.
Excel file 10: info.xlsx

I want one single SQL table with all the data in all files using a SQL QUERY.我想要一个 SQL 表,其中包含使用 SQL 查询的所有文件中的所有数据。 Can it be done using BULK Insert or something?可以使用 BULK Insert 或其他东西来完成吗?

Any help appreciated任何帮助表示赞赏

Consider OPENROWSET or OPENDATASOURCE using a compliant ODBC driver or OLEDB provider that runs a UNION query across all workbooks.考虑使用兼容的 ODBC 驱动程序或 OLEDB 提供程序的OPENROWSETOPENDATASOURCE ,它在所有工作簿上运行UNION查询。 Below assumes each workbook has named columns named, FirstName and LastName starting in A1 in worksheet named, Sheet1 .下面假设每个工作簿在名为Sheet1的工作表中从A1开始命名为FirstNameLastName列。

SELECT * FROM
OPENROWSET('MSDASQL','DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};
           DBQ=C:\Path\To\File1.xlsx', 
           'SELECT [FirstName], [LastName] FROM [Sheet1$]')

UNION ALL

SELECT * FROM
OPENROWSET('MSDASQL','DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};
           DBQ=C:\Path\To\File2.xlsx', 
           'SELECT [FirstName], [LastName] FROM [Sheet1$]')
...

UNION ALL

SELECT * FROM
OPENROWSET('MSDASQL','DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};
           DBQ=C:\Path\To\File10.xlsx', 
           'SELECT [FirstName], [LastName] FROM [Sheet1$]')

Alternatively, you can use a provider version:或者,您可以使用提供程序版本:

SELECT * FROM
OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0',
               'Data Source=C:\Path\To\File1.xlsx;Extended Properties=Excel 12.0')...Sheet1$ 
...

Possibly you can even combine in one call, connecting to first workbook:您甚至可以在一次通话中组合,连接到第一个工作簿:

SELECT * FROM
OPENROWSET('MSDASQL','DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};
           DBQ=C:\Path\To\File10.xlsx', 
           'SELECT [FirstName], [LastName] 
            FROM [Sheet1$]

            UNION ALL
            SELECT [FirstName], [LastName]
            FROM [Excel 12.0 Xml;HDR=Yes;Database=C:\Path\To\File2.xlsx].[Sheet1$]

            ...

            UNION ALL
            SELECT [FirstName], [LastName]
            FROM [Excel 12.0 Xml;HDR=Yes;Database=C:\Path\To\File10.xlsx].[Sheet1$]
          ')

You can do what Parfait suggested, or use VBA in Excel to get the job done.您可以按照 Parfait 的建议进行操作,或者使用 Excel 中的 VBA 来完成工作。

Sub Basic_Example_1()
    Dim MyPath As String, FilesInPath As String
    Dim MyFiles() As String
    Dim SourceRcount As Long, Fnum As Long
    Dim mybook As Workbook, BaseWks As Worksheet
    Dim sourceRange As Range, destrange As Range
    Dim rnum As Long, CalcMode As Long

    'Fill in the path\folder where the files are
    MyPath = "C:\your_path_here"

    'Add a slash at the end if the user forget it
    If Right(MyPath, 1) <> "\" Then
        MyPath = MyPath & "\"
    End If

    'If there are no Excel files in the folder exit the sub
    FilesInPath = Dir(MyPath & "*.xl*")
    If FilesInPath = "" Then
        MsgBox "No files found"
        Exit Sub
    End If

    'Fill the array(myFiles)with the list of Excel files in the folder
    Fnum = 0
    Do While FilesInPath <> ""
        Fnum = Fnum + 1
        ReDim Preserve MyFiles(1 To Fnum)
        MyFiles(Fnum) = FilesInPath
        FilesInPath = Dir()
    Loop

    'Change ScreenUpdating, Calculation and EnableEvents
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'Add a new workbook with one sheet
    Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
    rnum = 1

    'Loop through all files in the array(myFiles)
    If Fnum > 0 Then
        For Fnum = LBound(MyFiles) To UBound(MyFiles)
            Set mybook = Nothing
            On Error Resume Next
            Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
            On Error GoTo 0

            If Not mybook Is Nothing Then

                On Error Resume Next

                With mybook.Worksheets(1)
                    Set sourceRange = .Range("A1:C1")
                End With

                If Err.Number > 0 Then
                    Err.Clear
                    Set sourceRange = Nothing
                Else
                    'if SourceRange use all columns then skip this file
                    If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
                        Set sourceRange = Nothing
                    End If
                End If
                On Error GoTo 0

                If Not sourceRange Is Nothing Then

                    SourceRcount = sourceRange.Rows.Count

                    If rnum + SourceRcount >= BaseWks.Rows.Count Then
                        MsgBox "Sorry there are not enough rows in the sheet"
                        BaseWks.Columns.AutoFit
                        mybook.Close savechanges:=False
                        GoTo ExitTheSub
                    Else

                        'Copy the file name in column A
                        With sourceRange
                            BaseWks.cells(rnum, "A"). _
                                    Resize(.Rows.Count).Value = MyFiles(Fnum)
                        End With

                        'Set the destrange
                        Set destrange = BaseWks.Range("B" & rnum)

                        'we copy the values from the sourceRange to the destrange
                        With sourceRange
                            Set destrange = destrange. _
                                            Resize(.Rows.Count, .Columns.Count)
                        End With
                        destrange.Value = sourceRange.Value

                        rnum = rnum + SourceRcount
                    End If
                End If
                mybook.Close savechanges:=False
            End If

        Next Fnum
        BaseWks.Columns.AutoFit
    End If

ExitTheSub:
    'Restore ScreenUpdating, Calculation and EnableEvents
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With
End Sub

Also, Python would do it, and R would concatenate all the files into one single file.此外,Python 会这样做,而 R 会将所有文件连接到一个文件中。 You could use C#.NET or VB.NET to do this kind of thing.你可以使用 C#.NET 或 VB.NET 来做这种事情。 If you had a bunch of CSV files, or TXT files, you could loop through those, and do a BULK INSERT, but you can't do that with Excel files.如果您有一堆 CSV 文件或 TXT 文件,您可以遍历这些文件并执行 BULK INSERT,但您不能使用 Excel 文件来执行此操作。

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

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