0

嗨,我在扫描我的工作簿时遇到问题,需要扫描两个工作表。我做的最后一件事是扫描第一个工作表,但第二个工作表仍未包括在内。这是我的示例代码:

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")
4

2 回答 2

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

通过使用此代码,您正在“扫描”的前 10 行列ASheet1

xlWorkSheet2如果您想“扫描”,请执行相同操作Sheet2

于 2013-02-05T07:40:00.107 回答
0

查看本教程以从 excel 中获取数据,并在 DataTable 中处理,这种方式比 with 更快:

将 xlApp 调暗为 Excel.Application

将 xlWorkBook 调暗为 Excel.Workbook *...*

或检查我的方式:

将 _con 调暗为 OleDbConnection

Dim _cmd As New OleDbCommand

    Dim DtSet As DataSet

    Dim _tabExtrato as new DataTable

    Dim _adaptCommand As OleDbDataAdapter

Private Sub _readXLS(ByVal TXT 作为文本框,ByVal _linkFILE 作为字符串,ByVal _folha 作为字符串,ByVal DGV 作为 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
于 2013-02-08T14:08:56.273 回答