2

Is it possible to format a column in an excel destination in ssis before generating it? I'm thinking a script task? I want to format a column to be date/time format within the excel spreadsheet

Hadi
  • 36,233
  • 13
  • 65
  • 124
sql2015
  • 591
  • 3
  • 13
  • 34

1 Answers1

1

You can use Microsoft.Interop.Excel library and use NumberFormat property to change EntireColumn format to datetime.

Note: you have to add Microsoft.Office.Interop.Excel.dll file to the following directories (.Net Framework dll directory) C:\Windows\Microsoft.NET\Framework\v2.0.50727 and (sql server data tools dll directory) C:\Program Files\Microsoft SQL Server\100\DTS\Binn (if using vs 2005 and sql 2008) and then add this dll as a reference in your script task

Imports Microsoft.Interop.Excel

Public Sub Main()

        Dim m_XlApp = New Excel.Application
        Dim m_xlWrkbs As Excel.Workbooks = m_XlApp.Workbooks
        Dim m_xlWrkb As Excel.Workbook
        m_xlWrkb = m_xlWrkbs.Open("D:\1.xlsx")

        Dim m_XlWrkSheet As Excel.Worksheet = m_xlWrkb.Worksheets(1)

        m_XlWrkSheet.Columns(1).NumberFormat = "HH:mm:ss"
        'OR
        'ExcelWorksheet.Cells(1,1).EntireColumn.NumberFormat = "HH:mm:ss"

        m_xlWrkb.Save()
        m_xlWrkb.Close(SaveChanges:=True)

        Marshal.ReleaseComObject(m_xlWrkb)
        Marshal.ReleaseComObject(m_xlWrkbs)
        m_XlApp.Quit()
        Marshal.ReleaseComObject(m_XlApp)


        Dts.TaskResult = ScriptResults.Success

End Sub

References

Hadi
  • 36,233
  • 13
  • 65
  • 124
  • 1
    Thank you for your help. I think my issue might be because the column is set to text in the Database. so I have column A which is a time value (column data type set to text) and I have a duration column (also set as text) I use a cast in my SQL statement to add these values together to get a end time and output as a time value. this displays fine is SQL Server its when SSIS exports as excel that the time value does not show correctly. so I think the best way is to script the excel cell to be a time format is that correct? – sql2015 Jan 05 '18 at 08:41
  • Yes script task will solve the issue. Also it is good to try changing column datatype from the Excel Destination advanced editor. Just right click on this components and click on `Show advanced editor`. – Hadi Jan 05 '18 at 11:12
  • 1
    I see that you asked a new question about this issue. So if this answer solved the questiin just accept it. – Hadi Jan 05 '18 at 11:15
  • @sql2015 the other question you asked can be solved in this way. have you tried my answer? – Hadi Jan 05 '18 at 21:48
  • 1
    yes I managed to get date/time showing in excel by going through the show advanced editor and changing the data type that way and correctly shows the date and time but doesn't show the seconds which I need – sql2015 Jan 08 '18 at 10:52
  • Then you have to use a script task like shown in the answer. Excel use current culture format by default – Hadi Jan 08 '18 at 10:56
  • I'm new to C# so trying to get my head round it – sql2015 Jan 08 '18 at 11:00