关于VB读取excel内数据的问题

来源:百度知道 编辑:UC知道 时间:2024/09/22 01:51:51
如果运用 VB 读取 f:\1.XLS 内 sheet1表内 B2的数据 放到 text1.text 内

求详细代码

在“工程”的“引用”中选择“microsoft excel 11.0”,"microsoft excel 11.0"看版本而定
Dim xlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet

Private Sub Command1_Click()
On Error Resume Next
'后台进程运行excel程序,并得到该工作簿
Set xlBook = xlApp.Workbooks.Open("f:\1.xls")
xlApp.Visible = False
'获得该工作簿的“sheet1”表
Set xlSheet = xlBook.Sheets("sheet1")

xlSheet.Select
'抽取b2格子的数据并赋值给text1.text
text1.Text = xlSheet.Range("b2")
'释放资源,顺序到过来的,表》》簿》》应用程序
Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
End Sub

'工程中添加对microsoft excel 11.0对象库的引用

'为应用程序工作簿工作表对象声明对象变量
dim xlApp as excel.application
dim xlbook as excel.workbook
dim xlsheet as excel.worksheet

'将对象引用赋值给变量,用ADD方法创建新的工作簿和工作表对象
s