从excel读表到vb

时间:2014-03-20 11:30:43

标签: vb.net

您好我已经编写/修改了以下代码以从excel读取表格。不幸的是,这个表非常大,超过600行和700列,操作需要一个半小时才能完成。有没有人在这么大的桌子里有更快的阅读方式?

Public Function ReadExcelTable(ExcelFileName As String, ExcelWSName As String, FirstRow As Integer, LastRow As Integer, FirstCol As Integer, LastCol As Integer) As String(,)

    ' define variables
    Dim CurrRow As Integer, CurrCol As Integer
    Dim OutputTable(0 To LastRow - FirstRow, 0 To LastCol - FirstCol) As String

    Dim xlApp As Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet

    'xlApp.Visible = True
    xlApp = CreateObject("Excel.Application")
    xlWorkBook = xlApp.Workbooks.Open(ExcelFileName)
    xlWorkSheet = xlWorkBook.Worksheets(ExcelWSName)

    CurrRow = -1
    For rCnt = FirstRow To LastRow
        CurrCol = -1
        CurrRow = CurrRow + 1
        For cCnt = FirstCol To LastCol
            CurrCol = CurrCol + 1
            If Not IsNothing(xlWorkSheet.Cells(rCnt, cCnt).Value) Then
                OutputTable(CurrRow, CurrCol) = PrepareString(xlWorkSheet.Cells(rCnt, cCnt).Value.ToString)
            Else
                OutputTable(CurrRow, CurrCol) = ""
            End If
        Next
    Next

    ReadExcelTable = OutputTable

    xlWorkBook.Close(False)
    Call Kill_Excel()
    'xlApp.Quit()

    xlWorkSheet = Nothing
    xlWorkBook = Nothing
    xlApp = Nothing


End Function

1 个答案:

答案 0 :(得分:0)

您可以尝试使用OleDb而不是Interop 这只是一个试图模仿你的方法的伪代码。 可能需要一些调整。 (以connectionstring part here为例) 但我希望能给你这个主意。

Public Function ReadExcelTable(ExcelFileName As String, ExcelWSName As String, FirstRow As Integer, LastRow As Integer, FirstCol As Integer, LastCol As Integer) As String(,)

    Try 
        Dim curRow As Integer = 1
        Dim con as String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
                             ExcelFileName & ";Extended Properties='Excel 12.0 Xml;HDR=No;'"
        Using cn = New System.Data.OleDb.OleDbConnection(con)
           cn.Open() 
           Dim cmd = New System.Data.OleDb.OleDbCommand("select * from [" & ExcelWSName &"$]", cn) 
           Using dr As System.Data.OleDb.OleDbDataReader = cmd.ExecuteReader 
           While dr.Read 
              if curRow >= FirstRow Then
                   ''' reading values from excel 
                  Dim curCol As Integer = 0
                  For cCnt = FirstCol To LastCol
                      If Not reader.IsDBNull(cCnt)) Then
                         OutputTable(CurRow, curCol) = PrepareString(reader(cCnt).ToString())
                      Else
                         OutputTable(CurRow, curCol) = ""
                      End If
                      curCol += 1
                  Next
              End If
              curRow+= 1
              if curRow > LastRow Then
                 Exit While
              End If
           End While 
           End Using 
        End Using 
    Catch ex As Exception 
        Console.WriteLine(ex.ToString()) 
    End Try 
End Sub