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

前端之家收集整理的这篇文章主要介绍了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()

猜你在找的VB相关文章