Visual Basic 使用 Excel Sheet

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

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


创建 Excel 应用程序

让我们首先在 Microsoft Visual Studio 中按照以下步骤创建窗口窗体应用程序:File → New Project → Windows Forms Applications

最终, 选择 OK, Microsoft Visual Studio 创建您的项目并显示以下 Form1

向窗体插入一个按钮 Button1

将对 Microsoft Excel 对象库的引用添加到项目中。操作如下:

  • 在 Project 目录中选择 Add Reference 添加引用。

  • 在 COM 选项下, 找到 Microsoft Excel Object Library 然后选择它。

  • 点击 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 工具栏上的 开始 按钮执行并运行上述代码时,它将显示以下窗口:

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