【问题标题】:Extract data from a specific cell with SSIS使用 SSIS 从特定单元格中提取数据
【发布时间】:2015-09-17 07:40:04
【问题描述】:

假设我有一个 Excel 表格,第三行带有标题。

|Table title
|Entry Date: September 15, 2015
|    ColumnA    |    ColumnB    |    (etc.)    |

使用 SSIS,我想导入标题下方的所有内容并将其附加到现有的带有列的 SQL 表中

EntryDate    ColumnA     ColumnB    (...)

步骤是

  1. 导入标题下方的数据
  2. 选择包含日期的单元格
  3. 从字符串中提取日期
  4. 填写 SQL 表中的 EntryDate 列

我发现a method 定义了要从中导入的行,但 SSIS 不接受格式并恢复为表名。我还没有找到导入日期的解决方案。

【问题讨论】:

    标签: sql sql-server ssis


    【解决方案1】:

    我不知道如何使 OPENROWSET 工作。我会自己用 VB 在脚本任务中完成。

    对于你的约会,我会使用

    DateValue = Replace(Workbook.Sheets("Data").Cells(Row, 1), "Entry Date: ", "")
    

    我将从第 3 行开始并获取标题(如果需要),然后逐行导入数据。

    我没有你需要的一切,但这里有一些东西可以帮助你入门。您确实需要一些 VB 知识才能将所有部分组合在一起。

        'Declare variables
        Dim objConn As ADODB.Connection
        Dim objCmd As ADODB.Command
        Dim objRS As ADODB.Recordset
    
        Dim Excel1 As New Excel.Application
        Dim Workbook As Excel.Workbook
        Dim Worksheet As Excel.Worksheet = Nothing
        Dim Query As String
    
        Dim CellValue As String
        Dim Row As Integer
    
        Public Sub Main()
    
            objConn = New ADODB.Connection
            objCmd = New ADODB.Command
            objRS = New ADODB.Recordset
    
            'Open Connection
            objConn.ConnectionString = "Provider=SQLNCLI11.1; Data Source=KPRD; Initial Catalog = REPORTING; User ID=user; PWD=Password1; Persist Security Info=True; Auto Translate=False; Connection Timeout=300;"
            objConn.Open()
    
            'Set and Excecute SQL Command
            Query = Dts.Variables("SQLData").Value.ToString
    
            objCmd.ActiveConnection = objConn
            objCmd.CommandText = Query
            objCmd.CommandTimeout = 120
    
            'Open Recordset
            objRS.Source = objCmd
            objRS.Open() 
    
            Excel1.Visible = False
            Excel1.DisplayAlerts = False
    
            ReportName = "\\ServerName\folder\Report.xlxs"      
    
            Row = 2
    
            DateValue = Replace(Workbook.Sheets("Data").Cells(Row, 1), "Entry Date: ", "")
    
            Workbook = Excel1.Workbooks.Open(ReportName, , False) 'False = read-write.
    
            Do Until Workbook.Sheets("Data").Cells(Row, 1) = ""
    
            'Get data from a row
    
            'Write SQL to insert data into your db
    
    GetNextRow:
            Row = Row + 1
            Loop
    
    Done:
            Excel1.Quit()
            Excel1 = Nothing
            ReportName = Nothing
            Dts.TaskResult = ScriptResults.Success
    
    ExitSub:
            Exit Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 2016-03-04
      相关资源
      最近更新 更多