摘要
本文示範如何將 Microsoft Excel 自動化,以及如何使用數值陣列填入多儲存格範圍。 本文也說明如何使用自動化,以陣列方式擷取多儲存格範圍。
其他相關資訊
若要填入多儲存格範圍而不一次填入一個儲存格,您可以將 Range 物件的 Value 屬性設定為二維陣列。 同樣地,您可以使用 Value 屬性,一次擷取多個儲存格的二維值陣列。 下列步驟示範使用二維陣列設定和擷取資料的這個程式。
建置適用于 Microsoft Excel 的自動化用戶端
啟動 Microsoft Visual Studio .NET。
在 [檔案] 功能表上,按一下 [新增],然後按一下 [專案]。 從 Visual Basic 專案類型中選取 [Windows 應用程式]。 根據預設,會建立 Form1。
新增 Microsoft Excel 物件程式庫的參考。 如果要執行這項操作,請依照下列步驟執行:
- 按一下 [專案] 功能表上的 [加入參考]。
- 在 [COM] 索引標籤上,找出 [Microsoft Excel 物件程式庫],然後按一下 [選取]。
注意 Microsoft Office 2007 和 Microsoft Office 2003 包含主要 Interop 元件 (PIA) 。 Microsoft Office XP 不包含 PIA,但可以下載。
按一下 [新增參考] 對話方塊中的 [確定] 以接受您的選擇。 如果系統提示您為選取的程式庫產生包裝函式,請按一下 [是]。
在 [檢視] 功能表上,選取 [工具箱] 以顯示 [工具箱]。 將兩個按鈕和一個核取方塊新增至 Form1。
將核取方塊的 Name 屬性設定為 FillWithStrings。
按兩下 Button1。 [表單] 的程式碼視窗隨即出現。
將下列內容新增至 Form1.vb 的頂端:
Imports Microsoft.Office.Interop
在程式碼視窗中,取代下列程式碼
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click End Sub
搭配:
'Keep the application object and the workbook object global, so you can 'retrieve the data in Button2_Click that was set in Button1_Click. Dim objApp As Excel.Application Dim objBook As Excel._Workbook Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim objBooks As Excel.Workbooks Dim objSheets As Excel.Sheets Dim objSheet As Excel._Worksheet Dim range As Excel.Range ' Create a new instance of Excel and start a new workbook. objApp = New Excel.Application() objBooks = objApp.Workbooks objBook = objBooks.Add objSheets = objBook.Worksheets objSheet = objSheets(1) 'Get the range where the starting cell has the address 'm_sStartingCell and its dimensions are m_iNumRows x m_iNumCols. range = objSheet.Range("A1", Reflection.Missing.Value) range = range.Resize(5, 5) If (Me.FillWithStrings.Checked = False) Then 'Create an array. Dim saRet(5, 5) As Double 'Fill the array. Dim iRow As Long Dim iCol As Long For iRow = 0 To 5 For iCol = 0 To 5 'Put a counter in the cell. saRet(iRow, iCol) = iRow * iCol Next iCol Next iRow 'Set the range value to the array. range.Value = saRet Else 'Create an array. Dim saRet(5, 5) As String 'Fill the array. Dim iRow As Long Dim iCol As Long For iRow = 0 To 5 For iCol = 0 To 5 'Put the row and column address in the cell. saRet(iRow, iCol) = iRow.ToString() + "|" + iCol.ToString() Next iCol Next iRow 'Set the range value to the array. range.Value = saRet End If 'Return control of Excel to the user. objApp.Visible = True objApp.UserControl = True 'Clean up a little. range = Nothing objSheet = Nothing objSheets = Nothing objBooks = Nothing End Sub
返回 Form1 的設計檢視,然後按兩下 Button2。
在程式碼視窗中,取代下列程式碼
Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click End Sub
搭配:
Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click Dim objSheets As Excel.Sheets Dim objSheet As Excel._Worksheet Dim range As Excel.Range 'Get a reference to the first sheet of the workbook. On Error Goto ExcelNotRunning objSheets = objBook.Worksheets objSheet = objSheets(1) ExcelNotRunning: If (Not (Err.Number = 0)) Then MessageBox.Show("Cannot find the Excel workbook. Try clicking Button1 to " + _ "create an Excel workbook with data before running Button2.", _ "Missing Workbook?") 'We cannot automate Excel if we cannot find the data we created, 'so leave the subroutine. Exit Sub End If 'Get a range of data. range = objSheet.Range("A1", "E5") 'Retrieve the data from the range. Dim saRet(,) As Object saRet = range.Value 'Determine the dimensions of the array. Dim iRows As Long Dim iCols As Long iRows = saRet.GetUpperBound(0) iCols = saRet.GetUpperBound(1) 'Build a string that contains the data of the array. Dim valueString As String valueString = "Array Data" + vbCrLf Dim rowCounter As Long Dim colCounter As Long For rowCounter = 1 To iRows For colCounter = 1 To iCols 'Write the next value into the string. valueString = String.Concat(valueString, _ saRet(rowCounter, colCounter).ToString() + ", ") Next colCounter 'Write in a new line. valueString = String.Concat(valueString, vbCrLf) Next rowCounter 'Report the value of the array. MessageBox.Show(valueString, "Array Values") 'Clean up a little. range = Nothing objSheet = Nothing objSheets = Nothing End Sub
測試自動化用戶端
- 按 F5 建置 並執行範例程式。
- 按一下 [按鈕1]。 Microsoft Excel 會開始使用新的活頁簿,而第一張工作表的 A1:E5 儲存格會填入陣列中的數值資料。
- 按一下 [按鈕2]。 程式會將儲存格 A1:E5 中的資料擷取到新的陣列,並在訊息方塊中顯示結果。
- 選取 [FillWithStrings],然後按一下 [Button1] 以字串資料填入儲存格 A1:E5。
參考
如需使用陣列來設定及擷取舊版 Visual Studio Excel 資料的其他資訊,請按一下下列文章編號以檢視 Microsoft 知識庫中的文章:
247412 資訊:從 Visual Basic 將資料傳輸至 Excel 的方法