清单1.1 从应用程序对象导航到Excel中的工作表
Dim myWorkbooks As Excel.Workbooks = app.Workbooks Dim myWorkbook As Excel.Workbook = myWorkbooks.Item(1) Dim myWorksheets As Excel.Sheets = myWorkbook.Worksheets Dim myWorksheet As Excel.Worksheet myWorksheet = CType(myWorksheets.Item(1), Excel.Worksheet)
如果代码不需要在变量中缓存每个对象模型对象,但只需要获取一个Worksheet对象,则编写此代码的更有效的方法如下所示:
Dim myWorksheet As Excel.Worksheet myWorksheet = CType(app.Workbooks.Item(1).Worksheets.Item(1), Excel.Worksheet)
活代码:第一步:创建visual basic 2017的窗体应用程序(运行环境:win 10+visual studio 2017+office 2010)
第二步:添加引用:COM的 "Microsoft Office 14.0 Object Library 2.5"(指的是Office 2010)和“程序集”的"扩展"中的"Microsoft.office.Interop.Excel 14.0.0.0"
第三步:代码
Imports Excel = Microsoft.Office.Interop.Excel Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim app As Excel.Application = New Excel.Application Dim myWorkbook As Excel.Workbook Dim myWorksheet As Excel.Worksheet app.Visible = True myWorkbook = app.Workbooks.Add() myWorksheet = CType(myWorkbook.Sheets.Add(), Excel.Worksheet) myWorksheet.Cells(1, 1) = "这是A1" End Sub End Class
第四步:运行结果
清单1.2 使用整数或String作为Count属性和Item属性索引遍历工作表集合
Dim myWorkbooks As Excel.Workbooks = app.Workbooks Dim workbookCount As Integer = myWorkbooks.Count For i As Integer = 1 To workbookCount ‘ Get the workbook by its integer index Dim myWorkbook As Excel.Workbook = myWorkbooks.Item(i) ‘ Get the workbook by its string index Dim workbookName As String = myWorkbook.Name Dim myWorkbook2 As Excel.Workbook = myWorkbooks.Item(workbookName) MsgBox(String.Format("Workbook {0}", myWorkbook2.Name)) Next
活代码:
活代码:第一步:创建visual basic 2017的窗体应用程序(运行环境:win 10+visual studio 2017+office 2010)
第二步:添加引用:COM的 "Microsoft Office 14.0 Object Library 2.5"(指的是Office 2010)和“程序集”的"扩展"中的"Microsoft.office.Interop.Excel 14.0.0.0"
第三步:代码
Imports Excel = Microsoft.Office.Interop.Excel Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim app As Excel.Application = New Excel.Application Dim myWorkbook As Excel.Workbook Dim myWorksheet As Excel.Worksheet app.Visible = True myWorkbook = app.Workbooks.Add() myWorksheet = CType(myWorkbook.Sheets.Add(), Excel.Worksheet) myWorksheet.Cells(1, 1) = "这是A1" Dim worksheetCount As Integer = myWorkbook.Worksheets.Count For i As Integer = 1 To worksheetCount Dim str As String str = myWorkbook.Worksheets.Item(i).Name ‘以整数作为索引 MessageBox.Show(str, "获取工作表名称") Next For i As Integer = 1 To worksheetCount Dim str As String str = myWorkbook.Worksheets.Item("sheet" & i).Name MessageBox.Show(str, "获取工作表名称") ‘以字符串作为索引 Next End Sub End Class
第四步:运行结果
拓展假如要编辑工作表sheet2中D3单元格,并填入“我是丑丑”,该如何实现呢
Imports Excel = Microsoft.Office.Interop.Excel Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim app As Excel.Application = New Excel.Application Dim myWorkbook As Excel.Workbook Dim myWorksheet As Excel.Worksheet app.Visible = True myWorkbook = app.Workbooks.Add() ‘myWorksheet = CType(myWorkbook.Sheets.Add(, , 3,), Excel.Worksheet) ‘第3个参数表示添加多少工作表,这里添加3个, Dim C4_sheet As Excel.Worksheet = myWorkbook.Worksheets.Item("sheet2") ‘工作表的索引从0开始, C4_sheet.Cells(3, 4) = "我是丑丑" ‘Cells(行,列),也就是D3单元格 End Sub End Class
运行结果:
清单1.3 使用For Each结构遍历工作表集合
Dim myWorkbooks As Excel.Workbooks = app.Workbooks For Each workbook As Excel.Workbook In myWorkbooks MsgBox(String.Format("Workbook {0}", workbook.Name)) Next
时间: 2024-10-19 01:44:58