您好我有关于扫描工作簿的问题,有两个工作表需要扫描.. 我做的最后一件事是我扫描第一张工作表,但第二张工作表仍未包括在内。 这是我的示例代码:
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp = New Excel.ApplicationClass
reportpath = filename
xlWorkBook = xlApp.Workbooks.Open(reportpath)
xlWorkSheet = xlWorkBook.Worksheets("Sheet1")
xlWorkSheet = xlWorkBook.Worksheets("Sheet2")
答案 0 :(得分:0)
将您的代码更改为:
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbookv
Dim xlWorkSheet1 As Excel.Worksheet
Dim xlWorkSheet2 As Excel.Worksheet
xlApp = New Excel.ApplicationClass
reportpath = filename
xlWorkBook = xlApp.Workbooks.Open(reportpath)
xlWorkSheet1 = xlWorkBook.Worksheets("Sheet1")
xlWorkSheet2 = xlWorkBook.Worksheets("Sheet2")
For i = 1 To 10
MsgBox(xlWorkSheet1.Range("A" & i))
Next i
使用此代码,您'扫描'A
的{{1}}列的前10行。
如果您要“扫描”Sheet1
xlWorkSheet2
执行相同的操作
答案 1 :(得分:0)
检查此tutorial以从excel获取数据,并在DataTable中处理,这种方式比以下更快:
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook * ... *
或检查我的方式:
Dim _con As OleDbConnection
Dim _cmd As New OleDbCommand
Dim DtSet As DataSet
Dim _tabExtrato as new DataTable
Dim _adaptCommand As OleDbDataAdapter
Private Sub _readXLS(ByVal TXT As TextBox, ByVal _linkFILE As String, ByVal _folha As String, ByVal DGV As DataGridView)
_con = New OleDbConnection _
("provider=Microsoft.Jet.OLEDB.4.0; Data Source='" & _linkFILE & "'; Extended Properties=Excel 8.0;")
Dim trimTxt As String = "SELECT * FROM "
'_folha = the sheet
Dim _sqlQuery As String = " " & trimTxt & " [" & _folha & "$] "
_adaptCommand = New OleDbDataAdapter(_sqlQuery, _con)
_adaptCommand.TableMappings.Add("Table", "DSTable")
DtSet = New DataSet
'_adaptCommand.Fill(DtSet)
_adaptCommand.Fill(_tabExtrato)
DGV.DataSource = _tabExtrato
_con.Close()
End Sub