Dim x As Integer
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim AppExcel As New Excel.Application
Set AppExcel = CreateObject("Excel.Application")'创建Excel对象
Set xlBook = AppExcel.Workbooks.Open("D:\数据表.xls") '打开工作簿
If AppExcel Is Nothing Then Exit Sub'错误则退出
For x = 1 To AppExcel.Sheets.Count '找到对应的工作表
If AppExcel.Sheets(x).Name ="工作表名" Then
Set xlSheet = AppExcel.Sheets(x)
Exit For
End if
Next
'下面为单元格的定义
xlSheet.Cells([RowIndex],[ColumnIndex]).Value'单元格(行 , 列)
xlSheet.Range("B2:B2").Value'单元格选择
Set xlSheet = Nothing
Set xlBook = Nothing
Set AppExcel = Nothing
End Sub
'VB6.0必需先引用Microsoft Excel 11.0 Object Library
VB6.0连接EXCEL设置方法1. 工程 引入 Microsoft Excel 12.0 Object Library ,这里的12.0 可能会因为office版本不同而不同。
2. 代码程序通用部分定义
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
3.打开EXCEL文件
CommonDialog1.ShowOpen '选择文件路径
Text1.Text = CommonDialog1.FileName '将路径给TEXT1.TEXT
filePath = Text1.Text
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
xlApp.Visible = ture '设置EXCEL对象可见
Set xlBook = xlApp.Workbooks.Open(filePath) '打开EXCEL工作簿
Set xlSheet = xlBook.Worksheets(1) '设置活动工作表sheet,可以按名字可以可以按序号
Text2(0).Text = xlSheet.Cells(6, 1) '将A6内容填入TEXT2(0).TEXT
Text2(1).Text = xlSheet.Cells(7, 1) 'A7
Text2(2).Text = xlSheet.Cells(8, 1) 'A8
Text2(3).Text = xlSheet.Cells(9, 1)
Text2(4).Text = xlSheet.Cells(10, 1)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)