有额外的点使用OLEDB读取Excel的CSV文件的文件名文件名、文件、OLEDB、Excel

由网友(吻祢美眸╭許祢一世薀柔ァ)分享简介:我已经练成csv文件,在文件名点。 (这是行业命名惯例,所以我不能改变它。)我想读取使用OLEDB的所有数据。如果我从文件名,我命令作品删除点。如果不是它说无效的对象。如何忽略/解决点在文件名?文件名:ABC试题库步道2014年2月$ 18083.65.csv 我的code:公共功能GetAllData(BYVAL文件...

我已经练成csv文件,在文件名点。 (这是行业命名惯例,所以我不能改变它。)我想读取使用OLEDB的所有数据。如果我从文件名,我命令作品删除点。如果不是它说无效的对象。如何忽略/解决点在文件名?

文件名:ABC试题库步道2014年2月$ 18083.65.csv

我的code:

 公共功能GetAllData(BYVAL文件名作为字符串)作为数据表

    尝试
        昏暗DT作为新数据表

        使用CON作为新的OleDbConnection随着{.ConnectionString =的String.Format(ConnectionNoHeader,Path.GetDirectoryName(文件名))}
            cn.Open()
            昏暗SQLAdapter作为OleDbDataAdapter的=新OleDbDataAdapter的()
            昏暗的CMD作为OleDbCommand的=新的OleDbCommand(SELECT * FROM [+ Path.GetFileName(文件名)+],CON)
            SQLAdapter.SelectCommand = CMD
            SQLAdapter.Fill(DT)

        结束使用
        返回DT
    抓住
        返回任何结果
    结束尝试
端功能
 

解决方案

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版的文件名。

 进口System.IO
进口System.Data.OleDb
进口了System.Runtime.InteropServices加入这一行

公共类Form1中
  添加以下行
  声明统一code函数GetShortPathName库KERNEL32.DLL别名GetShortPathNameW(BYVAL longPath作为字符串,其中&;的MarshalAs(UnmanagedType.LPTStr)> BYVAL ShortPath作为System.Text.StringBuilder,<的MarshalAs(UnmanagedType.U4 )> BYVAL BUFFERSIZE为整数)作为整数

  私人小组的button1_Click(发送者为对象,E作为EventArgs的)把手Button1.Click
    DIM DTB作为数据表= GetAllData(C:垃圾 ABC测试的银行步道2014年2月$ 18083.65.csv)
    DataGridView1.DataSource = DTB
  结束小组

  公共职能GetAllData(BYVAL文件名作为字符串)作为数据表
    尝试
      昏暗DT作为新数据表
      昏暗strcnn作为字符串=供应商= Microsoft.Jet.OLEDB.4.0;数据源='&放大器; Path.GetDirectoryName(文件名)及';扩展属性=文字; HDR =无; FMT =分隔符号
      使用CON作为新的OleDbConnection随着{.ConnectionString = strcnn}
        con.Open()
        昏暗SQLAdapter作为OleDbDataAdapter的=新OleDbDataAdapter的()
        如果INSTR(文件名。)<> InStrRev(文件名。),然后添加这些行
          昏暗某人作为新System.Text.StringBuilder(256)添加这些行
          呼叫GetShortPathName(文件名,SB,256)添加这些行
          文件名= sb.ToString'添加这些行
        结束如果添加这些行
        DIM strcmd作为字符串=SELECT * FROM [+ Path.GetFileName(文件名)+]
        昏暗CMD作为OleDbCommand的=新的OleDbCommand(strcmd,CON)
        SQLAdapter.SelectCommand = CMD
        SQLAdapter.Fill(DT)
      结束使用
      返回DT
    抓住
      返回任何结果
    结束尝试
  端功能
末级
 

如何把一个或几个CSV文件中的数据读取到Excel表格中

I have excel csv file with a dot in file name. (This is the industry naming convention, so I cannot change it.) I am trying to read all data using OLEDB. If I remove the dot from file name, my command works. If not it says invalid object. How to ignore/resolve dot in file name ?

file name : ABC Test bank Trail Feb 2014 $ 18083.65.csv

my code:

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

解决方案

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 suggests using the API call GetShortPathName to use the old MS-DOS 8.3 version of the filename.

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:JunkABC 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

阅读全文

相关推荐

最新文章