SSIS中遍历EXCEL的全部sheets

  为了使用SSIS读取EXCEL中的全部sheet,操作步骤如下:
1. 建立package级别的变量:
  • ExcelFile EXCEL数据文件名
  • ExcelTables Excel Sheets集合变量
  • TableName 当前读取的SHEET名称
2. 拖放一个脚本组件,用于从excel中读取sheets name到变量ExcelTables。因此只读变量设为ExcelFile ,读写变量设为ExcelTables ,代码如下:
        Dim excelFile As String
        Dim connectionString As String
        Dim excelConnection As OleDbConnection
        Dim tablesInFile As DataTable
        Dim tableCount As Integer = 0
        Dim tableInFile As DataRow
        Dim currentTable As String
        Dim tableIndex As Integer = 0

        Dim excelTables As New ArrayList()

        excelFile = Dts.Variables("ExcelFile").Value.ToString
        connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & excelFile & _
            ";Extended Properties=Excel 8.0"
        excelConnection = New OleDbConnection(connectionString)
        excelConnection.Open()
        tablesInFile = excelConnection.GetSchema("Tables")
        tableCount = tablesInFile.Rows.Count
        For Each tableInFile In tablesInFile.Rows
            currentTable = tableInFile.Item("TABLE_NAME").ToString

            If (currentTable.StartsWith("'Hotmail UTS") Or currentTable.StartsWith("'TV UTS")) And currentTable.EndsWith("'") Then
                excelTables.Add(currentTable)

                Dts.Log(currentTable, 22, Nothing)
                tableIndex += 1
            End If
        Next

        Dts.Variables("ExcelTables").Value = excelTables

        Dts.TaskResult = Dts.Results.Success

3. 拖放一个Foreach Loop组件,连接在脚本组件后,并做以下设置
  • 枚举器为For each from variables
  • 变量为ExcelTables
  • 变量映射 User:TableName = Param 0
4. 在foreach Loop组件中放一个数据流组件

5. 在数据流中放上EXCEL源数据组件
配置如下:
  • 数据访问模式 - 表名或者视图名称变量
  • 变量名 - TableName
  • ValidateExternalMetadata - False
这时,配置基本结束,剩下就是如何导出数据了。




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值