码迷,mamicode.com
首页 > 其他好文 > 详细

Visual Basic 2017 操作Excel和word【1】

时间:2017-09-24 20:30:33      阅读:508      评论:0      收藏:0      [点我收藏+]

标签:http   book   cat   rar   9.png   send   ges   cells   表名   

  清单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

 

Visual Basic 2017 操作Excel和word【1】

标签:http   book   cat   rar   9.png   send   ges   cells   表名   

原文地址:http://www.cnblogs.com/xiehaofeng/p/7588109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!