使用SSIS

时间:2015-09-15 19:19:27

标签: sql sql-server ssis

假设我在第三行有一个包含标题的Excel表。

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

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

EntryDate    ColumnA     ColumnB    (...)

步骤

  1. 导入标题下方的数据
  2. 选择包含日期的单元格
  3. 从字符串中提取日期
  4. 填写SQL表格中的EntryDate列
  5. 我找到a method来定义要导入的行,但SSIS不接受格式化并恢复为表名。我还没有找到导入日期的解决方案。

1 个答案:

答案 0 :(得分:0)

我不知道如何使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