HOW TO:列出活頁簿中的所有工作表
Microsoft.Office.Interop.Excel.Workbook 類別會提供 Microsoft.Office.Interop.Excel.Worksheets 物件。 這個物件在活頁簿中含有所有 Microsoft.Office.Interop.Excel.Worksheet 物件的集合。
**適用於:**本主題中的資訊適用於 Excel 2007 和 Excel 2010 的文件層級專案和應用程式層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能。
若要在文件層級自訂中列出活頁簿的全部現有工作表
在 Worksheets 集合中逐一查看每個工作表的名稱,並將名稱傳送到從 NamedRange 控制項位移 (Offset) 的儲存格。
Private Sub ListSheets() Dim index As Integer = 0 Dim NamedRange1 As Microsoft.Office.Tools.Excel.NamedRange = _ Globals.Sheet1.Controls.AddNamedRange( _ Globals.Sheet1.Range("A1"), "NamedRange1") For Each displayWorksheet As Excel.Worksheet In Globals.ThisWorkbook.Worksheets NamedRange1.Offset(index, 0).Value2 = displayWorksheet.Name index += 1 Next displayWorksheet End Sub
private void ListSheets() { int index = 0; Microsoft.Office.Tools.Excel.NamedRange NamedRange1 = Globals.Sheet1.Controls.AddNamedRange( Globals.Sheet1.Range["A1", missing], "NamedRange1"); foreach (Excel.Worksheet displayWorksheet in Globals.ThisWorkbook.Worksheets) { NamedRange1.Offset[index, 0].Value2 = displayWorksheet.Name; index++; } }
若要在應用程式層級增益集中列出活頁簿的全部現有工作表
在 Worksheets 集合中逐一查看每個工作表的名稱,並將名稱傳送到與 Microsoft.Office.Interop.Excel.Range 物件相距某位移的儲存格。
Private Sub ListSheets() Dim index As Integer = 0 Dim rng As Excel.Range = Me.Application.Range("A1") For Each displayWorksheet As Excel.Worksheet In Me.Application.Worksheets rng.Offset(index, 0).Value2 = displayWorksheet.Name index += 1 Next displayWorksheet End Sub
private void ListSheets() { int index = 0; Excel.Range rng = this.Application.get_Range("A1", missing); foreach (Excel.Worksheet displayWorksheet in this.Application.Worksheets) { rng.get_Offset(index, 0).Value2 = displayWorksheet.Name; index++; } }