$exception {"Public member 'Value' on type 'Range' not found."} System.MissingMemberException
- 代码
- 准备运行
- 问题解决
- 1. 下载别的版本的.net框架
- 2. 安装
- 3. 运行
代码
Imports Excel = Microsoft.office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'声明Excel对象
Dim appXL As Excel.Application
'声明工作簿对象
Dim wbXL As Excel.Workbook
'声明工作表对象
Dim shXL As Excel.Worksheet
Dim raXL As Excel.Range
appXL = CreateObject("Excel.Application")
appXL.Visible = True
'添加一个工作表
wbXL = appXL.Workbooks.Add
shXL = wbXL.ActiveSheet
'给工作表添加表头
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"
'设置A1:D1加粗,垂直居中
With shXL.Range("A1", "D1")
.Font.Bold = True
.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
End With
'创建一个数组作为表格中的值去填充
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"
'把上述学生数组的姓和名填入表格A2:B6
shXL.Range("A2", "B6").Value = students
'用类似的格式填充C2:C6
raXL = shXL.Range("C2", "C6")
raXL.Formula = "=A2 & "" "" & B2"
'填充D2:D6的值
With shXL
.Cells(2, 4).Value = "Biology"
.Cells(3, 4).Value = "Mathmematics"
.Cells(4, 4).Value = "Pyhsics"
.Cells(5, 4).Value = "Mathmematics"
.Cells(6, 4).Value = "Arabic"
End With
'自动调整列A:D
raXL = shXL.Range("A1", "D1")
raXL.EntireColumn.AutoFit()
'确保表格是可见的,且能被用户控制
appXL.Visible = True
appXL.UserControl = True
'相关的对象引用
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
窗体很简单,就是一个按钮触发
准备运行
报错,发现是.value发现没有找到,搜了一顿之后发现,可能是.net框架的问题,因为我就在开始建项目的时候,.net框架5.0后面括号里面显示不支持。当时也不知道那是个啥,直接就下一步了,没想到这里挖坑了。
问题解决
1. 下载别的版本的.net框架
我下载了一个4.8.1版本的和6.0版本的,其实下载一个就好了。
2. 安装
等待安装之后,我本来想直接在项目——右键属性——应用程序——目标框架,里面去选择我刚刚安装的,但是我的里面就只有5.0和3.1,还都显示不支持(下图是我安装之后重新新建了一个才出来的)
3. 运行
运行结果很成功