从excel添加项目到列表框 [英] Adding items to listbox from excel

查看:201
本文介绍了从excel添加项目到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在excel文件中有一列包含100行我正在尝试使用按钮将此列导入列表框



问题是只有48行是从excel列导入。



为什么列中的所有行都不会导入?



这里是我的代码(vb.net表单)



I have a column in excel file contain 100 row I'm trying to import this column into listbox using a button

The problem is that only 48 row is importing from the excel column .

why not all rows inside the column are imported?

here is my code (vb.net form)

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim oExcel As Object = CreateObject("Excel.Application")
    Dim oBook As Object = oExcel.Workbooks.Open("C:\Users\User\Desktop\1.xlsx")
    Dim oSheet As Object = oBook.Worksheets(1)
    Dim i As Integer
    Dim cell As String
For i = 0 To AscW(ListBox1.Items.Count.ToString()(i = i + 1)) - 1
        'set cell name, e.g. A1, A2, etc
        cell = "B" & Convert.ToString(i + 1)
        ' get cell data from Excel
        cell = oSheet.Range(cell).Value
        If cell = "" Then
            Exit For
        Else
            ListBox5.Items.Add(cell)
        End If
    Next
    oExcel.Quit()
End Sub

推荐答案

如果您查看我之前发布的解决方案的编辑从excel导入到列表框 [ ^ ]你会注意到我已经解释了问题是什么



重申一下,for循环
If you look at the edit to my solution to your previous post importing from excel to listbox[^] you will notice that I have already explained what the problem is

To reiterate, the for loop
For i = 0 To AscW(ListBox1.Items.Count.ToString()(i = i + 1)) - 1

仅查看ite列表框中已经的ms



回到上一篇文章,查看我提供的WHILE循环。

is only looking at items that are already in the ListBox

Go back to your previous post and look at the WHILE loop I provided.


这篇关于从excel添加项目到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