VB.Net Excel工作表

VB.Net支持Microsoft Excel 2010的COM对象模型和应用程序之间的互操作性。

要应用程序中使用这种互操作性,需要在Windows窗体应用程序中导入名称空间Microsoft.Office.Interop.Excel

VB.Net创建一个Excel文件的应用程序

让我们开始创建一个窗体窗体应用程序按照以下步骤在Microsoft Visual Studio中:文件 -> 新建项目 -> Windows窗体应用程序

最后,选择确定,Microsoft Visual Studio创建一个项目(ExcelSheet)并显示以下Form1

在窗体中插入一个Button控件Button1。
将Microsoft Excel对象库的引用添加到项目。参考以下步骤:

  • 从项目菜单中选择添加引用。如下图所示 -
  • 在COM选项卡上找到Microsoft Excel对象库,然后单击选择。如下图所示 -

  • 最后点击确定

双击代码窗口并填充Button1的Click事件,如下所示。参考以下实现代码 -

'  Add the following code snippet on top of Form1.vb

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim appXL As Excel.Application
        Dim wbXl As Excel.Workbook
        Dim shXL As Excel.Worksheet
        Dim raXL As Excel.Range
        ' Start Excel and get Application object.
        appXL = CreateObject("Excel.Application")
        appXL.Visible = True
        ' Add a new workbook.
        wbXl = appXL.Workbooks.Add
        shXL = wbXl.ActiveSheet
        ' Add table headers going cell by cell.
        shXL.Cells(1, 1).Value = "姓氏"
        shXL.Cells(1, 2).Value = "名字"
        shXL.Cells(1, 3).Value = "姓名"
        shXL.Cells(1, 4).Value = "专业"
        ' Format A1:D1 as bold, vertical alignment = center.
        With shXL.Range("A1", "D1")
            .Font.Bold = True
            .VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
        End With
        ' Create an array to set multiple values at once.
        Dim students(5, 2) As String
        students(0, 0) = "张"
        students(0, 1) = "雨寒"
        students(1, 0) = "牛"
        students(1, 1) = "乐蕊"
        students(2, 0) = "李"
        students(2, 1) = "采梦"
        students(3, 0) = "王"
        students(3, 1) = "妙晴"
        students(4, 0) = "杨"
        students(4, 1) = "思松"
        ' Fill A2:B6 with an array of values (First and Last Names).
        shXL.Range("A2", "B6").Value = students
        ' Fill C2:C6 with a relative formula (=A2 & " " & B2).
        raXL = shXL.Range("C2", "C6")
        raXL.Formula = "=A2 & """"& B2"
        ' Fill D2:D6 values.
        With shXL
            .Cells(2, 4).Value = "生物学"
            .Cells(3, 4).Value = "数学"
            .Cells(4, 4).Value = "物理"
            .Cells(5, 4).Value = "化学"
            .Cells(6, 4).Value = "地理"
        End With
        ' AutoFit columns A:D.
        raXL = shXL.Range("A1", "D1")
        raXL.EntireColumn.AutoFit()
        ' Make sure Excel is visible and give the user control
        ' of Excel's lifetime.
        appXL.Visible = True
        appXL.UserControl = True
        ' Release object references.
        raXL = Nothing
        shXL = Nothing
        wbXl = Nothing
        appXL.Quit()
        appXL = Nothing
        Exit Sub
Err_Handler:
        MsgBox(Err.Description, vbCritical, "Error: " & Err.Number)
    End Sub
End Class

当上面的代码执行并使用Microsoft Visual Studio工具栏上的“开始”按钮运行时,它将显示以下窗口:

点击导出成Excel文件 按钮应用程序将要求保存Excel工作簿,Excel表格内容如下 -


猿狮妹
2023-03-24
VB NetExcel 工作 在线教程
热门教程
1 VB.Net教程 VB.Net是Microsoft开发的一种简单的,现代的,面向对象的计算机编程语言,它将.NET Framework和公共语言运行时的强大功能与Visual Basic的优势相结合。本教程将介绍和讲解基本的VB.Net编程,并讲解各种有关VB.Net编程语言的基础、高级概念。
2 VB.Net Excel工作表 VB.Net支持Microsoft Excel 2010的COM对象模型和应用程序之间的互操作性。 要应用程序中使用这种互操作性,需要在Windows窗体应用程序中导入名称空间Microsoft
3 VB.Net程序结构 在学习VB.Net编程语言的基本构建块之前,先来看看一个最基本的VB.Net程序结构,以便可以在以后的章节中作为参考来理解程序代码块。 VB.Net Hello World示
4 VB.Net开发环境安装配置 在本章中,我们将讨论和学习可用于创建VB.Net应用程序的工具。 我们已经提到,VB.Net是.Net框架的一部分,用于编写.Net应用程序。 因此,在讨论用于运
5 VB.Net子程序(Sub) 在前面的章节中提到:子程序是不返回任何值的程序。到目前为止,在前面所有的例子中一直使用子过程Main。 在这些教程中,我们一直在编写控制台应
6 VB.Net第一个程序(Helloworld) 为了演示如何使用VS 2017创建一个简单的VB.net应用程序,这里将使用VS 2017来创建一个简单的“Hello,World”的控制台应用应用程序。这个例子将使用VB.NET
7 VB.Net数据类型 数据类型是指用于声明不同类型的变量或函数的扩展系统。变量的类型决定了它在存储器中占用多少空间以及如何解释存储的位模式。 VB.Net数据类型VB.
8 VB.Net类和对象 当定义一个类时,就定义了一个数据类型的蓝图(或模板)。这实际上并没有定义任何数据,但它确实定义了类名和含义,即该类的一个对象将包含哪些内
9 VB.Net正则表达式 正则表达式是可以与输入文本进行匹配的模式。.Net 框架提供了允许这种匹配的正则表达式引擎。模式由一个或多个字符文字,运算符或构造组成。 用
10 VB.Net基本控件 对象是通过使用工具箱控件在Visual Basic 窗体上创建的一种用户界面元素。 实际上,在Visual Basic中,窗体(Form)本身就是一个对象。 每个Visual Basic 控件都
  • Copyright © 2021 猿狮院, All rights reserved.