Autofit Column Width using SSIS ETL on Visual Studio

时间:2017-06-12 16:54:29

标签: sql-server excel ssis etl column-width

I´m creating an ETL on visual studio, but when I export my data to a table on Excel, the columns seem a bit short. Is there a way to autofit the column width from visual studio? Thank you

Update 1

Here is the code as I copied it and also the error I get

Code

error

1 个答案:

答案 0 :(得分:1)

您可以使用在script task成功后执行的DataFlow Task来实现此目的。您必须将Microsoft.Office.Interop.Excel程序集添加到脚本任务引用中并使用以下代码: (使用Vb.Net)

注意:您必须将Microsoft.Office.Interop.Excel.dll文件添加到以下目录(.Net Framework dll目录)C:\ Windows \ Microsoft.NET \ Framework \ v2.0.50727和(sql)服务器数据工具DLL目录)C:\ Program Files \ Microsoft SQL Server \ 100 \ DTS \ Binn(使用vs 2005和sql 2008),然后在脚本任务中添加此dll作为参考

Imports Microsoft.Office.Interop

Public Sub Main()

    Dim strFile as String = "C:\New Folder\1.xls"
    Dim m_XlApp As New Excel.Application
    Dim m_xlWrkbs As Excel.Workbooks = m_XlApp.Workbooks
    Dim m_xlWrkb As Excel.Workbook

    m_xlWrkb = m_xlWrkbs.Open(strFile)

    'Loop over all worksheets
    For Each m_XlWrkSheet As Excel.Worksheet In m_xlWrkb.Worksheets

        'Columns    
        m_XlWrkSheet.UsedRange.Columns.AutoFit()

        'Rows
        m_XlWrkSheet.UsedRange.Rows.AutoFit()


    Next

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

End Sub