JSRUN 用代码说话

Excel工作表

编辑教程

VB.Net-Excel工作表

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

要在应用程序中使用此互操作性,您需要在Windows附加应用程序中引入命名空间Microsoft.Office.Interop.Excel。

从VB.Net创建一个Excel应用程序

让我们从Microsoft Visual Studio中的以下步骤开始创建副本表单应用程序:文件->新建项目-> Windows合并应用程序

最后,选择确定,Microsoft Visual Studio创建您的项目并显示Form1。

插入Button控件Button1。

向项目中添加对Microsoft Excel对象库的引用。进行以下操作:

  • 从项目菜单中选择添加引用。

  • 添加引用

  • 在COM选项卡上,找到Microsoft Excel对象库,然后依次选择。

  • COM选项卡

  • 单击确定。

双击代码窗口并填充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 = "First Name"
      shXL.Cells(1, 2).Value = "Last Name"
      shXL.Cells(1, 3).Value = "Full Name"
      shXL.Cells(1, 4).Value = "Specialization"
      ' 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) = "Zara"
      students(0, 1) = "Ali"
      students(1, 0) = "Nuha"
      students(1, 1) = "Ali"
      students(2, 0) = "Arilia"
      students(2, 1) = "RamKumar"
      students(3, 0) = "Rita"
      students(3, 1) = "Jones"
      students(4, 0) = "Umme"
      students(4, 1) = "Ayman"
      ' 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 = "Biology"
          .Cells(3, 4).Value = "Mathmematics"
          .Cells(4, 4).Value = "Physics"
          .Cells(5, 4).Value = "Mathmematics"
          .Cells(6, 4).Value = "Arabic"
      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
JSRUN闪电教程系统是国内最先开创的教程维护系统, 所有工程师都可以参与共同维护的闪电教程,让知识的积累变得统一完整、自成体系。 大家可以一起参与进共编,让零散的知识点帮助更多的人。
X
支付宝
9.99
无法付款,请点击这里
金额: 0
备注:
转账时请填写正确的金额和备注信息,到账由人工处理,可能需要较长时间
如有疑问请联系QQ:565830900
正在生成二维码, 此过程可能需要15秒钟