【问题标题】:Migrating Several Excel Files to SQL Server Using FME or Something Else使用 FME 或其他方法将多个 Excel 文件迁移到 SQL Server
【发布时间】:2019-03-26 18:18:43
【问题描述】:

我有几个要迁移到 SQL Server 2017 的 Excel 文件。我尝试使用 SSIS 2017 (ForEachLoop),但我无法让它工作(我看到之前的答案将我推荐给旧版本的 SSIS)。对我来说,一个解决方法是将 excel 文件转换为 csv 并直接将它们一个接一个地导入 SSMS。我还可以使用 FME 将“每个”excel 文件作为表格发送到 SQL Server。

如果我能以某种方式遍历包含所有 excel 文件(不同版本,97-2003、2013 等)的文件夹并将它们逐个读取到 SQL Sever 2017 中,那就太好了,每个作为具有自己名称的单个表 - 就像我一个一个地导入它们一样。

例如,abc.xlsx 变成 dbo.abc,def.xlsx 变成 dbo.def,ghi.xls 变成 dbo.ghi,jkl.xls 变成 dbo.jkl

【问题讨论】:

  • 你熟悉c#吗?
  • 我不精通该语言以生成一个工作程序。但我可以阅读代码并理解它的作用。
  • 获取帮助。这远远超出了你的能力,甚至会挑战那些有经验的人。问题是每个文件都包含一组(可能)唯一的数据。因此逻辑必须“查看”每个文件以确定表的名称、包含的列、列的数据类型、行的数量和位置等。一个人(你)可以使用向导来执行此操作,因为这个人在视觉上做了所有这些。这不是一个微不足道的目标。您是否有理由不能使用 FME(您似乎有经验)?
  • 我是 FME 的新手,我没有很多经验。好吧,将一个 excel 文件迁移到 SQL Server 是 FME 中的一个简单工作流程。我有几张 excel 表,想遍历文件夹的内容。所有项目都是 excel 文件,它们都具有相同/相似的结构,尽管有些项目缺少标题名称。
  • 我认为除了编写一个脚本任务来完成所有工作之外,没有使用SSIS的解决方案(那么就不需要SSIS了)

标签: sql-server excel ssis etl fme


【解决方案1】:

对不起。这晚了8个月。

这不是上述问题的解决方案,但我想我分享一下我是如何完成这项任务的。基本上,我使用 FME 将所有 excel 文件(按其特定名称)导入 SQL Server。我没有编写任何代码或遍历目录中的文件。我通过拖动画布 (GUI) 上的所有输入来创建我的工作流,并将它们全部连接到 SQL Server 上的特定表(使用它们的特定名称)。

我知道这不是完成任务的有效方式,但它确实有效!

