Advertisement
ASP_Volume3 System Services/ Functions #55538

GetDataTableFromExcel() Function

Function that returns ADO.NET DataTable from MS Excel file using Microsoft.Jet.OLEDB.4.0 Provider.

AI

AIサマリー: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

ソースコード
original-source
Upload
Private Function GetDataTableFromExcel(ByVal sFilePath As String) As System.Data.DataTable
    Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" + sFilePath + ";" & _
    "Extended Properties=""Excel 8.0;"""
    Dim cn As New OleDb.OleDbConnection(sConnectionString)
    cn.Open()
    Dim oTables As DataTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
    Dim i As Integer
    For i = 0 To oTables.Rows.Count - 1
      Dim sSheetName As String = oTables.Rows(i)("TABLE_NAME").ToString()
      If sSheetName.IndexOf("$") <> -1 Then
        Dim oDataSet As New DataSet
        Dim oAdapter As New OleDbDataAdapter("SELECT * FROM [" + sSheetName + "]", cn)
        oAdapter.TableMappings.Add("Table", sSheetName)
        oAdapter.Fill(oDataSet)
        Dim oDataTable As DataTable = oDataSet.Tables(0)
        cn.Close()
        If oDataTable.Rows.Count > 0 And oDataTable.Columns.Count > 5 Then
          Return oDataTable
        End If
      End If
    Next
  End Function
オリジナルのコメント (3)
Wayback Machineから復元