0

将 500 个具有相同结构但每个都有自己唯一的工作表名称的 XLS 文件导入 SQL Server 的最简单方法是什么?

或者,我已将所有 XLS 文件合并到一个 XLS 文件中,但现在每个工作表名称在 XLS 文件中都是唯一的。

4

1 回答 1

0

I found these 2 VBA that helped me complete the job.

The 1st one combines all XLS in one directory into a single XLS and creates a unique Worksheet for each XLS file.

Sub Merge_Multiple_XLS_Into_WorkSheets()
    Path = "C:\Folder\"
    Filename = Dir(Path & "*.xls")
    Do While Filename <> ""
        Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
        For Each Sheet In ActiveWorkbook.Sheets
        Sheet.Copy After:=ThisWorkbook.Sheets(1)
        Next Sheet
        Workbooks(Filename).Close
        Filename = Dir()
    Loop
End Sub

The 2nd one combining all Sheets into a single one

Sub Merge_Sheets_Into_One()
    Const sRANGE = "A2:Z100"
    Dim iSheet, iTargetRow As Long, oCell As Object, bRowWasNotBlank As Boolean
    Dim iTop, iLeft, iBottom, iRight As Long
    Sheets(1).Select: Sheets.Add
    Sheets(1).Select
    Cells.Select
    Selection.Clear
    bRowWasNotBlank = True
    For iSheet = 2 To ThisWorkbook.Sheets.Count: DoEvents
    For Each oCell In Sheets(iSheet).Range(sRANGE).Cells: DoEvents
    If oCell.Column = 1 Then
    If bRowWasNotBlank Then iTargetRow = iTargetRow + 1
    bRowWasNotBlank = False
    End If
    If oCell.MergeCells Then
    bRowWasNotBlank = True
    If oCell.MergeArea.Cells(1).Row = oCell.Row Then
    If oCell.MergeArea.Cells(1).Column = oCell.Column Then
    Sheets(1).Cells(iTargetRow, oCell.Column) = oCell
    iTop = iTargetRow
    iLeft = oCell.Column
    iBottom = iTop + oCell.MergeArea.Rows.Count - 1
    iRight = iLeft + oCell.MergeArea.Columns.Count - 1
    Sheets(1).Range(Cells(iTop, iLeft), Cells(iBottom, iRight)).MergeCells = True
    End If
    End If
    End If
    If Len(oCell) Then bRowWasNotBlank = True
    Sheets(1).Cells(iTargetRow, oCell.Column) = oCell
    Next oCell
    Next
    Sheets(1).Activate
End Sub

...and after running these 2 VBS scripts, it's just a matter of running Import Data wizard inside from the MS-SQL Management Studio.

Hope this helps...

于 2013-05-17T05:24:40.320 回答