竭诚为您提供优质文档/双击可除
vb,读取excel表格数据并在listview
控件中显示
篇一:Vb用listView读取excel实例
Vb用listView读取excel实例
第一步:用excel创建成绩表,如下图示例
第二步:打开Vb6创建一工程、标准窗口
第三步:引用excel,在菜单栏,工程——引用,如下图
第四步:在工具栏加载控件microsoftwindowscommoncontrols6.0(sp6),如下图第五步:在标准窗口加入listview控件,控件属性设置如下图
在“列首”标签添加字段名,如下图
第五步:编写代码
dimxlappasexcel.application
privatesubForm_load()
setxlapp=createobject("excel.application")
xlapp.Visible=False
xlapp.workbooks.openapp.path查找列中所有的单元格setlitem=listView1.listitems.add()
litem.text=xlapp.Range("a"checkifemptydataRow
blnnullRow=true
Forinti=1to10
iftrim$(objimportsheet.cells(intcounti,inti).Value) ""then
blnnullRow=False
endif
nextinti
若不是空行,则进行读取动作,否则继续向后遍历excel 中的行
ifblnnullRow=Falsethen
获取单元格中的数据,做有效性check,并将合法数据创建为实体存入对象数组中
objimportsheet.cells(intcounti,1).Value
……
endif
nextintcounti
5、退出excel进程,并关闭excel相关操作对象
objexcelFile.quit
setobjworkbook=nothing
setobjimportsheet=nothing
setobjexcelFile=nothing
篇三:vb代码读取excel数据源
取得excel文件所有内容:
addmicrosoftadofrist.
privatesubcommand2_click()
dimadoconnectionasnewadodb.connection
dimadoRecordsetasnewadodb.Recordset
commondialog1.showopen
oledb+odbcdriver方式:
adoconnection.open"dataprovider=msdasql.1;driver=mi crosoftexceldriver(*.xls);dbq=e:\temp\book2.xls"
microsoft.jet.oledb.4.0方式,(建)
adoconnection.open"provider=microsoft.jet.oledb.4.0 ;persist
securityinfo=False;datasource="extendedproperties=e
xcel8.0;hdR=yes"
adoRecordset.open"select*from[sheet1$]",adoconnecti on,adopenkeyset,adlockoptimistic
debug.printadoRecordset.Recordcount
dimiasinteger
dountiladoRecordset.eoF
Fori=0toadoRecordset.Fields.count-1
debug.printadoRecordset.Fields.item(0).name
debug.printadoRecordset.Fields.item(0).Value
nexti
adoRecordset.movenext
loop
endsub
但还不知道如何取得sheet名,不好意思,那位老鸟知道请告知,谢谢sheet名用excel.application对象dimxlappasexcel.application
dimxlbookasexcel.workbook
setxlapp=newexcel.application
setxlbook=xlapp.workbooks.open("c;\123.xls"))
Fori=1tointsheetsum
strtemp=xlbook.worksheets(i).name。