帮我看看 VB读取EXCEL的这段代码有错没

来源:百度知道 编辑:UC知道 时间:2024/09/28 17:53:42
dim n as long
Dim oConn As New ADODB.Connection
Dim oRS As New ADODB.Recordset
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Book1.xls;" & _
"Extended Properties=""Excel 8.0;"""
oRS.Open "Select * from [Sheet1$]", oConn, adOpenStatic
n = 1
do until oRS.EOF
msgbox "第" & n & " 条记录,第一列:" & oRS.fields(1) & _
";第二列:" & oRS.fields(2)
n = n + 1
oRS.Movenext
Loop
oRS.Close
oConn.Close

使用ADO连接 这样有错没 ~~
我准备修改下 用这个读取EXCEL中的 固定列的值~~

你的代码可以改成这样:
Dim n As Long
Set ExcelApp = CreateObject("Excel.Application") '创建EXCEL对象
Set ExcelBook = ExcelApp.Workbooks.open("D:\Documents and Settings\chaoliu\桌面\2月份考勤bbb.xls")
Set ExcelSheet = ExcelBook.Worksheets(1)

n = 1
Do
MsgBox "第" & n & " 条记录,第一列:" & ExcelSheet.Range("A" & n).Value & _
";第二列:" & ExcelSheet.Range("B" & n).Value
n = n + 1
Loop Until ExcelSheet.Range("A" & n).Value = ""
Set ExcelSheet = Nothing
Set ExcelBook = Nothing
Set ExcelApp = Nothing

使用说明:
*****************************************

VB读Excel文件,创建xls表,并写入内容
Set ExcelApp = CreateObject("Excel.Application") '创建EXCEL对象
Set ExcelBook = ExcelApp.Workbooks.Add
Set ExcelSheet = ExcelBook.Worksheets(1) '添加工作页
ExcelSheet.Activate &