2014-03-27 88 views
3

我的excel csv文件在文件名中有一個點。 (這是行業命名慣例,所以我不能改變它。)我正在嘗試使用OLEDB讀取所有數據。如果我從文件名刪除點,我的命令起作用。如果沒有,它說無效的對象。如何忽略/解析文件名中的點?使用OLEDB讀取帶有額外點的文件名中的Excel csv文件

文件名:ABC測試銀行開拓者2014年2月$ 18083.65.csv

我的代碼:

Public Function GetAllData(ByVal FileName As String) As DataTable 

    Try 
     Dim dt As New DataTable 

     Using con As New OleDbConnection With {.ConnectionString = String.Format(ConnectionNoHeader, Path.GetDirectoryName(FileName))} 
      cn.Open() 
      Dim SQLAdapter As OleDbDataAdapter = New OleDbDataAdapter()    
      Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [" + Path.GetFileName(FileName) + "]", con) 
      SQLAdapter.SelectCommand = cmd 
      SQLAdapter.Fill(dt) 

     End Using 
     Return dt 
    Catch 
     Return Nothing 
    End Try 
End Function 
+1

如果你只是讀取文件一次,然後一招是讓一些其他名稱相同的文件的副本,如abc.csv,然後做你的查詢,並刪除文件 –

+0

好主意。但問題是這些文件很大,因此可能會影響性能。如果什麼都行不通,我必須走這條路。 – voddy

+1

+1其他人有過同樣的問題 – SSS

回答

3

http://social.msdn.microsoft.com/Forums/en-US/22302a07-d599-46c5-be19-6164156e7762/reading-a-csv-file-with-embedded-periods-in-the-file-name-using-oledbcommand?forum=adodotnetdataproviders建議使用API​​調用GetShortPathName使用舊的MS-DOS 8.3版本的文件名。

Imports System.IO 
Imports System.Data.OleDb 
Imports System.Runtime.InteropServices 'Add this line' 

Public Class Form1 
    'Add the following line' 
    Declare Unicode Function GetShortPathName Lib "kernel32.dll" Alias "GetShortPathNameW" (ByVal longPath As String, <MarshalAs(UnmanagedType.LPTStr)> ByVal ShortPath As System.Text.StringBuilder, <MarshalAs(UnmanagedType.U4)> ByVal bufferSize As Integer) As Integer 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim dtb As DataTable = GetAllData("C:\Junk\ABC Test bank Trail Feb 2014 $ 18083.65.csv") 
    DataGridView1.DataSource = dtb 
    End Sub 

    Public Function GetAllData(ByVal FileName As String) As DataTable 
    Try 
     Dim dt As New DataTable 
     Dim strcnn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & Path.GetDirectoryName(FileName) & "';Extended Properties=""text;HDR=No;FMT=Delimited""" 
     Using con As New OleDbConnection With {.ConnectionString = strcnn} 
     con.Open() 
     Dim SQLAdapter As OleDbDataAdapter = New OleDbDataAdapter() 
     If InStr(FileName, ".") <> InStrRev(FileName, ".") Then 'Add these lines' 
      Dim sb As New System.Text.StringBuilder(256)   'Add these lines' 
      Call GetShortPathName(FileName, sb, 256)    'Add these lines' 
      FileName = sb.ToString         'Add these lines' 
     End If             'Add these lines' 
     Dim strcmd As String = "SELECT * FROM [" + Path.GetFileName(FileName) + "]" 
     Dim cmd As OleDbCommand = New OleDbCommand(strcmd, con) 
     SQLAdapter.SelectCommand = cmd 
     SQLAdapter.Fill(dt) 
     End Using 
     Return dt 
    Catch 
     Return Nothing 
    End Try 
    End Function 
End Class 
+0

P.S.我在Windows 8.1下測試了它,它工作正常 – SSS

+0

非常感謝。這正是我所追求的。 – voddy

+0

不客氣!快樂編程。 – SSS

相關問題