我可以帮助您获取 VS 2008 中的工作表名称(2010 年可能相同)。
创建 Object 类型的变量 (objExcelSheets) 创建脚本任务。将文件名/路径变量添加到脚本(只读) 将对象变量添加到读写变量
这是脚本任务的一些代码
Private Sub GetExcelSheets()
Dim excelFile, connstr, curTable As String
Dim excelConnection As OleDb.OleDbConnection
Dim tablesInFile As DataTable
Dim tablenameInFile As DataRow
Dim tableCount As Integer = 0
Dim tableIndex As Integer = 0
Dim excelTables As String()
Dim blnFound As Boolean = False
ReDim excelTables(0)
excelFile = Dts.Variables("sFilePath").Value.ToString
connstr = GetExcelConnString()
excelConnection = New OleDb.OleDbConnection(connstr)
excelConnection.Open()
tablesInFile = excelConnection.GetSchema("Tables")
tableCount = tablesInFile.Rows.Count
For Each tablenameInFile In tablesInFile.Rows
curTable = tablenameInFile.Item("TABLE_NAME").ToString.Trim.ToLower
If curTable.IndexOf("SOMETHING_IN_THE_SHEETS_TO_PROCESS") >= 0 Then
blnFound = True
ReDim excelTables(tableIndex)
excelTables(tableIndex) = "[" + curTable + "]"
tableIndex += 1
End If
Next
If IsNothing(excelTables(0)) Then excelTables(0) = String.Empty
excelConnection.Close()
Dts.Variables("objExcelSheet").Value = excelTables
End Sub
Private Function GetExcelConnString() As String
Dim sExtendedProperties, sExtension, sFilePath, sExcelConn As String
sFilePath = Dts.Variables("sFilePath").Value.ToString
sExtension = sFilePath.Substring(sFilePath.LastIndexOf("."))
If sExtension.ToLower = ".xlsx" Then
sExtendedProperties = ";Extended Properties=""EXCEL 12.0;HDR=NO"";"
ElseIf sExtension.ToLower = ".xls" Then
sExtendedProperties = ";Extended Properties=""EXCEL 8.0;HDR=NO;IMEX=1"";"
Else
sExtendedProperties = String.Empty
End If
sExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sFilePath & sExtendedProperties
Return sExcelConn
End Function
请注意,这需要安装 ACE 驱动程序,如果您没有这些驱动程序,则需要将 sExcelConn 替换为文件的连接字符串。
这会将工作表名称中带有 SOMETHING_IN_THE_SHEETS_TO_PROCESS 的所有 Excel 工作表放入对象变量 objExcelSheet。您可以将其替换为您需要的任何内容或将其全部删除。
然后,您可以执行 ForEach 循环来处理每张纸。
来自变量枚举器的 Foreach - 变量 = objExcelSheet
变量映射 - 变量(索引为 0 的工作表名称)
这应该可以帮助您获得所需的内容,您可以在其中动态选择要处理的工作表并从那里做您需要做的事情。