使用adonet读取excel的2个函数

上一篇 / 下一篇  2011-10-18 14:46:51 / 个人分类:VBScript

'获取某单元格数据51Testing软件测试网 V1D'p\4N

Function get_excel_value(rowindex,cellindex)51Testing软件测试网0T+]%I0wn:r^ N

On Error Resume NEXT

}K f0WM0

Dim myarray()

m5VqKe"e"]\4E0

Set con=createobject("adodb.connection")

4D2I*GI,yC8A0

 conset="DSN=test;DBQ=D:\test\test.xls;DefaultDir=D:\test;DriverId=790;FIL=excel 8.0;MaxBufferSize=2048;PageTimeout=5;"51Testing软件测试网/tQ!e(s*?5N n0j

con.open conset

xY5wU5F#q0

Set rs=createobject("adodb.recordset")51Testing软件测试网f4C?0}V`%e

sql="select count(*) from [Sheet1$]"51Testing软件测试网t q,^'R'G*?'Y

rs.open sql,con

I2f a8K+@\*zZ&\0

rows=rs.Fields(0)51Testing软件测试网 K#IQF1?f,aj

  rs.Close

%[.J*c6gd'sg1BBe0

 sql="select * from [Sheet1$]"

`%p0M_ z#?0

  rs.open sql,con

jn.ua e!p3}Q0

   If rowindex >rows or cellindex>rs.Fields.Count Then

Ry)AA![0

 

:lz6w0P(f G0

     ‘MsgBox "该单元格无数据"51Testing软件测试网&rm?Q/rw/|7R\(To-y

     get_excel_value =-151Testing软件测试网&r:F9M1W/Fh:o4p~

     Exit function51Testing软件测试网1JU:Dc$| Z{

   Else

k&~wO;f/x bdx$@0

     rs.Move rowindex-1

tA3LL6B,h]0

   get_excel_value =rs.Fields(cellindex-1).Value51Testing软件测试网/d @$jC!u8Cw

 51Testing软件测试网N_/_~1r:D?

   End If

o J|+[})j1u}A+B;Z0

 con.Close51Testing软件测试网bv?^.i'On0Z

 Set con=Nothing

H d)F0I'w n0

On Error Goto 051Testing软件测试网m9X)m4Q-q

 

4d~;sE[\0]gF0

End Function

0{1O R4mm0

 51Testing软件测试网m:A@`F&oD C0Q

'get_excel_cell:获取excel某列的数据,返回数组

4yc/r-};}0n0

'cellindex:获取列的序号或列名

NZ})f$ka3i8J4K0c0

Function get_excel_cell(cellindex)51Testing软件测试网\&Kb6P N D:u*M1N

On Error Resume NEXT51Testing软件测试网]$?P;m}%_

Dim myarray()

X5x~ LI!}-R0

Set con=createobject("adodb.connection")

4L+~#hw sy7j0

conset="DSN=test;DBQ=D:\test\test.xls;DefaultDir=D:\test;DriverId=790;FIL=excel 8.0;MaxBufferSize=2048;PageTimeout=5;"51Testing软件测试网5e g^.m6['_ZN^R\1a

con.open conset51Testing软件测试网 J&h.OJ0_,i,p

Set rs=createobject("adodb.recordset")

F2ki&^ir a7p_0

sql="Select count(*) from [Sheet1$]"51Testing软件测试网2z[S`,ZY&?#M

rs.Open sql,con

4f5O;P1X9r%P G0

'记录的总行数51Testing软件测试网-^`/oZ#cZ ^

rows=rs.Fields(0).Value

Ik#X$W(S3m#nkd0

rs.Close51Testing软件测试网'|!H9L*w4d(yc@

 sql="select * from [Sheet1$]"51Testing软件测试网%A:WaTS]O n

  rs.open sql,con

1a9w8w!C Ro0

  ReDim myarray(rows-1)51Testing软件测试网U B3Z I/}S1M

  For i=0 To rows-1

)k[v/u*P0

  'icellindex列的值赋值给数组51Testing软件测试网/y/@c}6mI

  myarray(i)=rs.Fields(cellindex).Value

z1Of:J P#HQ&E0W0

  rs.MoveNext51Testing软件测试网H ` ZI NO4_

  next51Testing软件测试网t;i w B3Vv

 get_excel_cell=myarray51Testing软件测试网so d"e%`o

  con.Close51Testing软件测试网Te$J]r

 Set con=Nothing51Testing软件测试网:?lT7un

On Error Goto 051Testing软件测试网#e*BR*p-wA-e

End Function51Testing软件测试网AYUP;k


TAG: vbs VBS 函数 adonet

 

评分:0

我来说两句

Open Toolbar