2013-08-30 49 views
0

在ssis Excel Source Editor中,excel工作表的名稱可用。 有沒有辦法讓這個名字變成一個變量? 或者是否有任何方法可以獲取xlsx文件中每張圖片的名稱? 使用VB或任何其他ssis方法?如何使用vb/ssis獲取每張excel的名稱

+0

可能重複[如何跳過foreach循環容器中的項目設置爲foreach ado?](http://stackoverflow.com/questions/8311752/how-can-i-skip-items-in-a-foreach-loop -container-set-to-foreach-ado-enumerator) – billinkc

回答

3

試試這個:

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

我不知道你是怎麼需要的結果。你可能想要將它存儲在一個數組中。

+0

我需要將此名稱存儲到目標表中 – user1254579

+0

它顯示ActiveWorkbook未聲明 – user1254579

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 

UPDATE: 對不起,我沒有看到你希望它在一個表中。這將一個表添加到Sheet2。確保您的工作簿中有一個「Sheet2中」,或剛插入的任何空白工作表名稱爲表()方法..

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 
0

在vb.net綁定表名稱嘗試使用此方法組合框

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 
相關問題