课程表

VB.Net基本教程

VB.Net高级教程

工具箱
速查手册

VB.Net - Excel工作表

当前位置:免费教程 » 程序设计 » VB.Net

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选项卡
  • 点击OK。


双击代码窗口并填充Button1的Click事件,如下所示。

  1. ' Add the following code snippet on top of Form1.vb
  2. Imports Excel = Microsoft.Office.Interop.Excel
  3. Public Class Form1
  4. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  5. Dim appXL As Excel.Application
  6. Dim wbXl As Excel.Workbook
  7. Dim shXL As Excel.Worksheet
  8. Dim raXL As Excel.Range
  9. ' Start Excel and get Application object.
  10. appXL = CreateObject("Excel.Application")
  11. appXL.Visible = True
  12. ' Add a new workbook.
  13. wbXl = appXL.Workbooks.Add
  14. shXL = wbXl.ActiveSheet
  15. ' Add table headers going cell by cell.
  16. shXL.Cells(1, 1).Value = "First Name"
  17. shXL.Cells(1, 2).Value = "Last Name"
  18. shXL.Cells(1, 3).Value = "Full Name"
  19. shXL.Cells(1, 4).Value = "Specialization"
  20. ' Format A1:D1 as bold, vertical alignment = center.
  21. With shXL.Range("A1", "D1")
  22. .Font.Bold = True
  23. .VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
  24. End With
  25. ' Create an array to set multiple values at once.
  26. Dim students(5, 2) As String
  27. students(0, 0) = "Zara"
  28. students(0, 1) = "Ali"
  29. students(1, 0) = "Nuha"
  30. students(1, 1) = "Ali"
  31. students(2, 0) = "Arilia"
  32. students(2, 1) = "RamKumar"
  33. students(3, 0) = "Rita"
  34. students(3, 1) = "Jones"
  35. students(4, 0) = "Umme"
  36. students(4, 1) = "Ayman"
  37. ' Fill A2:B6 with an array of values (First and Last Names).
  38. shXL.Range("A2", "B6").Value = students
  39. ' Fill C2:C6 with a relative formula (=A2 & " " & B2).
  40. raXL = shXL.Range("C2", "C6")
  41. raXL.Formula = "=A2 & "" "" & B2"
  42. ' Fill D2:D6 values.
  43. With shXL
  44. .Cells(2, 4).Value = "Biology"
  45. .Cells(3, 4).Value = "Mathmematics"
  46. .Cells(4, 4).Value = "Physics"
  47. .Cells(5, 4).Value = "Mathmematics"
  48. .Cells(6, 4).Value = "Arabic"
  49. End With
  50. ' AutoFit columns A:D.
  51. raXL = shXL.Range("A1", "D1")
  52. raXL.EntireColumn.AutoFit()
  53. ' Make sure Excel is visible and give the user control
  54. ' of Excel's lifetime.
  55. appXL.Visible = True
  56. appXL.UserControl = True
  57. ' Release object references.
  58. raXL = Nothing
  59. shXL = Nothing
  60. wbXl = Nothing
  61. appXL.Quit()
  62. appXL = Nothing
  63. Exit Sub
  64. Err_Handler:
  65. MsgBox(Err.Description, vbCritical, "Error: " & Err.Number)
  66. End Sub
  67. End Class


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


VB.Net Excel的实例


单击按钮将显示以下excel表。 将要求您保存工作簿。


VB.Net的Excel表格结果
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号