【讨论】:

    【解决方案2】:

    这是一个非常有趣的问题!好吧,这听起来确实像是 SSIS 的工作。我猜你试过了,但没有成功,对吧。还有其他选择!

    你可以试试这个(我没试过)。

    https://www.mstsolutions.com/technical/importing-multiple-excel-files-into-sql-database/

    现在,当然是动态地创建一堆表。

    CREATE PROCEDURE sproc_BuildTable 
        @TableName NVARCHAR(128)
       ,@Column1Name NVARCHAR(32)
       ,@Column1DataType NVARCHAR(32)
       ,@Column1Nullable NVARCHAR(32)
       AS
    
       DECLARE @SQLString NVARCHAR(MAX)
       SET @SQLString = 'CREATE TABLE '+@TableName + '( '+ @Column1Name +' '+ @Column1DataType +' '+ @Column1Nullable +') ON PRIMARY '
    
       EXEC (@SQLString)
       GO
    

    然后,您将所有这些 Excel 文件转换为 CSV 文件,您可以轻松地遍历每个文件并将所有文件批量插入到 SQL Server 中的单独表中。

    DECLARE @intFlag varchar
    SET @intFlag = 1
    WHILE (@intFlag <=5)
    BEGIN
    
    PRINT @intFlag
    
    
    declare @fullpath1 varchar(1000)
    select @fullpath1 = '''C:\your_path_here\test\sample' + @intFlag + '.csv'''
    print(@fullpath1)
    
    declare @cmd1 nvarchar(1000)
    select @cmd1 = 'bulk insert [dbo].[sample' + @intFlag + '] from' + @fullpath1 + ' with (FIELDTERMINATOR = '','', FIRSTROW = 2, ROWTERMINATOR=''\n'')'
    print(@cmd1)
    
    exec (@cmd1)
    
    
    SET @intFlag = @intFlag + 1
    
    END
    GO
    

    仅供参考,要将文件夹中的所有 Excel 文件转换为 CSV 文件,请运行以下脚本。

    Sub WorkbooksSaveAsCsvToFolder()
    'UpdatebyExtendoffice20181031
    Dim xObjWB As Workbook
    Dim xObjWS As Worksheet
    Dim xStrEFPath As String
    Dim xStrEFFile As String
    Dim xObjFD As FileDialog
    Dim xObjSFD As FileDialog
    Dim xStrSPath As String
    Dim xStrCSVFName As String
    
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.Calculation = xlCalculationManual
        On Error Resume Next
    Set xObjFD = Application.FileDialog(msoFileDialogFolderPicker)
        xObjFD.AllowMultiSelect = False
        xObjFD.Title = "Kutools for Excel - Select a folder which contains Excel files"
        If xObjFD.Show <> -1 Then Exit Sub
        xStrEFPath = xObjFD.SelectedItems(1) & "\"
    
        Set xObjSFD = Application.FileDialog(msoFileDialogFolderPicker)
    
        xObjSFD.AllowMultiSelect = False
        xObjSFD.Title = "Kutools for Excel - Select a folder to locate CSV files"
        If xObjSFD.Show <> -1 Then Exit Sub
        xStrSPath = xObjSFD.SelectedItems(1) & "\"
    
        xStrEFFile = Dir(xStrEFPath & "*.xls*")
    
        Do While xStrEFFile <> ""
            Set xObjWB = Workbooks.Open(Filename:=xStrEFPath & xStrEFFile)
            xStrCSVFName = xStrSPath & Left(xStrEFFile, InStr(1, xStrEFFile, ".") - 1) & ".csv"
            xObjWB.SaveAs Filename:=xStrCSVFName, FileFormat:=xlCSV
            xObjWB.Close savechanges:=False
            xStrEFFile = Dir
      Loop
        Application.Calculation = xlCalculationAutomatic
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    End Sub
    

    在上面的示例中,我有 5 个 CSV 文件,因此我正在循环浏览文件夹 5x 中的文件。

    您可以轻松地将所有 Excel 文件加载到 Access 中的单独表中,然后将每个 Access 表导出到 SQL Server 中的单独表中。

    Option1:
            Dim strPathFile As String, strFile As String, strPath As String
            Dim strTable As String
            Dim blnHasFieldNames As Boolean
    
            ' Change this next line to True if the first row in EXCEL worksheet
            ' has field names
            blnHasFieldNames = False
    
            ' Replace C:\Documents\ with the real path to the folder that
            ' contains the EXCEL files
            strPath = "C:\Documents\"
    
            ' Replace tablename with the real name of the table into which
            ' the data are to be imported
            strTable = "tablename"
    
            strFile = Dir(strPath & "*.xls")
            Do While Len(strFile) > 0
                  strPathFile = strPath & strFile
                  DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
                        strTable, strPathFile, blnHasFieldNames
    
            ' Uncomment out the next code step if you want to delete the
            ' EXCEL file after it's been imported
            '       Kill strPathFile
    
                  strFile = Dir()
            Loop
    
    
    Option2:
    Dim blnHasFieldNames as Boolean
    Dim strWorksheet As String, strTable As String
    Dim strPath As String, strPathFile As String
    
    ' Change this next line to True if the first row in EXCEL worksheet
    ' has field names
    blnHasFieldNames = False
    
    ' Replace C:\Documents\ with the real path to the folder that
    ' contains the EXCEL files
    strPath = "C:\Documents\"
    
    ' Replace worksheetname with the real name of the worksheet that is to be
    ' imported from each file
    strWorksheet = "worksheetname"
    
    ' Import the data from each workbook file in the folder
    strFile = Dir(strPath & "*.xls")
    Do While Len(strFile) > 0
          strPathFile = strPath & strFile
          strTable = "tbl_" & Left(strFile, InStrRev(strFile, ".xls") - 1)
    
          DoCmd.TransferSpreadsheet acImport, _
                acSpreadsheetTypeExcel9, strTable, strPathFile, _
                blnHasFieldNames, strWorksheet & "$"
    
          ' Uncomment out the next code step if you want to delete the
          ' EXCEL file after it's been imported
          ' Kill strPathFile
    
          strFile = Dir()
    Loop
    
    
    Option3:
            Dim blnHasFieldNames As Boolean, blnEXCEL As Boolean, blnReadOnly As Boolean
            Dim intWorkbookCounter As Integer
            Dim lngCount As Long
            Dim objExcel As Object, objWorkbook As Object
            Dim colWorksheets As Collection
            Dim strPath As String, strFile As String
            Dim strPassword As String
    
            ' Establish an EXCEL application object
            On Error Resume Next
            Set objExcel = GetObject(, "Excel.Application")
            If Err.Number <> 0 Then
                  Set objExcel = CreateObject("Excel.Application")
                  blnEXCEL = True
            End If
            Err.Clear
            On Error GoTo 0
    
            ' Change this next line to True if the first row in EXCEL worksheet
            ' has field names
            blnHasFieldNames = False
    
            ' Replace C:\MyFolder\ with the actual path to the folder that holds the EXCEL files
            strPath = "C:\MyFolder\"
    
            ' Replace passwordtext with the real password;
            ' if there is no password, replace it with vbNullString constant
            ' (e.g., strPassword = vbNullString)
            strPassword = "passwordtext"
    
            blnReadOnly = True ' open EXCEL file in read-only mode
    
            strFile = Dir(strPath & "*.xls")
    
            intWorkbookCounter = 0
    
            Do While strFile <> ""
    
                  intWorkbookCounter = intWorkbookCounter + 1
    
                  Set colWorksheets = New Collection
    
                  Set objWorkbook = objExcel.Workbooks.Open(strPath & strFile, , _
                        blnReadOnly, , strPassword)
    
                  For lngCount = 1 To objWorkbook.Worksheets.Count
                        colWorksheets.Add objWorkbook.Worksheets(lngCount).Name
                  Next lngCount
    
                  ' Close the EXCEL file without saving the file, and clean up the EXCEL objects
                  objWorkbook.Close False
                  Set objWorkbook = Nothing
    
                  ' Import the data from each worksheet into a separate table
                  For lngCount = colWorksheets.Count To 1 Step -1
                        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
                              "tbl" & colWorksheets(lngCount) & intWorkbookCounter, _
                              strPath & strFile, blnHasFieldNames, _
                              colWorksheets(lngCount) & "$"
                  Next lngCount
    
                  ' Delete the collection
                  Set colWorksheets = Nothing
    
                  ' Uncomment out the next code step if you want to delete the
                  ' EXCEL file after it's been imported
                  ' Kill strPath & strFile
    
                  strFile = Dir()
    
            Loop
    
            If blnEXCEL = True Then objExcel.Quit
            Set objExcel = Nothing
    

    只是为了结束事情,请参阅下面的链接了解更多信息。

    https://www.red-gate.com/simple-talk/sql/t-sql-programming/questions-about-using-tsql-to-import-excel-data-you-were-too-shy-to-ask/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 2017-09-07
      • 2018-11-26
      相关资源
      最近更新 更多