VB中如何读取Excel中的数据
publicsubform_load()dimn_shuziasintegerDimxlappAsExcel.ApplicationDimxlbookAsExcel.Wo...
public sub form_load()
dim n_shuzi as integer
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim shuzu(3) As Integer
Dim n_shuzi As Integer
Public f_row As Integer
Public m_row As Integer
xlapp = New Excel.Application
xlbook = xlapp.Workbooks.Open(“D:\123.clsx”)
xlapp.Visible = True
xlsheet = xlbook.Worksheets("Sheet1")
n_shuzi = xlsheet.Cells(1, 1)
For f_row = 0 To 3
shuzu(f_row) = xlsheet.Cells(f_row + 1, 1)
Next
如上面所示程序 从123.xlsx中读取数据 写入数组中 把cells(1,1)的数值赋给参数n_shuzi,即就是n_shuzi=xlsheet.cells(1,1)到这快就会出现
InvalidCastException was unhandled 这是什么错误?请大师帮忙解决 展开
dim n_shuzi as integer
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim shuzu(3) As Integer
Dim n_shuzi As Integer
Public f_row As Integer
Public m_row As Integer
xlapp = New Excel.Application
xlbook = xlapp.Workbooks.Open(“D:\123.clsx”)
xlapp.Visible = True
xlsheet = xlbook.Worksheets("Sheet1")
n_shuzi = xlsheet.Cells(1, 1)
For f_row = 0 To 3
shuzu(f_row) = xlsheet.Cells(f_row + 1, 1)
Next
如上面所示程序 从123.xlsx中读取数据 写入数组中 把cells(1,1)的数值赋给参数n_shuzi,即就是n_shuzi=xlsheet.cells(1,1)到这快就会出现
InvalidCastException was unhandled 这是什么错误?请大师帮忙解决 展开
3个回答
展开全部
xlbook = xlapp.Workbooks.Open(“D:\123.clsx”)和你说明中的123.xlsx文件名不同,检查一下,确保文件确实存在。
InvalidCastException 是 无效类型转换异常,用n_shuzi = xlsheet.Cells(1, 1).value试试。
InvalidCastException 是 无效类型转换异常,用n_shuzi = xlsheet.Cells(1, 1).value试试。
追问
大师 问题解决了 谢谢啊 还是使用最后一句加上.value 嘿嘿 3Q
追答
不客气,能得到正解就好。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Public f_row As Integer
Public m_row As Integer
Private Sub Command1_Click()
Dim n_shuzi As Integer
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim shuzu(3) As Integer
Set xlapp = New Excel.Application
Set xlbook = xlapp.Workbooks.Open("c:\1.xls")
xlapp.Visible = True
Set xlsheet = xlbook.Worksheets("Sheet1")
n_shuzi = Val(xlsheet.Cells(1, 1))
For f_row = 0 To 3
shuzu(f_row) = Val(xlsheet.Cells(f_row + 1, 1))
Next
End Sub
追问
改成上面你说的之后 n_shuzi=val(xlsheet.cells(1,1))还是报Argument 'Expression' cannot be converted to type 'Range'.这个错误 我把改成n_shuzi=val(“xlsheet.cells(1,1)")后不报错了 但是n_shuzi就是没有值 我Excel表格中给A1的值是2,大师为什么?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
shuzu(f_row) = val(xlsheet.Cells(f_row + 1, 1))
追问
大师 改成你所写的之后 错误又报 ArgumentException was unhandled (argument 'Expression' cannot be converted to type 'Range')
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |