I think that the best way to import a file like this one is to make a Script task to convert the Excel file into a CSV and then upload the CSV into the database as a simple flat file source.
- Create two variables named
VarDataPath
with the path of Excel file and VarCSVPath
with the path of the CSV generated file.
- Create a Script task with the following VB.net snippet. Unfortunally I have not SSIS on my computer at the moment so I can't try my code.
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Public Class ScriptMain
Public Sub Main()
Dim xl As Object
Dim wb As Object
Dim sh As Object
Dim NumSheets As Integer
Dim counter As Long
Dim fs As Object
Dim conv_file As Object
Dim line As String
Dim strDataPath As String = Dts.Variables("VarDataPath").Value.ToString() 'This variable contains the Excel path
Dim strFileName As String = Dts.Variables("VarCSVPath").Value.ToString() 'This variable contains the CSV path
Dim myArray As Integer() = New Integer() {4, 10, 16} ' Row number of FirstName/LastName
xl = CreateObject("Excel.Application")
wb = xl.WorkBooks.Open(strDataPath)
sh = wb.Sheets(1)
fs = CreateObject("Scripting.FileSystemObject")
conv_file = fs.CreateTextFile(strFileName, True)
' CSV file head
conv_file.writeline("Name,Col1,Col2,Col3,Col4,Col5")
For Each val In myArray
For a = 2 To 5 ' I have to loop all 4 rows of tables starting from 2 rows down the FirstName/LastName cell
' Each line I wrote valeu of Name,Col1,Col2,Col3,Col4,Col5
line = ""
line=line & sh.Cells(val, 1).ToString & ";" 'Name
line=line & sh.Cells(val+a, 1).ToString & ";" 'Col1
line=line & sh.Cells(val+a, 2).ToString & ";" 'Col2
line=line & sh.Cells(val+a, 3).ToString & ";" 'Col3
line=line & sh.Cells(val+a, 4).ToString & ";" 'Col4
line=line & sh.Cells(val+a, 5).ToString & ";" 'Col5
conv_file.writeline(line)
Next
Next
conv_file.close()
sh = Nothing
wb.Close(False)
wb = Nothing
xl.Quit()
xl = Nothing
Dts.TaskResult = Dts.Results.Success
End Sub
End Class
- Execute the package to let SSIS create a CSV file.
- Add a data flow to the package to upload the CSV generated into the database.
Also, you can have a look at these articles: