2016-01-23 81 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