SSIS Script task to convert XLSX files with multiple sheets to CSV file

**** Posting it again as someone moved my previous post to VB forums, the reason i am here is to get help on c#, vb code that can be  fit into SSIS Script task****

Hello,

i have been strunggling to find solution to convert XLSX files with multiple sheets to csv file. can any one please help me.

Requirements

>> Convert XLSX file with multiple sheets to CSV file

>> CSV file names : XLSX filename + '_' + sheet name

>> scirpt has to be in VB as i am using ssis 2005

>> i started develping scirpt using Micorosoft.office.interop.Excel.dll . this dll is referenced to script task.

>> found bellow web link as useful.. because i am not god at progarmming, couldn't understnad some of it.

http://www.mssqltips.com/sqlservertip/2772/importing-data-from-excel-using-ssis--part-2/

http://www.dreamincode.net/forums/topic/278621-help-convert-xls-to-csv-vbnet-by-worksheet/

Thanks in Advance

Jai

December 11th, 2014 3:04pm

I did, because you will have a better luck in a C# or VB forums.

Or just search online because it was already solved 100s of times e.g. http://stackoverflow.com/questions/12162477/importing-multiple-csv-to-multiple-worksheet-in-a-single-workbook

my search result has dozens of pages returned: http://tinyurl.com/omsunng

Also, if your code works in a console C# app it should inside the Script Task, too.

You have a programming in C# question, not SSIS.

I will leave this thread over here if you wish and see how quickly you get help from the ETL community.

Free Windows Admin Tool Kit Click here and download it now
December 11th, 2014 3:12pm

I did, because you will have a better luck in a C# or VB forums.

>>Thanks for that, appreciate your idea

Or just search online because it was already solved 100s of times e.g. http://stackoverflow.com/questions/12162477/importing-multiple-csv-to-multiple-worksheet-in-a-single-workbook

>> above solution is for loading multiple csv files to different sheets in excel..i was looking other way around.

my search result has dozens of pages returned: http://tinyurl.com/omsunng

>> do you think i just posted here after some one gave requirement, i have been working on this for few hours. have been searching google since i started this task.. but no luck, i have provided links to possible solutions

Also, if your code works in a console C# app it should inside the Script Task, too.

>>its not just about code...what dlls need to reference in scipt task of ssis. using ssis 2005, found c# code (which is in fist link), but it is not working properly when i convert to VB.

I will leave this thread over here if you wish and see how quickly you get help from the ETL community.

>>Being MVP, i guess you know what i am trying to say here..thanks again

December 11th, 2014 3:50pm

' Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic
' The ScriptMain class is the entry point of the Script Task.
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports Excel = Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop.Excel
Public Class ScriptMain
    Private Shared mWorkBook As Workbook
    Private Shared mWorkSheets As Sheets
    Private Shared mWSheet1 As Worksheet
    Private Shared oXL As Excel.Application
    Private Shared ErrorMessage As String = String.Empty
    Enum ScriptResults
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    End Enum
    Public Sub Main()
        Dts.TaskResult = Dts.Results.Success
        Try
            Dim sourceExcelPathAndName As String = "C:\Users\Vijay\Documents\m.xlsx"
            Dim targetCSVPathAndName As String = "C:\Users\Vijay\Documents\multi"
            Dim excelSheetName As String = "Sheet1"
            Dim columnDelimeter As String = "|#|"
            Dim headerRowsToSkip As Integer = 0
            If ConvertExcelToCSV(sourceExcelPathAndName, targetCSVPathAndName, excelSheetName, columnDelimeter, headerRowsToSkip) = True Then
                MsgBox("Passed", MsgBoxStyle.Information)
            Else
                Dts.TaskResult = CInt(ScriptResults.Failure)
            End If
        Catch ex As Exception
            Dts.TaskResult = CInt(ScriptResults.Failure)
            MsgBox(Dts.TaskResult, MsgBoxStyle.Information)
        End Try
        Dts.TaskResult = Dts.Results.Success
    End Sub
    Public Shared Function ConvertExcelToCSV(ByVal sourceExcelPathAndName As String, ByVal targetCSVPathAndName As String, ByVal excelSheetName As String, ByVal columnDelimeter As String, ByVal headerRowsToSkip As Integer) As Boolean
        Try
            oXL = New Excel.Application()
            oXL.Visible = False
            oXL.DisplayAlerts = False
            Dim workbooks As Excel.Workbooks = oXL.Workbooks
            mWorkBook = workbooks.Open(sourceExcelPathAndName, 0, False, 5, "", "", _
            False, XlPlatform.xlWindows, "", True, False, 0, _
            True, False, False)
            mWorkSheets = mWorkBook.Worksheets
            Dim ws As Worksheet
            For Each ws In mWorkSheets
                ws.Select()
                Dim range As Excel.Range = ws.UsedRange
                Dim rngCurrentRow As Excel.Range
                For i As Integer = 0 To headerRowsToSkip - 1
                    rngCurrentRow = range.EntireRow
                    rngCurrentRow.Delete(XlDeleteShiftDirection.xlShiftUp)
                Next
                range.Replace(vbLf, " ", Type.Missing, Type.Missing, Type.Missing, Type.Missing, _
                Type.Missing, Type.Missing)
                range.Replace(",", columnDelimeter, Type.Missing, Type.Missing, Type.Missing, Type.Missing, _
                Type.Missing, Type.Missing)
                Dim targetFile As String
                targetFile = targetCSVPathAndName & ws.Name & ".csv"
                mWorkBook.SaveAs(targetFile, XlFileFormat.xlCSVMSDOS, Type.Missing, Type.Missing, Type.Missing, Type.Missing, _
                Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, False)
                If ws IsNot Nothing Then
                    ws = Nothing
                End If
            Next
            If mWorkBook IsNot Nothing Then
                mWorkBook.Close(Type.Missing, Type.Missing, Type.Missing)
            End If
            If mWorkBook IsNot Nothing Then
                mWorkBook = Nothing
            End If
            Return True
        Catch ex As Exception
            ErrorMessage = ex.ToString()
            Return False
        Finally
            If oXL IsNot Nothing Then
                oXL.Quit()
            End If
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL)
            If oXL IsNot Nothing Then
                oXL = Nothing
            End If
            GC.WaitForPendingFinalizers()
            GC.Collect()
            GC.WaitForPendingFinalizers()
            GC.Collect()
        End Try
    End Function
End Class

  • Marked as answer by V_jai Friday, December 12, 2014 2:15 PM
Free Windows Admin Tool Kit Click here and download it now
December 12th, 2014 2:15pm

This did it! I was looking to extract several worksheets into separate .csv files so I can then import using SSIS. Thanks V_jai!
July 14th, 2015 7:45pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics