在VB.NET怎样动态读取EXCEL单元格的值?
各位高手帮忙看一下
我想用VB.NET动态读取每个单元格的值,在LISTBOX显示出来,不知怎样用变量,请各位帮帮忙?
Dim objExcelFile As Excel.Application
Dim objWorkBook As Excel.Workbook
Dim objImportSheet As Excel.Worksheet
objExcelFile = New Excel.Application
objWorkBook = objExcelFile.Workbooks.Open(strFileName)
objImportSheet = objWorkBook.Sheets.items(1)
listbox.items.add()
如下EXCEL表.
dept itemcede
10 000001
10 123458
10 124478
12 346798
12 789425
__________________________________________________________________________
用Range对象!!!
__________________________________________________________________________
用Cells对象也可以!
例如:
listbox.items.add(objImportSheet.Range( "B1 "))
listbox.items.add(objImportSheet.Rows(1).Cells(2).Text)
__________________________________________________________________________
这样也不行,假如我的EXCEL表有300的行这样的数据,不是要写300行。而且每次处理的行数都不一样。我就是想实现能自动判断单元格的行值有多少行,然后在LISTBOX中显示出来
__________________________________________________________________________
objImportSheet.UsedRange.Rows.Count 就是表的最大行数
__________________________________________________________________________
要不先把EXCEL里的数据读到dataset里再listbox.items.add(dataset.table(0).row(i)( "字段名 "))
__________________________________________________________________________
也可用做个循环.来读取(找一个必有列eg:第一列)
dim i as int64
do until Trim(objImportSheet.Cells(i, 1).value) = " "
..............
''赋值
...........
loop
__________________________________________________________________________
private ds as new dataset()
Private sub Form1_Load()
dim cn as new oledb.oledbconnection()
dim da as new oledb.oledbdataadapter()
dim c as new oledb.oledbcommand()
'' '' '' '' ''接续设定HDR 有标题的时候Yes 否则No
cn.connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/sampl.xls; " & _
"Extended Properties= " "Excel 8.0;HDR=Yes " " "
with c
.commandtext= "Select * From [Sheet1$] "
.connection=cn
end with
da.selectCommand=c
da.fill(ds)
TextBox1.databindings.add( "Text ",ds.tables(0), "dept ")
TextBox2.databindings.add( "Text ",ds.tables(0), "itemcede ")
end sub
剩下的自己研究一下吧!
__________________________________________________________________________
还是不行
__________________________________________________________________________
前面不是有人说了如何获取最大行数吗?
for n=1 to exSheet.UsedRange.Rows.Count
itm=new listviewitem
itm.text=exSheet.Cells(n,1).value
listview.add itm
next
__________________________________________________________________________
各位高手帮忙看一下
我想用VB.NET动态读取每个单元格的值,在LISTBOX显示出来,不知怎样用变量,请各位帮帮忙?
Dim objExcelFile As Excel.Application
Dim objWorkBook As Excel.Workbook
Dim objImportSheet As Excel.Worksheet
objExcelFile = New Excel.Application
objWorkBook = objExcelFile.Workbooks.Open(strFileName)
objImportSheet = objWorkBook.Sheets.items(1)
listbox.items.add()
如下EXCEL表.
dept itemcede
10 000001
10 123458
10 124478
12 346798
12 789425
__________________________________________________________________________
用Range对象!!!
__________________________________________________________________________
用Cells对象也可以!
例如:
listbox.items.add(objImportSheet.Range( "B1 "))
listbox.items.add(objImportSheet.Rows(1).Cells(2).Text)
__________________________________________________________________________
这样也不行,假如我的EXCEL表有300的行这样的数据,不是要写300行。而且每次处理的行数都不一样。我就是想实现能自动判断单元格的行值有多少行,然后在LISTBOX中显示出来
__________________________________________________________________________
objImportSheet.UsedRange.Rows.Count 就是表的最大行数
__________________________________________________________________________
要不先把EXCEL里的数据读到dataset里再listbox.items.add(dataset.table(0).row(i)( "字段名 "))
__________________________________________________________________________
也可用做个循环.来读取(找一个必有列eg:第一列)
dim i as int64
do until Trim(objImportSheet.Cells(i, 1).value) = " "
..............
''赋值
...........
loop
__________________________________________________________________________
private ds as new dataset()
Private sub Form1_Load()
dim cn as new oledb.oledbconnection()
dim da as new oledb.oledbdataadapter()
dim c as new oledb.oledbcommand()
'' '' '' '' ''接续设定HDR 有标题的时候Yes 否则No
cn.connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/sampl.xls; " & _
"Extended Properties= " "Excel 8.0;HDR=Yes " " "
with c
.commandtext= "Select * From [Sheet1$] "
.connection=cn
end with
da.selectCommand=c
da.fill(ds)
TextBox1.databindings.add( "Text ",ds.tables(0), "dept ")
TextBox2.databindings.add( "Text ",ds.tables(0), "itemcede ")
end sub
剩下的自己研究一下吧!
__________________________________________________________________________
还是不行
__________________________________________________________________________
前面不是有人说了如何获取最大行数吗?
for n=1 to exSheet.UsedRange.Rows.Count
itm=new listviewitem
itm.text=exSheet.Cells(n,1).value
listview.add itm
next
__________________________________________________________________________