gpt4 book ai didi

excel - 循环读取excel数据

转载 作者:行者123 更新时间:2023-12-04 21:10:41 25 4
gpt4 key购买 nike

我需要将读取的数据从 excel 循环到 vb.net,当我到达最后一行/列“!@#$%^&*()”时,excel 数据将停止读取。我怎样才能做到这一点?

Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim xRange As Excel.Range
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub cmdGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGenerate.Click
'Dim row As String
Dim empty_cell_ctr As Integer = 0 '5
Dim end_of_xlsheet As Boolean = False
Dim sRow As Integer 'start row
Dim col_end As Integer 'col W
'
'loading excel(open and read)
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Open("c:\sample.xls")
xlWorkSheet = xlWorkBook.Worksheets("Timesheet")
xlApp.Visible = True

While Not end_of_xlsheet

If sRow = "'!@#$%^&*()_+" Then
xRange = xRange.Cells(sRow, col_end)
end_of_xlsheet = False 'end of sheet
Continue While
End If

sRow += 1

End While

MessageBox.Show(sRow)
End Sub
End Class

最佳答案

您似乎想多了,您可以通过访问 获取电子表格中的所有数据使用范围属性,然后通过访问 将其加载到二维数组变量中。值2 该对象的属性如下:

Dim application = New Excel.Application()
Dim workbook As Excel.Workbook = application.Workbooks.Open("C:\aaa\bbb.xlsx")
Dim worksheet As Excel.Worksheet = workbook.Sheets(1)

Dim usedRange = worksheet.UsedRange
Dim usedRangeAs2DArray As Object(,) = usedRange.Value2

workbook.Save()
workbook.Close()
application.Quit()

Marshal.ReleaseComObject(application)

关于excel - 循环读取excel数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13676259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com