12,162
社区成员
发帖
与我相关
我的任务
分享
Dim path As String = System.IO.Path.GetDirectoryName(excelPath) + "\test.log"
Dim MyWriter As New System.IO.StreamWriter(path, True, System.Text.Encoding.Default)
Dim objConn As OleDbConnection = Nothing
Try
MyWriter.WriteLine(1)
Dim ds As DataSet = New DataSet()
If System.IO.File.Exists(excelPath) = False Then '判断文件是否存在
Return Nothing
End If
'获得excel类型
Dim strExtension As String = System.IO.Path.GetExtension(excelPath).ToString
Dim strFileName As String = System.IO.Path.GetFileName(excelPath).ToString
MyWriter.WriteLine(strExtension + "|" + strFileName.ToString)
'判断是何种类型采用相应连接
Select Case strExtension
Case ".xls"
objConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelPath + ";" + "Extended Properties='Excel 8.0;HDR=yes;'")
Case ".xlsx"
MyWriter.WriteLine(31)
objConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";" + "Extended Properties='Excel 12.0 XML;HDR=yes;'")
Case Else
Return Nothing
End Select
MyWriter.WriteLine(32)
objConn.Open()
MyWriter.WriteLine(3)
'获得excel表下的sheet名
Dim schemaTable As DataTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, Nothing)
'获得第一个sheet名
Dim tableName As String = ""
For i As Integer = 0 To schemaTable.Rows.Count - 1 Step 1
If InStr(schemaTable.Rows(i)("TABLE_NAME").ToString, "$") Then
tableName = schemaTable.Rows(i)("TABLE_NAME").ToString
Exit For
End If
Next