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