2016-01-23 53 views
0

我似乎無法使用下面的連接字符串來讀取.xlsx文件:VB.NET閱讀XLSX文件給出了不是有效的路徑

Webconfig

<add name="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'"/> 

代碼文件

conStr = ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString 

Dim connExcel As New OleDbConnection(conStr) 
connExcel.Open() 

我得到這個錯誤:

Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)". OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" returned message "'F:\Ishan\Projects\ImportExcel2DB\ImportExcel2DB\Files\Whole Extract.xlsx' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.".

並且是在此特定位置存在文件。任何幫助,將不勝感激!

+0

我的項目在你的連接字符串'{0}'和'{1}'。你傳遞給這些人的價值是什麼? –

回答

0

的HDR必須是或否請參閱下面

Imports System.IO 
Imports System.Data 
Imports System.Data.OleDb 
Module Module1 

    Sub Main() 
     Dim reader As New CSVReader() 
     Dim ds As DataSet = reader.ReadCSVFile("filename", True) 
    End Sub 

End Module 

Public Class CSVReader 

    Public Function ReadCSVFile(ByVal fullPath As String, ByVal headerRow As Boolean) As DataSet 

     Dim path As String = fullPath.Substring(0, fullPath.LastIndexOf("\") + 1) 
     Dim filename As String = fullPath.Substring(fullPath.LastIndexOf("\") + 1) 
     Dim ds As DataSet = New DataSet() 

     Dim header As String 
     If headerRow Then 
      header = "Yes" 
     Else 
      header = "No" 
     End If 

     Try 
      If File.Exists(fullPath) Then 

       Dim ConStr As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}" + ";Extended Properties=""Text;HDR={1};FMT=Delimited\""", path, header) 
       Dim SQL As String = String.Format("SELECT * FROM {0}", filename) 
       Dim adapter As OleDbDataAdapter = New OleDbDataAdapter(SQL, ConStr) 
       adapter.Fill(ds, "TextFile") 
       ds.Tables(0).TableName = "Table1" 
      End If 
      For Each col As DataColumn In ds.Tables("Table1").Columns 
       col.ColumnName = col.ColumnName.Replace(" ", "_") 
      Next 

     Catch ex As Exception 
      Console.WriteLine(ex.Message) 
     End Try 
     Return ds 
    End Function 
End Class 
+0

嗨JD,我期待VB.net的答案! –

+0

轉換的C#代碼 – jdweng