0

在 ssis Excel Source Editor 中,可以使用 Excel 工作表的名称。有什么方法可以将该名称添加到变量上?或者有什么方法可以获取 xlsx 文件中每张纸的名称?使用 vb 或任何其他 ssis 方法?

4

3 回答 3

3

尝试这个:

For i=0 to ActiveWorkbook.Sheets.Count-1
   msgbox(WorkSheets(i).Name)
Next

我不知道你如何需要结果。您可能希望将其存储在数组中。

于 2013-08-30T12:56:17.637 回答
0

这将起作用(将代码插入代码模块):

Sub IterateAllWorksheetNames()
    dim wksht as Worksheet, FriendlyNameStr as String, CodeNameStr As String

    For Each wksht in ThisWorkbook.Worksheets
        wkshtNameStr = wksht.Name '// This is your worksheet "friendly" name 
                                  '// (..the name that is on the worksheet tab).

        CodeNameStr = wksht.CodeName '// This is the CodeName of the worksheet
                                     '// (..the actual object name in the collection)
    Next wksht


    '//  NOTE: By Default the CodeName and FriendlyName are by default the same (sheet1,
    '//  sheet2..) so don't get confused. You're probably looking for just "wksht.Name"

End Sub

更新:对不起,我没有看到你想要它在一个表中。这将在 Sheet2 中添加一个表。确保您的工作簿具有“Sheet2”,或者只是将任何 EMPTY 工作表名称插入到 Sheets() 方法中。

Dim wksht As Worksheet: Set wksht = Sheets("Sheet2")

With wksht.ListObjects.Add(xlSrcRange)
    .Name = "MyWorksheetNames"
    .ListColumns(1).Name = "FriendlyNames"
    .ListColumns.Add(2).Name = "CodeNames"

    For Each x In ThisWorkbook.Worksheets
        With .ListRows.Add
            .Range(1, 1).Value = x.Name
            .Range(1, 2).Value = x.CodeName
        End With
    Next x
End With
于 2013-08-31T00:56:22.020 回答
0

在 vb.net 中尝试这个将工作表名称绑定到 Combobox

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load        Dim filePath As String = "C:\Book1.xlsx" 
Dim connString As String = String.Empty        
If filePath.EndsWith(".xlsx") Then            '2007 Format            
connString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=No'", filePath)        
Else            '2003 Format            
connString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=No'", filePath)        
End If         'Get the Sheets in Excel WorkBook        
Dim connExcel As New OleDbConnection(connString)        
Dim cmdExcel As New OleDbCommand()        
Dim oda As New OleDbDataAdapter()        
cmdExcel.Connection = connExcel        
connExcel.Open()        
cmbSheets.DataSource = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)        cmbSheets.DisplayMember = "TABLE_NAME"        cmbSheets.ValueMember = "TABLE_NAME"        connExcel.Close()    
End Sub
于 2014-12-16T09:41:56.303 回答