Home » excel » sql server – How do I find the right syntax for VB.Net in SSIS 2013 using Microsoft.Office.Interop.Excel?

sql server – How do I find the right syntax for VB.Net in SSIS 2013 using Microsoft.Office.Interop.Excel?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am using SSIS to load data from Excel 2013 to SQL Server 2014. One column has data that exceeds 255 char. Using the source Excel task will only read the first 255 char truncating the rest.

I wrote a .vb script to open the file and save it as a csv while also keeping all of the char past 255, however i have not found how to make a specified sheet active before saving it as a csv file.

I decided to convert the code to VB.Net using Microsoft.Office.Interop.Excel to access the file, however I still have a problem getting the right syntax. I cannot find anything for VB.Net using Microsoft.Office.Interop.Excel.

Public Sub Main()

    Dim FilePath As String = "I:\DSS Clarity\Clarity Technical\METADATA MINING\INPUT_SAP_BO_UNIVERSE_FILES\"
    Dim FileName As String = "UNV Universes.xlsx"


    Dim excel As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application
    excel.DisplayAlerts = False

    ' Open Excel spreadsheet.
    Dim wb As Workbook
    wb = excel.Workbooks.Open(FilePath & FileName)

    'wb.Worksheets.Select("Tables")    <-- need to make the Tables sheet active - how do i do that?


    wb.SaveAs(FilePath & Left(FileName, InStrRev(FileName, ".")) & "csv", 24)
    wb.Close()

    Dts.TaskResult = ScriptResults.Success
End Sub

I am expecting to see all of the data for a specified sheet saved in csv format, not just the excel constraint of max 255 char for one field.

How to&Answers:

I made the following edits to your code:

  1. I selected the first worksheet before saving
  2. I edited the SaveAs() function parameters
  3. You must Close the application using excel.Quit() command at the end of the script
Public Sub Main()

    Dim FilePath As String = "I:\DSS Clarity\Clarity Technical\METADATA MINING\INPUT_SAP_BO_UNIVERSE_FILES\"
    Dim FileName As String = "UNV Universes.xlsx"


    Dim excel As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application
    excel.DisplayAlerts = False

    ' Open Excel spreadsheet.
    Dim wb As Workbook
    wb = excel.Workbooks.Open(FilePath & FileName)

    wb.Worksheets(0).Select(Type.Missing) 'Or try wb.Worksheets(0).Select()

    wb.SaveAs(FilePath & Left(FileName, InStrRev(FileName, ".")) & "csv", Excel.XlFileFormat.xlCSV,Excel.XlSaveAsAccessMode.xlNoChange)
    wb.Close(false)
    excel.Quit()

    Dts.TaskResult = ScriptResults.Success

End Sub

<a href="https://exceptionshub.com/category/sql-server”>More Answers>