vb.net 读取EXCEL文件中的数据

        Dim xlApp As Microsoft.Office.Interop.Excel.Application
       
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
       
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet

       
Dim this As Boolean = True

        xlApp
= New Microsoft.Office.Interop.Excel.Application
       
If Not FileIO.FileSystem.FileExists("E:/001.xls") Then
           
MsgBox("文件不存在!")
           
Return
       
End If

        xlBook
= xlApp.Workbooks.Open("E:/001.xls")
        xlSheet
= xlBook.Worksheets(1) '用第一个sheet
        xlApp.ActiveWindow.DisplayGridlines = False

       
Dim i As Integer = 0
       
Dim j As Integer = 0
       
Dim strA As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
       
Dim str As String = ""

       
str = TextBox1.Text.ToUpper
        i
= str.IndexOf(":")

       
Dim strF As String = ""
       
Dim strB As String = ""

        strF
= str.Substring(0, i)
        strB
= str.Substring(i + 1, str.Length - i - 1)

       
For i = 0 To strF.Length - 1
           
If Char.IsNumber(strF.Chars(i)) Then Exit For
       
Next
       
Dim strFA As String = strF.Substring(0, i)
       
Dim strFi As Integer = Integer.Parse(strF.Substring(i, strF.Length - 1))

       
For i = 0 To strB.Length - 1
           
If Char.IsNumber(strB.Chars(i)) Then Exit For
       
Next
       
Dim strBA As String = strB.Substring(0, i)
       
Dim strBi As Integer = Integer.Parse(strB.Substring(i, strB.Length - 1))

       
Dim ddd As String
       
Dim dx As String
       
With xlSheet
           
For j = strFi To strBi
                ddd
= ""
               
For i = Integer.Parse(strA.IndexOf(strFA)) + 1 To Integer.Parse(strA.IndexOf(strBA)) + 1
                   
str = .Range(strA.Chars(i - 1) & String.Concat(j)).Value
                   
Dim ch As Char = " "
                   
If str = Nothing Then
                        dx
= New String(ch, 20)    '相当于设置每列数据的宽度=20
                    Else
                        dx
= New String(ch, (20 - str.Length - 1)) '相当于设置每列数据的宽度=20
                    End If
                   
str = str & dx
                    ddd
= ddd & str

               
Next ListBox1.Items.Add(ddd) Next
       
End With

        xlBook.Save()

        xlBook.Close()
        xlApp.Quit()

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值