ファイルの先頭に、最初の行がスキップされる原因がありますか?たぶん、印刷できない文字ですか? NPCは、期待されるエンコーディングで保存されていないファイルから来る可能性があります。私がCSVファイルを作成したとき、私はあなたが期待した結果を受け取りました。ここで私がテストに使用するコードです:
Private Sub Test()
Dim TempDir = My.Computer.FileSystem.SpecialDirectories.Temp
Dim TempFile = "Test.csv"
'//Create our test file with a header row and three data rows
Using FS As New System.IO.FileStream(System.IO.Path.Combine(TempDir, TempFile), IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.Read)
Using SW As New System.IO.StreamWriter(FS, System.Text.Encoding.ASCII)
SW.WriteLine("Col1,Col2")
SW.WriteLine("R1", "R1")
SW.WriteLine("R2", "R2")
SW.WriteLine("R3", "R3")
End Using
End Using
'//Read the data into a table specifying that the first row should be treated as a header
Using dtData As New DataTable()
Using da As New System.Data.OleDb.OleDbDataAdapter
Using cn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & TempDir & ";" & "Extended Properties=""Text;HDR=Yes;FMT=Delimited;""")
Using cd As New System.Data.OleDb.OleDbCommand("SELECT * FROM " & TempFile, cn)
cn.Open()
da.SelectCommand = cd
da.Fill(dtData)
Trace.WriteLine("With header, expected 3, found " & dtData.Rows.Count)
End Using
End Using
End Using
End Using
'//Read the data into a table again, this time specifying that the there isn't a header row
Using dtData As New DataTable()
Using da As New System.Data.OleDb.OleDbDataAdapter
Using cn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & TempDir & ";" & "Extended Properties=""Text;HDR=No;FMT=Delimited;""")
Using cd As New System.Data.OleDb.OleDbCommand("SELECT * FROM " & TempFile, cn)
cn.Open()
da.SelectCommand = cd
da.Fill(dtData)
Trace.WriteLine("Without header, expected 4, found " & dtData.Rows.Count)
End Using
End Using
End Using
End Using
'//Delete our temporary file
System.IO.File.Delete(System.IO.Path.Combine(TempDir, TempFile))
End Sub
あなたがUnicodeに最初のエンコーディングを変更する場合は、あなたが見ているかもしれない何かである代わりに、結果に8と9行を取得します。エンコードの問題が判明した場合は、拡張プロパティにCharacterSet=Unicode
を追加できます。
以下のように、strCSVFilenameにパスがある場合は何らかの理由で、最初の行をヘッダ行として扱います。ファイル名のみを持つ場合(そして、接続文字列によってADOにどのパスを使用するかが指示されます)、最初の行がデータテーブルにインポートされます。 – David
私のために働かなかった。データソースにはパスがあり、SELECTステートメントにはファイル名のみがあります。 HDR = NOですが、1行目のフィールドは列ヘッダーになります。 –